diff options
Diffstat (limited to 'sphinx/util/texescape.py')
-rw-r--r-- | sphinx/util/texescape.py | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/sphinx/util/texescape.py b/sphinx/util/texescape.py index 4e7055119..c3231d3d3 100644 --- a/sphinx/util/texescape.py +++ b/sphinx/util/texescape.py @@ -32,7 +32,6 @@ tex_replacements = [ ('¶', r'\P{}'), ('§', r'\S{}'), ('€', r'\texteuro{}'), - ('∞', r'\(\infty\)'), ('±', r'\(\pm\)'), ('→', r'\(\rightarrow\)'), ('‣', r'\(\rightarrow\)'), @@ -53,6 +52,8 @@ tex_replacements = [ # A map Unicode characters to LaTeX representation # (for LaTeX engines which don't support unicode) unicode_tex_replacements = [ + # map special Unicode characters to TeX commands + ('∞', r'\(\infty\)'), # superscript ('⁰', r'\(\sp{\text{0}}\)'), ('¹', r'\(\sp{\text{1}}\)'), @@ -80,7 +81,8 @@ unicode_tex_replacements = [ tex_escape_map = {} # type: Dict[int, str] tex_escape_map_without_unicode = {} # type: Dict[int, str] tex_replace_map = {} -tex_hl_escape_map_new = {} +tex_hl_escape_map_new = {} # type: Dict[int, str] +tex_hl_escape_map_new_without_unicode = {} # type: Dict[int, str] def get_escape_func(latex_engine: str) -> Callable[[str], str]: @@ -101,6 +103,24 @@ def escape_for_unicode_latex_engine(s: str) -> str: return s.translate(tex_escape_map_without_unicode) +def get_hlescape_func(latex_engine: str) -> Callable[[str], str]: + """Get hlescape() function for given latex_engine.""" + if latex_engine in ('lualatex', 'xelatex'): + return hlescape_for_unicode_latex_engine + else: + return hlescape + + +def hlescape(s: str) -> str: + """Escape text for LaTeX highlighter.""" + return s.translate(tex_hl_escape_map_new) + + +def hlescape_for_unicode_latex_engine(s: str) -> str: + """Escape text for unicode supporting LaTeX engine.""" + return s.translate(tex_hl_escape_map_new_without_unicode) + + def escape_abbr(text: str) -> str: """Adjust spacing after abbreviations. Works with @ letter or other.""" return re.sub(r'\.(?=\s|$)', r'.\@{}', text) @@ -120,3 +140,7 @@ def init() -> None: if a in '[]{}\\': continue tex_hl_escape_map_new[ord(a)] = b + tex_hl_escape_map_new_without_unicode[ord(a)] = b + + for a, b in unicode_tex_replacements: + tex_hl_escape_map_new[ord(a)] = b |