Show More
@@ -20,25 +20,26 import re | |||
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | #Latex substitutions for escaping latex. |
|
23 | LATEX_SUBS = ( | |
|
24 | (re.compile('\033\[[0-9;]+m'),''), # handle console escapes | |
|
25 | (re.compile(r'\\'), r'{\\textbackslash}'), | |
|
26 | (re.compile(r'([{}_#%&$])'), r'\\\1'), | |
|
27 | (re.compile(r'~'), r'\~{}'), | |
|
28 | (re.compile(r'\^'), r'\^{}'), | |
|
29 | (re.compile(r'"'), r"''"), | |
|
30 | (re.compile(r'\.\.\.+'), r'\\ldots'), | |
|
31 | ) | |
|
23 | # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates | |
|
24 | LATEX_SUBS = { | |
|
25 | '&': r'\&', | |
|
26 | '%': r'\%', | |
|
27 | '$': r'\$', | |
|
28 | '#': r'\#', | |
|
29 | '_': r'\letterunderscore{}', | |
|
30 | '{': r'\letteropenbrace{}', | |
|
31 | '}': r'\letterclosebrace{}', | |
|
32 | '~': r'\lettertilde{}', | |
|
33 | '^': r'\letterhat{}', | |
|
34 | '\\': r'\letterbackslash{}'} | |
|
35 | ||
|
32 | 36 | |
|
33 | 37 | #----------------------------------------------------------------------------- |
|
34 | 38 | # Functions |
|
35 | 39 | #----------------------------------------------------------------------------- |
|
36 | 40 | |
|
37 | __all__ = [ | |
|
38 | 'escape_latex', | |
|
39 | 'strip_math_space' | |
|
40 | ] | |
|
41 | ||
|
41 | __all__ = ['escape_latex', | |
|
42 | 'strip_math_space'] | |
|
42 | 43 | |
|
43 | 44 | def escape_latex(text): |
|
44 | 45 | """ |
@@ -49,10 +50,8 def escape_latex(text): | |||
|
49 | 50 | text : str |
|
50 | 51 | Text containing characters that may conflict with Latex |
|
51 | 52 | """ |
|
52 | return_text = text | |
|
53 | for pattern, replacement in LATEX_SUBS: | |
|
54 | return_text = pattern.sub(replacement, return_text) | |
|
55 | return return_text | |
|
53 | ||
|
54 | return ''.join([LATEX_SUBS.get(c, c) for c in text]) | |
|
56 | 55 | |
|
57 | 56 | |
|
58 | 57 | def strip_math_space(text): |
@@ -28,9 +28,9 class TestLatex(TestsBase): | |||
|
28 | 28 | def test_escape_latex(self): |
|
29 | 29 | """escape_latex test""" |
|
30 | 30 | tests = [ |
|
31 |
(r'How are \you doing today?', r'How are \ |
|
|
32 |
(r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\ |
|
|
33 |
(r'# $ % & ~ _ ^ \ { }',r'\# \$ \% \& \ |
|
|
31 | (r'How are \you doing today?', r'How are \letterbackslash{}you doing today?'), | |
|
32 | (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\letterbackslash{}escapechar=`\letterbackslash{}A\letterbackslash{}catcode`\letterbackslash{}|=0 |string|foo'), | |
|
33 | (r'# $ % & ~ _ ^ \ { }', r'\# \$ \% \& \lettertilde{} \letterunderscore{} \letterhat{} \letterbackslash{} \letteropenbrace{} \letterclosebrace{}'), | |
|
34 | 34 | ('','')] |
|
35 | 35 | |
|
36 | 36 | for test in tests: |
General Comments 0
You need to be logged in to leave comments.
Login now