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