latex.py
63 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10676 | """Latex filters. | ||
Jonathan Frederic
|
r10478 | |||
Jonathan Frederic
|
r10676 | Module of useful filters for processing Latex within Jinja latex templates. | ||
Jonathan Frederic
|
r10478 | """ | ||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10485 | import re | ||
#----------------------------------------------------------------------------- | ||||
# Globals and constants | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r12064 | LATEX_RE_SUBS = ( | ||
(re.compile(r'\.\.\.+'), r'\\ldots'), | ||||
) | ||||
Jonathan Frederic
|
r12062 | # Latex substitutions for escaping latex. | ||
# see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates | ||||
MinRK
|
r12063 | |||
Jonathan Frederic
|
r12062 | LATEX_SUBS = { | ||
'&': r'\&', | ||||
MinRK
|
r12063 | '%': r'\%', | ||
'$': r'\$', | ||||
'#': r'\#', | ||||
'_': r'\_', | ||||
'{': r'\{', | ||||
'}': r'\}', | ||||
'~': r'\textasciitilde{}', | ||||
'^': r'\^{}', | ||||
'\\': r'\textbackslash{}', | ||||
} | ||||
Jonathan Frederic
|
r12062 | |||
Jonathan Frederic
|
r10478 | |||
#----------------------------------------------------------------------------- | ||||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10676 | |||
MinRK
|
r12864 | __all__ = ['escape_latex'] | ||
Brian E. Granger
|
r11088 | |||
Jonathan Frederic
|
r10676 | def escape_latex(text): | ||
""" | ||||
Jonathan Frederic
|
r12080 | Escape characters that may conflict with latex. | ||
Jonathan Frederic
|
r12062 | |||
Jonathan Frederic
|
r10676 | Parameters | ||
---------- | ||||
text : str | ||||
Text containing characters that may conflict with Latex | ||||
""" | ||||
Jonathan Frederic
|
r12075 | text = ''.join(LATEX_SUBS.get(c, c) for c in text) | ||
MinRK
|
r12064 | for pattern, replacement in LATEX_RE_SUBS: | ||
text = pattern.sub(replacement, text) | ||||
Jonathan Frederic
|
r12062 | |||
MinRK
|
r12064 | return text | ||
Jonathan Frederic
|
r10478 | |||