mirror of https://github.com/eyhc1/rendercv.git
renderer: escape special characters in link placeholders (#112)
This commit is contained in:
parent
298a198224
commit
bfde7bb42b
|
@ -362,28 +362,45 @@ def escape_latex_characters(latex_string: str, strict: bool = True) -> str:
|
||||||
"~": "\\textasciitilde{}",
|
"~": "\\textasciitilde{}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strict_escape_characters = {
|
||||||
|
"$": "\\$",
|
||||||
|
"_": "\\_",
|
||||||
|
"^": "\\textasciicircum{}",
|
||||||
|
}
|
||||||
|
|
||||||
if strict:
|
if strict:
|
||||||
# To allow math input, users can use this function with strict = False
|
# To allow math input, users can use this function with strict = False
|
||||||
escape_characters["$"] = "\\$"
|
escape_characters.update(strict_escape_characters)
|
||||||
escape_characters["_"] = "\\_"
|
|
||||||
escape_characters["^"] = "\\textasciicircum{}"
|
|
||||||
|
|
||||||
# Don't escape links as hyperref package will do it automatically:
|
translation_map = str.maketrans(escape_characters)
|
||||||
|
strict_translation_map = str.maketrans(strict_escape_characters)
|
||||||
|
|
||||||
|
# Don't escape urls as hyperref package will do it automatically:
|
||||||
|
# Also always escape link placeholders strictly (as we don't expect any math in
|
||||||
|
# them):
|
||||||
# Find all the links in the sentence:
|
# Find all the links in the sentence:
|
||||||
links = re.findall(r"\[.*?\]\(.*?\)", latex_string)
|
links = re.findall(r"\[(.*?)\]\((.*?)\)", latex_string)
|
||||||
|
|
||||||
# Replace the links with a placeholder:
|
# Replace the links with a dummy string and save links with escaped characters:
|
||||||
|
new_links = []
|
||||||
for i, link in enumerate(links):
|
for i, link in enumerate(links):
|
||||||
latex_string = latex_string.replace(link, f"!!-link{i}-!!")
|
placeholder = link[0]
|
||||||
|
escaped_placeholder = placeholder.translate(strict_translation_map)
|
||||||
|
url = link[1]
|
||||||
|
|
||||||
|
original_link = f"[{placeholder}]({url})"
|
||||||
|
latex_string = latex_string.replace(original_link, f"!!-link{i}-!!")
|
||||||
|
|
||||||
|
new_link = f"[{escaped_placeholder}]({url})"
|
||||||
|
new_links.append(new_link)
|
||||||
|
|
||||||
# Loop through the letters of the sentence and if you find an escape character,
|
# Loop through the letters of the sentence and if you find an escape character,
|
||||||
# replace it with its LaTeX equivalent:
|
# replace it with its LaTeX equivalent:
|
||||||
translation_map = str.maketrans(escape_characters)
|
|
||||||
latex_string = latex_string.translate(translation_map)
|
latex_string = latex_string.translate(translation_map)
|
||||||
|
|
||||||
# Replace the links with the original links:
|
# Replace !!-link{i}-!!" with the original urls:
|
||||||
for i, link in enumerate(links):
|
for i, new_link in enumerate(new_links):
|
||||||
latex_string = latex_string.replace(f"!!-link{i}-!!", link)
|
latex_string = latex_string.replace(f"!!-link{i}-!!", new_link)
|
||||||
|
|
||||||
return latex_string
|
return latex_string
|
||||||
|
|
||||||
|
|
|
@ -97,11 +97,11 @@ def test_markdown_file_class(tmp_path, rendercv_data_model, jinja2_environment):
|
||||||
("##%%&&~~", "\\#\\#\\%\\%\\&\\&\\textasciitilde{}\\textasciitilde{}"),
|
("##%%&&~~", "\\#\\#\\%\\%\\&\\&\\textasciitilde{}\\textasciitilde{}"),
|
||||||
(
|
(
|
||||||
(
|
(
|
||||||
"[link](you shouldn't escape whatever is in here & % # ~) [second"
|
"[link_test](you shouldn't escape whatever is in here & % # ~) [second"
|
||||||
" link](https://myurl.com)"
|
" link](https://myurl.com)"
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"[link](you shouldn't escape whatever is in here & % # ~) [second"
|
"[link\\_test](you shouldn't escape whatever is in here & % # ~) [second"
|
||||||
" link](https://myurl.com)"
|
" link](https://myurl.com)"
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue