##// END OF EJS Templates
Add strip_url_static_file_prefix to filters
Peter Davis -
Show More
@@ -1,63 +1,72 b''
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 MARKDOWN_IMAGE_RE = re.compile(r'!\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)')
23
22 LATEX_RE_SUBS = (
24 LATEX_RE_SUBS = (
23 (re.compile(r'\.\.\.+'), r'\\ldots'),
25 (re.compile(r'\.\.\.+'), r'\\ldots'),
24 )
26 )
25
27
26 # Latex substitutions for escaping latex.
28 # Latex substitutions for escaping latex.
27 # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
29 # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
28
30
29 LATEX_SUBS = {
31 LATEX_SUBS = {
30 '&': r'\&',
32 '&': r'\&',
31 '%': r'\%',
33 '%': r'\%',
32 '$': r'\$',
34 '$': r'\$',
33 '#': r'\#',
35 '#': r'\#',
34 '_': r'\_',
36 '_': r'\_',
35 '{': r'\{',
37 '{': r'\{',
36 '}': r'\}',
38 '}': r'\}',
37 '~': r'\textasciitilde{}',
39 '~': r'\textasciitilde{}',
38 '^': r'\^{}',
40 '^': r'\^{}',
39 '\\': r'\textbackslash{}',
41 '\\': r'\textbackslash{}',
40 }
42 }
41
43
42
44
43 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
44 # Functions
46 # Functions
45 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
46
48
47 __all__ = ['escape_latex']
49 __all__ = ['escape_latex',
50 'strip_url_static_file_prefix']
48
51
49 def escape_latex(text):
52 def escape_latex(text):
50 """
53 """
51 Escape characters that may conflict with latex.
54 Escape characters that may conflict with latex.
52
55
53 Parameters
56 Parameters
54 ----------
57 ----------
55 text : str
58 text : str
56 Text containing characters that may conflict with Latex
59 Text containing characters that may conflict with Latex
57 """
60 """
58 text = ''.join(LATEX_SUBS.get(c, c) for c in text)
61 text = ''.join(LATEX_SUBS.get(c, c) for c in text)
59 for pattern, replacement in LATEX_RE_SUBS:
62 for pattern, replacement in LATEX_RE_SUBS:
60 text = pattern.sub(replacement, text)
63 text = pattern.sub(replacement, text)
61
64
62 return text
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