##// END OF EJS Templates
Remove escape ansi
Jonathan Frederic -
Show More
@@ -1,124 +1,123 b''
1 1 """Latex filters.
2 2
3 3 Module of useful filters for processing Latex within Jinja latex templates.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 import re
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Globals and constants
20 20 #-----------------------------------------------------------------------------
21 21
22 22 LATEX_RE_SUBS = (
23 (re.compile('\033\[[0-9;]+m'), ''), # handle console escapes
24 23 (re.compile(r'\.\.\.+'), r'\\ldots'),
25 24 )
26 25
27 26 # Latex substitutions for escaping latex.
28 27 # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
29 28
30 29 LATEX_SUBS = {
31 30 '&': r'\&',
32 31 '%': r'\%',
33 32 '$': r'\$',
34 33 '#': r'\#',
35 34 '_': r'\_',
36 35 '{': r'\{',
37 36 '}': r'\}',
38 37 '~': r'\textasciitilde{}',
39 38 '^': r'\^{}',
40 39 '\\': r'\textbackslash{}',
41 40 }
42 41
43 42
44 43 #-----------------------------------------------------------------------------
45 44 # Functions
46 45 #-----------------------------------------------------------------------------
47 46
48 47 __all__ = ['escape_latex',
49 48 'strip_math_space']
50 49
51 50 def escape_latex(text):
52 51 """
53 52 Remove ansi codes and escape characters that may conflict with latex.
54 53
55 54 Parameters
56 55 ----------
57 56 text : str
58 57 Text containing characters that may conflict with Latex
59 58 """
60 59 text = ''.join(LATEX_SUBS.get(c, c) for c in text)
61 60 for pattern, replacement in LATEX_RE_SUBS:
62 61 text = pattern.sub(replacement, text)
63 62
64 63 return text
65 64
66 65
67 66 def strip_math_space(text):
68 67 """
69 68 Remove the space between latex math commands and enclosing $ symbols.
70 69 This filter is important because latex isn't as flexible as the notebook
71 70 front end when it comes to flagging math using ampersand symbols.
72 71
73 72 Parameters
74 73 ----------
75 74 text : str
76 75 Text to filter.
77 76 """
78 77
79 78 # First, scan through the markdown looking for $. If
80 79 # a $ symbol is found, without a preceding \, assume
81 80 # it is the start of a math block. UNLESS that $ is
82 81 # not followed by another within two math_lines.
83 82 math_regions = []
84 83 math_lines = 0
85 84 within_math = False
86 85 math_start_index = 0
87 86 ptext = ''
88 87 last_character = ""
89 88 skip = False
90 89 for index, char in enumerate(text):
91 90
92 91 #Make sure the character isn't preceeded by a backslash
93 92 if (char == "$" and last_character != "\\"):
94 93
95 94 # Close the math region if this is an ending $
96 95 if within_math:
97 96 within_math = False
98 97 skip = True
99 98 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
100 99 math_regions.append([math_start_index, index+1])
101 100 else:
102 101
103 102 # Start a new math region
104 103 within_math = True
105 104 math_start_index = index
106 105 math_lines = 0
107 106
108 107 # If we are in a math region, count the number of lines parsed.
109 108 # Cancel the math region if we find two line breaks!
110 109 elif char == "\n":
111 110 if within_math:
112 111 math_lines += 1
113 112 if math_lines > 1:
114 113 within_math = False
115 114 ptext = ptext+text[math_start_index:index]
116 115
117 116 # Remember the last character so we can easily watch
118 117 # for backslashes
119 118 last_character = char
120 119 if not within_math and not skip:
121 120 ptext = ptext+char
122 121 if skip:
123 122 skip = False
124 123 return ptext
General Comments 0
You need to be logged in to leave comments. Login now