Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3f63d3

Browse files
committed
Simplify the LaTeX section (only three escapes are needed for alltt)
1 parent 392bfd3 commit d3f63d3

1 file changed

Lines changed: 6 additions & 11 deletions

File tree

Tools/scripts/highlight.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def analyze_python(source):
5757
if kind:
5858
text, written = combine_range(lines, written, (srow, scol))
5959
yield '', text
60-
text, written = combine_range(lines, written, (erow, ecol))
60+
text, written = tok_str, (erow, ecol)
6161
yield kind, text
6262
line_upto_token, written = combine_range(lines, written, (erow, ecol))
6363
yield '', line_upto_token
@@ -172,15 +172,10 @@ def build_html_page(classified_text, title='python',
172172
\end{document}
173173
'''
174174

175-
def latex_escape(s):
176-
'Replace LaTeX special characters with their escaped equivalents'
177-
# http://en.wikibooks.org/wiki/LaTeX/Basics#Special_Characters
178-
xlat = {
179-
'#': r'\#', '$': r'\$', '%': r'\%', '^': r'\textasciicircum{}',
180-
'&': r'\&', '_': r'\_', '{': r'\{', '}': r'\}', '~': r'\~{}',
181-
'\\': r'\textbackslash{}',
182-
}
183-
return re.sub(r'[\\#$%^&_{}~]', lambda mo: xlat[mo.group()], s)
175+
def alltt_escape(s):
176+
'Replace backslash and braces with their escaped equivalents'
177+
xlat = {'{': r'\{', '}': r'\}', '\\': r'\textbackslash{}'}
178+
return re.sub(r'[\\{}]', lambda mo: xlat[mo.group()], s)
184179

185180
def latex_highlight(classified_text, title = 'python',
186181
commands = default_latex_commands,
@@ -191,7 +186,7 @@ def latex_highlight(classified_text, title = 'python',
191186
for kind, text in classified_text:
192187
if kind:
193188
result.append(r'\py%s{' % kind)
194-
result.append(latex_escape(text))
189+
result.append(alltt_escape(text))
195190
if kind:
196191
result.append('}')
197192
return default_latex_document % dict(title=title, macros=macros, body=''.join(result))

0 commit comments

Comments
 (0)