diff options
Diffstat (limited to 'sphinx/util/texescape.py')
-rw-r--r-- | sphinx/util/texescape.py | 70 |
1 files changed, 32 insertions, 38 deletions
diff --git a/sphinx/util/texescape.py b/sphinx/util/texescape.py index e3c45adb4..f490c25c1 100644 --- a/sphinx/util/texescape.py +++ b/sphinx/util/texescape.py @@ -9,7 +9,10 @@ """ import re -from typing import Callable, Dict +from typing import Dict + +from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias + tex_replacements = [ # map TeX special chars @@ -84,47 +87,38 @@ unicode_tex_replacements = [ ('₉', r'\(\sb{\text{9}}\)'), ] -tex_escape_map = {} # type: Dict[int, str] -tex_escape_map_without_unicode = {} # type: Dict[int, str] -tex_replace_map = {} -tex_hl_escape_map_new = {} # type: Dict[int, str] -tex_hl_escape_map_new_without_unicode = {} # type: Dict[int, str] +tex_replace_map = {} # type: Dict[int, str] +_tex_escape_map = {} # type: Dict[int, str] +_tex_escape_map_without_unicode = {} # type: Dict[int, str] +_tex_hlescape_map = {} # type: Dict[int, str] +_tex_hlescape_map_without_unicode = {} # type: Dict[int, str] -def get_escape_func(latex_engine: str) -> Callable[[str], str]: - """Get escape() function for given latex_engine.""" - if latex_engine in ('lualatex', 'xelatex'): - return escape_for_unicode_latex_engine - else: - return escape - - -def escape(s: str) -> str: - """Escape text for LaTeX output.""" - return s.translate(tex_escape_map) +deprecated_alias('sphinx.util.texescape', + { + 'tex_escape_map': _tex_escape_map, + 'tex_hl_escape_map_new': _tex_hlescape_map, + }, + RemovedInSphinx40Warning) -def escape_for_unicode_latex_engine(s: str) -> str: - """Escape text for unicode supporting LaTeX engine.""" - 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.""" +def escape(s: str, latex_engine: str = None) -> str: + """Escape text for LaTeX output.""" if latex_engine in ('lualatex', 'xelatex'): - return hlescape_for_unicode_latex_engine + # unicode based LaTeX engine + return s.translate(_tex_escape_map_without_unicode) else: - return hlescape + return s.translate(_tex_escape_map) -def hlescape(s: str) -> str: +def hlescape(s: str, latex_engine: str = None) -> 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) + if latex_engine in ('lualatex', 'xelatex'): + # unicode based LaTeX engine + return s.translate(_tex_hlescape_map_without_unicode) + else: + return s.translate(_tex_hlescape_map) def escape_abbr(text: str) -> str: @@ -134,19 +128,19 @@ def escape_abbr(text: str) -> str: def init() -> None: for a, b in tex_replacements: - tex_escape_map[ord(a)] = b - tex_escape_map_without_unicode[ord(a)] = b + _tex_escape_map[ord(a)] = b + _tex_escape_map_without_unicode[ord(a)] = b tex_replace_map[ord(a)] = '_' for a, b in unicode_tex_replacements: - tex_escape_map[ord(a)] = b + _tex_escape_map[ord(a)] = b tex_replace_map[ord(a)] = '_' for a, b in tex_replacements: if a in '[]{}\\': continue - tex_hl_escape_map_new[ord(a)] = b - tex_hl_escape_map_new_without_unicode[ord(a)] = b + _tex_hlescape_map[ord(a)] = b + _tex_hlescape_map_without_unicode[ord(a)] = b for a, b in unicode_tex_replacements: - tex_hl_escape_map_new[ord(a)] = b + _tex_hlescape_map[ord(a)] = b |