[Python-checkins] cpython: Simplify the LaTeX section (only three escapes are needed for alltt)

raymond.hettinger python-checkins at python.org
Mon Jul 23 07:24:40 CEST 2012


http://hg.python.org/cpython/rev/b127046831e2
changeset:   78253:b127046831e2
parent:      78250:2429e760b0bd
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Jul 23 00:24:24 2012 -0500
summary:
  Simplify the LaTeX section (only three escapes are needed for alltt)

files:
  Tools/scripts/highlight.py |  17 ++++++-----------
  1 files changed, 6 insertions(+), 11 deletions(-)


diff --git a/Tools/scripts/highlight.py b/Tools/scripts/highlight.py
--- a/Tools/scripts/highlight.py
+++ b/Tools/scripts/highlight.py
@@ -57,7 +57,7 @@
         if kind:
             text, written = combine_range(lines, written, (srow, scol))
             yield '', text
-            text, written = combine_range(lines, written, (erow, ecol))
+            text, written = tok_str, (erow, ecol)
             yield kind, text
     line_upto_token, written = combine_range(lines, written, (erow, ecol))
     yield '', line_upto_token
@@ -172,15 +172,10 @@
 \end{document}
 '''
 
-def latex_escape(s):
-    'Replace LaTeX special characters with their escaped equivalents'
-    # http://en.wikibooks.org/wiki/LaTeX/Basics#Special_Characters
-    xlat = {
-        '#': r'\#', '$': r'\$', '%': r'\%', '^': r'\textasciicircum{}',
-        '&': r'\&', '_': r'\_', '{': r'\{', '}': r'\}', '~': r'\~{}',
-        '\\': r'\textbackslash{}',
-    }
-    return re.sub(r'[\\#$%^&_{}~]', lambda mo: xlat[mo.group()], s)
+def alltt_escape(s):
+    'Replace backslash and braces with their escaped equivalents'
+    xlat = {'{': r'\{', '}': r'\}', '\\': r'\textbackslash{}'}
+    return re.sub(r'[\\{}]', lambda mo: xlat[mo.group()], s)
 
 def latex_highlight(classified_text, title = 'python',
                     commands = default_latex_commands,
@@ -191,7 +186,7 @@
     for kind, text in classified_text:
         if kind:
             result.append(r'\py%s{' % kind)
-        result.append(latex_escape(text))
+        result.append(alltt_escape(text))
         if kind:
             result.append('}')
     return default_latex_document % dict(title=title, macros=macros, body=''.join(result))

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list