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