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