mirror of https://github.com/eyhc1/rendercv.git
escape LaTeX characters #2
This commit is contained in:
parent
80a1c4be1f
commit
9b253f93d8
|
@ -138,14 +138,19 @@ def escape_latex_characters(sentence: str) -> str:
|
||||||
"~": r"\textasciitilde{}",
|
"~": r"\textasciitilde{}",
|
||||||
"_": r"\_",
|
"_": r"\_",
|
||||||
"^": r"\textasciicircum{}",
|
"^": r"\textasciicircum{}",
|
||||||
"{": r"\{",
|
|
||||||
"}": r"\}",
|
|
||||||
"\\": r"\textbackslash{}",
|
|
||||||
}
|
}
|
||||||
|
# Handle backslash and curly braces separately because the other characters are
|
||||||
|
# escaped with backslash and curly braces:
|
||||||
|
sentence = sentence.replace("{", ">>{")
|
||||||
|
sentence = sentence.replace("}", ">>}")
|
||||||
|
sentence = sentence.replace("\\", "\\textbackslash{}")
|
||||||
|
sentence = sentence.replace(">>{", "\\{")
|
||||||
|
sentence = sentence.replace(">>}", "\\}")
|
||||||
|
|
||||||
# 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:
|
||||||
for character in sentence:
|
copy_of_the_sentence = sentence
|
||||||
|
for character in copy_of_the_sentence:
|
||||||
if character in escape_characters:
|
if character in escape_characters:
|
||||||
sentence = sentence.replace(character, escape_characters[character])
|
sentence = sentence.replace(character, escape_characters[character])
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ class TestDataModel(unittest.TestCase):
|
||||||
|
|
||||||
str_with_latex_characers = r"asdf#asdf$asdf%asdf& ~ fd_ \ ^aa aa{ bb}"
|
str_with_latex_characers = r"asdf#asdf$asdf%asdf& ~ fd_ \ ^aa aa{ bb}"
|
||||||
expected = (
|
expected = (
|
||||||
r"asdf\#asdf\$asdf\%asdf\& \textasciitilde{}\ fd\_ \textbackslash{}"
|
r"asdf\#asdf\$asdf\%asdf\& \textasciitilde{} fd\_ \textbackslash{}"
|
||||||
r" \textasciicircum{}aa aa\{ bb\}"
|
r" \textasciicircum{}aa aa\{ bb\}"
|
||||||
)
|
)
|
||||||
with self.subTest(msg="string with LaTeX characters"):
|
with self.subTest(msg="string with LaTeX characters"):
|
||||||
|
|
Loading…
Reference in New Issue