Use str.replace() instead of regex. #13

This commit is contained in:
Jan Černohorský 2023-07-20 22:23:23 +02:00
parent fe63458a51
commit a3d392c4fb
2 changed files with 17 additions and 18 deletions

View file

@ -4,7 +4,6 @@ from panflute import TableRow, TableCell, Caption, Doc
from panflute import ListContainer, Element from panflute import ListContainer, Element
from typing import Union, Dict from typing import Union, Dict
import re
import os import os
import io import io
import warnings import warnings
@ -33,12 +32,12 @@ class HTMLGenerator(OutputGenerator):
super().generate(e) super().generate(e)
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
text = re.sub(re.compile(r"&"), "&", text) text = text.replace("&", "&")
text = re.sub(re.compile(r"<"), "&lt;", text) text = text.replace("<", "&lt;")
text = re.sub(re.compile(r">"), "&rt;", text) text = text.replace(">", "&rt;")
text = re.sub(re.compile(r"\""), "&quot;", text) text = text.replace("\"", "&quot;")
text = re.sub(re.compile(r"'"), "&#39;", text) text = text.replace("'", "&#39;")
# text = re.sub(re.compile(r" '), "&nbsp;", text) # Don't replace no-break spaces with HTML escapes, because we trust unicode? # text = text.replace(" ", "&nbsp;") # Don't replace no-break spaces with HTML escapes, because we trust unicode?
return text return text
def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: def stag(self, tag: str, attributes: Dict[str,str]={}) -> str:

View file

@ -23,17 +23,17 @@ class LaTeXGenerator(OutputGenerator):
super().generate(e) super().generate(e)
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
text = re.sub(re.compile(r"&"), "\\&", text) text = text.replace("&", "\\&")
text = re.sub(re.compile(r"%"), "\\%", text) text = text.replace("%", "\\%")
text = re.sub(re.compile(r"\$"), "\\$", text) text = text.replace("$", "\\$")
text = re.sub(re.compile(r"#"), "\\#", text) text = text.replace("#", "\\#")
text = re.sub(re.compile(r"_"), "\\_", text) text = text.replace("_", "\\_")
text = re.sub(re.compile(r"\{"), "\\{", text) text = text.replace("{", "\\{")
text = re.sub(re.compile(r"\}"), "\\}", text) text = text.replace("}", "\\}")
text = re.sub(re.compile(r"~"), "\\textasciitilde{}", text) text = text.replace("~", "\\textasciitilde{}")
text = re.sub(re.compile(r"\^"), "\\textasciicircum{}", text) text = text.replace("^", "\\textasciicircum{}")
text = re.sub(re.compile(r"\\"), "\\textbackslash{}", text) text = text.replace("\\", "\\textbackslash{}")
text = re.sub(re.compile(r" "), "~", text) # We use unicode no-break spaces to force nbsp in output text = text.replace(" ", "~") # We use unicode no-break spaces to force nbsp in output
return text return text
def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: