diff options
author | Soheil Hassas Yeganeh <soheil@cs.toronto.edu> | 2014-01-08 17:04:39 -0500 |
---|---|---|
committer | Behdad Esfahbod <behdad@behdad.org> | 2014-01-10 16:08:51 +0800 |
commit | 92820dcf113bf496d6a7d1e298514041e9d39e54 (patch) | |
tree | b873f9ddfd06747227ae895ef21540d9b0ca6dc8 | |
parent | 5c26f27b2ae6d3e0c3f5103cb5b847442baa6b9a (diff) |
Puts escaping inside highlight()
Details:
* Escaping & and < is now inside highlight() for convenience.
-rwxr-xr-x | glyphy/glyphy_slides.py | 5 | ||||
-rwxr-xr-x | pangopygments.py | 16 |
2 files changed, 11 insertions, 10 deletions
diff --git a/glyphy/glyphy_slides.py b/glyphy/glyphy_slides.py index 7f1d4e0..f137ce1 100755 --- a/glyphy/glyphy_slides.py +++ b/glyphy/glyphy_slides.py @@ -420,12 +420,7 @@ list_slide ([ ], data={'align': pango.ALIGN_LEFT}) def source_slide(s): - # The highlighter highlights (i.e., adds tags around) operators - # (& and ;, here), so let's use a non-highlighted keyword, and escape them - # after highlighting. - s = s.replace("&", "__AMP__").replace("<", "__LT__") s = highlight(s, 'c') - s = s.replace("__AMP__", "&").replace("__LT__", "<") s = "<span font_desc='monospace'>" + s + "</span>" slide_noone (s, data={'align': pango.ALIGN_LEFT}) diff --git a/pangopygments.py b/pangopygments.py index 05685a7..4e2354b 100755 --- a/pangopygments.py +++ b/pangopygments.py @@ -73,9 +73,10 @@ __LEXERS = { def highlight(snippet, lang): ''' snippet is the string of code snippets, and lang, the language name. ''' - if not __LEXERS.get(lang): - print('Language %s is not supported.' % lang) - return snippet + # The highlighter highlights (i.e., adds tags around) operators + # (& and ;, here), so let's use a non-highlighted keyword, and escape them + # after highlighting. + snippet = snippet.replace("&", "__AMP__").replace("<", "__LT__") # Pygments messes up initial and final newlines; fix up begin = '' @@ -87,16 +88,21 @@ def highlight(snippet, lang): end = '\n' snippet = snippet[:-1] - snippet = pygments.highlight(snippet, __LEXERS[lang](), PangoFormatter()) + if __LEXERS.get(lang): + snippet = pygments.highlight(snippet, __LEXERS[lang](), PangoFormatter()) + else: + print("Language %s is not supported." % lang) if snippet[0] == '\n' and start == '\n': start = '' if snippet[-1] == '\n' and end == '\n': end = '' + snippet = snippet.replace("__AMP__", "&").replace("__LT__", "<") + return begin + snippet + end if __name__ == '__main__': code = 'print "Hello World"' - print highlight(code, 'python') + print highlight(code, 'py') |