Browse Source

Fix escape do TeXu

jk-bakalarka
Jiří Kalvoda 8 months ago
parent
commit
7230d87f6b
  1. 32
      src/formatitko/tex_generator.py

32
src/formatitko/tex_generator.py

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

Loading…
Cancel
Save