Fix escape do TeXu

This commit is contained in:
Jiří Kalvoda 2024-03-02 19:06:03 +01:00
parent bf46105db8
commit 7230d87f6b

View file

@ -27,19 +27,25 @@ class UCWTexGenerator(OutputGenerator):
super().__init__(output_file, *args, **kwargs)
def escape_special_chars(self, text: str) -> str:
text = text.replace("&", r"\&")
text = text.replace("%", r"\%")
text = text.replace("$", r"\$")
text = text.replace("#", r"\#")
text = text.replace("_", r"\_")
text = text.replace("{", r"\{")
text = text.replace("}", r"\}")
text = text.replace("~", r"\textasciitilde{}")
text = text.replace("^", r"\textasciicircum{}")
text = text.replace("\\", r"\textbackslash{}")
text = text.replace(" ", "~") # We use unicode no-break spaces to force nbsp in output
text = text.replace("", "")
return text
if '\\' in text:
print("ESCAPE", text)
out = ""
for char in text:
out += {
'&': r"\&",
'%': r"\%",
'$': r"\$",
'#': r"\#",
'_': r"\_",
'{': r"\{",
'}': r"\}",
'~': r"\textasciitilde{}",
'^': r"\textasciicircum{}",
'\\': r"\textbackslash{}",
' ': r"~",
'': r"",
}.get(char, char)
return out
def generate(self, e: Union[Element, ListContainer]):
if hasattr(e, "attributes") and "only" in e.attributes and e.attributes["only"] != "tex":