Show More
@@ -1,63 +1,72 b'' | |||
|
1 | 1 | """Latex filters. |
|
2 | 2 | |
|
3 | 3 | Module of useful filters for processing Latex within Jinja latex templates. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | import re |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Globals and constants |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | MARKDOWN_IMAGE_RE = re.compile(r'!\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)') | |
|
23 | ||
|
22 | 24 | LATEX_RE_SUBS = ( |
|
23 | 25 | (re.compile(r'\.\.\.+'), r'\\ldots'), |
|
24 | 26 | ) |
|
25 | 27 | |
|
26 | 28 | # Latex substitutions for escaping latex. |
|
27 | 29 | # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates |
|
28 | 30 | |
|
29 | 31 | LATEX_SUBS = { |
|
30 | 32 | '&': r'\&', |
|
31 | 33 | '%': r'\%', |
|
32 | 34 | '$': r'\$', |
|
33 | 35 | '#': r'\#', |
|
34 | 36 | '_': r'\_', |
|
35 | 37 | '{': r'\{', |
|
36 | 38 | '}': r'\}', |
|
37 | 39 | '~': r'\textasciitilde{}', |
|
38 | 40 | '^': r'\^{}', |
|
39 | 41 | '\\': r'\textbackslash{}', |
|
40 | 42 | } |
|
41 | 43 | |
|
42 | 44 | |
|
43 | 45 | #----------------------------------------------------------------------------- |
|
44 | 46 | # Functions |
|
45 | 47 | #----------------------------------------------------------------------------- |
|
46 | 48 | |
|
47 |
__all__ = ['escape_latex' |
|
|
49 | __all__ = ['escape_latex', | |
|
50 | 'strip_url_static_file_prefix'] | |
|
48 | 51 | |
|
49 | 52 | def escape_latex(text): |
|
50 | 53 | """ |
|
51 | 54 | Escape characters that may conflict with latex. |
|
52 | 55 | |
|
53 | 56 | Parameters |
|
54 | 57 | ---------- |
|
55 | 58 | text : str |
|
56 | 59 | Text containing characters that may conflict with Latex |
|
57 | 60 | """ |
|
58 | 61 | text = ''.join(LATEX_SUBS.get(c, c) for c in text) |
|
59 | 62 | for pattern, replacement in LATEX_RE_SUBS: |
|
60 | 63 | text = pattern.sub(replacement, text) |
|
61 | 64 | |
|
62 | 65 | return text |
|
63 | 66 | |
|
67 | ||
|
68 | def strip_url_static_file_prefix(text): | |
|
69 | text = MARKDOWN_IMAGE_RE.sub(r'![\1](\2)', text) | |
|
70 | return text | |
|
71 | ||
|
72 |
General Comments 0
You need to be logged in to leave comments.
Login now