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