Skip to content

Commit

Permalink
fix(datasheet): unescape braces in .md templates
Browse files Browse the repository at this point in the history
  • Loading branch information
htfab committed May 6, 2024
1 parent 96e1ab9 commit ac8a546
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
6 changes: 1 addition & 5 deletions documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ def write_datasheet(self, markdown_file: str, pdf_file: Optional[str] = None):

# now build the doc & print it
try:
doc = (
doc_template.replace("__git_url__", "{git_url}")
.replace("__doc_link__", "{doc_link}")
.format(**yaml_data)
)
doc = doc_template.format(**yaml_data)
fh.write(doc)
fh.write("\n\\clearpage\n")
except IndexError:
Expand Down
28 changes: 19 additions & 9 deletions markdown_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,32 @@ def limit_markdown_headings(source: str, min_level: int) -> str:
return markdown(source)


def unescape_braces(text: str):
return text.replace("%7B", "{").replace("%7D", "}")


class ImagePathRewriterRenderer(MarkdownRenderer):
def __init__(self, prefix: str):
super().__init__()
self.prefix = prefix

def image(self, token, state):
url = token["attrs"]["url"]
if "://" not in url and not url.startswith("/"):
token["attrs"]["url"] = os.path.join(self.prefix, url)
if "%7B" in url:
pass
elif "://" not in url and not url.startswith("/"):
url = os.path.join(self.prefix, url)
elif ".." in url:
token["attrs"]["url"] = ""
url = ""
elif url.startswith("/"):
token["attrs"]["url"] = os.path.join(self.prefix, "../" + url[1:])
url = os.path.join(self.prefix, "../" + url[1:])
token["attrs"]["url"] = url
return super().image(token, state)


def rewrite_image_paths(source: str, prefix: str) -> str:
markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix))
return markdown(source)
return unescape_braces(markdown(source))


class WebsiteImagePathRewriterRenderer(MarkdownRenderer):
Expand All @@ -53,8 +60,10 @@ def __init__(self, source_dir: str, target_dir: str):

def image(self, token, state):
url = token["attrs"]["url"]
if ".." in url:
token["attrs"]["url"] = ""
if "%7B" in url:
pass
elif ".." in url:
url = ""
elif "://" not in url and not url.startswith("/"):
filename = os.path.basename(url)
if url.startswith("/"):
Expand All @@ -63,7 +72,8 @@ def image(self, token, state):
os.path.join(self.source_dir, url),
os.path.join(self.target_dir, filename),
)
token["attrs"]["url"] = f"images/{filename}"
url = f"images/{filename}"
token["attrs"]["url"] = url
return super().image(token, state)


Expand All @@ -73,4 +83,4 @@ def rewrite_image_paths_for_website(
markdown = mistune.create_markdown(
renderer=WebsiteImagePathRewriterRenderer(source_dir, target_dir)
)
return markdown(source)
return unescape_braces(markdown(source))

0 comments on commit ac8a546

Please sign in to comment.