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