Show More
@@ -1,63 +1,63 | |||||
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 |
|
48 | |||
49 | def escape_latex(text): |
|
49 | def escape_latex(text): | |
50 | """ |
|
50 | """ | |
51 | Escape characters that may conflict with latex. |
|
51 | Escape characters that may conflict with latex. | |
52 |
|
52 | |||
53 | Parameters |
|
53 | Parameters | |
54 | ---------- |
|
54 | ---------- | |
55 | text : str |
|
55 | text : str | |
56 | Text containing characters that may conflict with Latex |
|
56 | Text containing characters that may conflict with Latex | |
57 | """ |
|
57 | """ | |
58 | text = ''.join(LATEX_SUBS.get(c, c) for c in text) |
|
58 | text = ''.join(LATEX_SUBS.get(c, c) for c in text) | |
59 | for pattern, replacement in LATEX_RE_SUBS: |
|
59 | for pattern, replacement in LATEX_RE_SUBS: | |
60 | text = pattern.sub(replacement, text) |
|
60 | text = pattern.sub(replacement, text) | |
61 |
|
61 | |||
62 | return text |
|
62 | return text | |
63 |
|
63 |
General Comments 0
You need to be logged in to leave comments.
Login now