From a3d392c4fb4ad0b23493994d37f80ef45f35069a Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Thu, 20 Jul 2023 22:23:23 +0200 Subject: [PATCH] Use str.replace() instead of regex. #13 --- src/formatitko/html_generator.py | 13 ++++++------- src/formatitko/latex_generator.py | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/formatitko/html_generator.py b/src/formatitko/html_generator.py index bc0535b..1c2ba68 100644 --- a/src/formatitko/html_generator.py +++ b/src/formatitko/html_generator.py @@ -4,7 +4,6 @@ from panflute import TableRow, TableCell, Caption, Doc from panflute import ListContainer, Element from typing import Union, Dict -import re import os import io import warnings @@ -33,12 +32,12 @@ class HTMLGenerator(OutputGenerator): super().generate(e) def escape_special_chars(self, text: str) -> str: - text = re.sub(re.compile(r"&"), "&", text) - text = re.sub(re.compile(r"<"), "<", text) - text = re.sub(re.compile(r">"), "&rt;", text) - text = re.sub(re.compile(r"\""), """, text) - text = re.sub(re.compile(r"'"), "'", text) - # text = re.sub(re.compile(r" '), " ", text) # Don't replace no-break spaces with HTML escapes, because we trust unicode? + text = text.replace("&", "&") + text = text.replace("<", "<") + text = text.replace(">", "&rt;") + text = text.replace("\"", """) + text = text.replace("'", "'") + # text = text.replace(" ", " ") # Don't replace no-break spaces with HTML escapes, because we trust unicode? return text def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: diff --git a/src/formatitko/latex_generator.py b/src/formatitko/latex_generator.py index a6adf85..4b823a6 100644 --- a/src/formatitko/latex_generator.py +++ b/src/formatitko/latex_generator.py @@ -23,17 +23,17 @@ class LaTeXGenerator(OutputGenerator): super().generate(e) def escape_special_chars(self, text: str) -> str: - text = re.sub(re.compile(r"&"), "\\&", text) - text = re.sub(re.compile(r"%"), "\\%", text) - text = re.sub(re.compile(r"\$"), "\\$", text) - text = re.sub(re.compile(r"#"), "\\#", text) - text = re.sub(re.compile(r"_"), "\\_", text) - text = re.sub(re.compile(r"\{"), "\\{", text) - text = re.sub(re.compile(r"\}"), "\\}", text) - text = re.sub(re.compile(r"~"), "\\textasciitilde{}", text) - text = re.sub(re.compile(r"\^"), "\\textasciicircum{}", text) - text = re.sub(re.compile(r"\\"), "\\textbackslash{}", text) - text = re.sub(re.compile(r" "), "~", text) # We use unicode no-break spaces to force nbsp in output + text = text.replace("&", "\\&") + text = text.replace("%", "\\%") + text = text.replace("$", "\\$") + text = text.replace("#", "\\#") + text = text.replace("_", "\\_") + text = text.replace("{", "\\{") + text = text.replace("}", "\\}") + text = text.replace("~", "\\textasciitilde{}") + text = text.replace("^", "\\textasciicircum{}") + text = text.replace("\\", "\\textbackslash{}") + text = text.replace(" ", "~") # We use unicode no-break spaces to force nbsp in output return text def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: