##// END OF EJS Templates
Escape latex fixes...
Escape latex fixes Added ... to \ldots escape Escape ansi like before

File last commit:

r12069:69adeb15
r12069:69adeb15
Show More
latex.py
124 lines | 3.9 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of filters
r10676 """Latex filters.
Jonathan Frederic
Created filters namespace
r10478
Jonathan Frederic
Cleanup and refactor of filters
r10676 Module of useful filters for processing Latex within Jinja latex templates.
Jonathan Frederic
Created filters namespace
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
Escape latex fixes...
r12069
from ansi import strip_ansi
Jonathan Frederic
Post code-review, extended refactor.
r10485
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
Jonathan Frederic
FIX, don't use regex
r12062 # Latex substitutions for escaping latex.
# see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
LATEX_SUBS = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
Jonathan Frederic
Escape latex fixes...
r12069 '_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
'...': r'\ldots{}',
}
Jonathan Frederic
FIX, don't use regex
r12062
Jonathan Frederic
Created filters namespace
r10478
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor of filters
r10676
Jonathan Frederic
FIX, don't use regex
r12062 __all__ = ['escape_latex',
'strip_math_space']
Brian E. Granger
Fixing import logic.
r11088
Jonathan Frederic
Cleanup and refactor of filters
r10676 def escape_latex(text):
"""
Jonathan Frederic
Escape latex fixes...
r12069 Remove ansi codes and escape characters that may conflict with latex.
Jonathan Frederic
FIX, don't use regex
r12062
Jonathan Frederic
Cleanup and refactor of filters
r10676 Parameters
----------
text : str
Text containing characters that may conflict with Latex
"""
Jonathan Frederic
FIX, don't use regex
r12062
Jonathan Frederic
Escape latex fixes...
r12069 # Remove the ansi coloring from the text and then escape it. Escape single
# characters first and then multiple characters.
text = strip_ansi(text)
text = ''.join([LATEX_SUBS.get(c, c) for c in text])
for search, replace in LATEX_SUBS.items():
if len(search) > 1:
text = text.replace(search,replace)
return text
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Filter names cleanup
r11685 def strip_math_space(text):
Jonathan Frederic
Created filters namespace
r10478 """
Remove the space between latex math commands and enclosing $ symbols.
Jonathan Frederic
Cleanup and refactor of filters
r10676 This filter is important because latex isn't as flexible as the notebook
front end when it comes to flagging math using ampersand symbols.
Parameters
----------
text : str
Text to filter.
Jonathan Frederic
Created filters namespace
r10478 """
# First, scan through the markdown looking for $. If
# a $ symbol is found, without a preceding \, assume
# it is the start of a math block. UNLESS that $ is
# not followed by another within two math_lines.
math_regions = []
math_lines = 0
within_math = False
math_start_index = 0
ptext = ''
last_character = ""
skip = False
for index, char in enumerate(text):
#Make sure the character isn't preceeded by a backslash
if (char == "$" and last_character != "\\"):
# Close the math region if this is an ending $
if within_math:
within_math = False
skip = True
ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
math_regions.append([math_start_index, index+1])
else:
# Start a new math region
within_math = True
math_start_index = index
math_lines = 0
# If we are in a math region, count the number of lines parsed.
# Cancel the math region if we find two line breaks!
elif char == "\n":
if within_math:
math_lines += 1
if math_lines > 1:
within_math = False
ptext = ptext+text[math_start_index:index]
# Remember the last character so we can easily watch
# for backslashes
last_character = char
if not within_math and not skip:
ptext = ptext+char
if skip:
skip = False
return ptext