##// END OF EJS Templates
Merge pull request #5948 from joergdietrich/patch-1...
Jonathan Frederic -
r16975:d045c0ad merge
parent child Browse files
Show More
@@ -1,63 +1,63 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 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
@@ -1,43 +1,43 b''
1 """
1 """
2 Module with tests for Latex
2 Module with tests for Latex
3 """
3 """
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
16
17 from ...tests.base import TestsBase
17 from ...tests.base import TestsBase
18 from ..latex import escape_latex
18 from ..latex import escape_latex
19
19
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestLatex(TestsBase):
25 class TestLatex(TestsBase):
26
26
27
27
28 def test_escape_latex(self):
28 def test_escape_latex(self):
29 """escape_latex test"""
29 """escape_latex test"""
30 tests = [
30 tests = [
31 (r'How are \you doing today?', r'How are \textbackslash{}you doing today?'),
31 (r'How are \you doing today?', r'How are \textbackslash{}you doing today?'),
32 (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\textbackslash{}escapechar=`\textbackslash{}A\textbackslash{}catcode`\textbackslash{}|=0 |string|foo'),
32 (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\textbackslash{}escapechar=`\textbackslash{}A\textbackslash{}catcode`\textbackslash{}|=0 |string|foo'),
33 (r'# $ % & ~ _ ^ \ { }', r'\# \$ \% \& \textasciitilde{} \_ \^{} \textbackslash{} \{ \}'),
33 (r'# $ % & ~ _ ^ \ { }', r'\# \$ \% \& \textasciitilde{} \_ \^{} \textbackslash{} \{ \}'),
34 ('...', r'\ldots'),
34 ('...', r'{\ldots}'),
35 ('','')]
35 ('','')]
36
36
37 for test in tests:
37 for test in tests:
38 self._try_escape_latex(test[0], test[1])
38 self._try_escape_latex(test[0], test[1])
39
39
40
40
41 def _try_escape_latex(self, test, result):
41 def _try_escape_latex(self, test, result):
42 """Try to remove latex from string"""
42 """Try to remove latex from string"""
43 self.assertEqual(escape_latex(test), result)
43 self.assertEqual(escape_latex(test), result)
General Comments 0
You need to be logged in to leave comments. Login now