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