##// END OF EJS Templates
Escape latex fixes...
Jonathan Frederic -
Show More
@@ -13,7 +13,8 b' Module of useful filters for processing Latex within Jinja latex templates.'
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 import re
16
17 from ansi import strip_ansi
17
18
18 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
19 # Globals and constants
20 # Globals and constants
@@ -26,12 +27,14 b' LATEX_SUBS = {'
26 '%': r'\%',
27 '%': r'\%',
27 '$': r'\$',
28 '$': r'\$',
28 '#': r'\#',
29 '#': r'\#',
29 '_': r'\letterunderscore{}',
30 '_': r'\_',
30 '{': r'\letteropenbrace{}',
31 '{': r'\{',
31 '}': r'\letterclosebrace{}',
32 '}': r'\}',
32 '~': r'\lettertilde{}',
33 '~': r'\textasciitilde{}',
33 '^': r'\letterhat{}',
34 '^': r'\^{}',
34 '\\': r'\letterbackslash{}'}
35 '\\': r'\textbackslash{}',
36 '...': r'\ldots{}',
37 }
35
38
36
39
37 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
@@ -43,7 +46,7 b" __all__ = ['escape_latex',"
43
46
44 def escape_latex(text):
47 def escape_latex(text):
45 """
48 """
46 Escape characters that may conflict with latex.
49 Remove ansi codes and escape characters that may conflict with latex.
47
50
48 Parameters
51 Parameters
49 ----------
52 ----------
@@ -51,7 +54,14 b' def escape_latex(text):'
51 Text containing characters that may conflict with Latex
54 Text containing characters that may conflict with Latex
52 """
55 """
53
56
54 return ''.join([LATEX_SUBS.get(c, c) for c in text])
57 # Remove the ansi coloring from the text and then escape it. Escape single
58 # characters first and then multiple characters.
59 text = strip_ansi(text)
60 text = ''.join([LATEX_SUBS.get(c, c) for c in text])
61 for search, replace in LATEX_SUBS.items():
62 if len(search) > 1:
63 text = text.replace(search,replace)
64 return text
55
65
56
66
57 def strip_math_space(text):
67 def strip_math_space(text):
@@ -28,9 +28,10 b' class TestLatex(TestsBase):'
28 def test_escape_latex(self):
28 def test_escape_latex(self):
29 """escape_latex test"""
29 """escape_latex test"""
30 tests = [
30 tests = [
31 (r'How are \you doing today?', r'How are \letterbackslash{}you doing today?'),
31 (r'How are \you doing today?', r'How are \textbackslash{}you doing today?'),
32 (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\letterbackslash{}escapechar=`\letterbackslash{}A\letterbackslash{}catcode`\letterbackslash{}|=0 |string|foo'),
32 (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\textbackslash{}escapechar=`\textbackslash{}A\textbackslash{}catcode`\textbackslash{}|=0 |string|foo'),
33 (r'# $ % & ~ _ ^ \ { }', r'\# \$ \% \& \lettertilde{} \letterunderscore{} \letterhat{} \letterbackslash{} \letteropenbrace{} \letterclosebrace{}'),
33 (r'# $ % & ~ _ ^ \ { }', r'\# \$ \% \& \textasciitilde{} \_ \^{} \textbackslash{} \{ \}'),
34 (r"This SHOULD work... If not, I'm wrong.", "This SHOULD work\ldots{} If not, I'm wrong."),
34 ('','')]
35 ('','')]
35
36
36 for test in tests:
37 for test in tests:
General Comments 0
You need to be logged in to leave comments. Login now