diff options
author | gbrandl <gbrandl@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-07-08 06:50:46 +0000 |
---|---|---|
committer | gbrandl <gbrandl@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-07-08 06:50:46 +0000 |
commit | ef66fdd7dd1b83397cf9c09b2fec678c3cac3140 (patch) | |
tree | 961d70ea50f2b98c2f5cf5d71f7e6c8197044bfe /sandbox/py-rest-doc/sphinx/highlighting.py | |
parent | f46ef2418216c95d0764b8907d177e3a569ddc21 (diff) | |
download | docutils-ef66fdd7dd1b83397cf9c09b2fec678c3cac3140.tar.gz |
Fix the HTML help formatter.
Disable highlighting if no Pygments installed.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5325 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/py-rest-doc/sphinx/highlighting.py')
-rw-r--r-- | sandbox/py-rest-doc/sphinx/highlighting.py | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/sandbox/py-rest-doc/sphinx/highlighting.py b/sandbox/py-rest-doc/sphinx/highlighting.py index 481808815..027d92849 100644 --- a/sandbox/py-rest-doc/sphinx/highlighting.py +++ b/sandbox/py-rest-doc/sphinx/highlighting.py @@ -9,46 +9,51 @@ :license: Python license. """ +import cgi from collections import defaultdict -from pygments import highlight -from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \ - TextLexer, RstLexer -from pygments.formatters import HtmlFormatter -from pygments.filters import ErrorToken -from pygments.style import Style -from pygments.styles.friendly import FriendlyStyle -from pygments.token import Generic, Comment +try: + from pygments import highlight + from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \ + TextLexer, RstLexer + from pygments.formatters import HtmlFormatter + from pygments.filters import ErrorToken + from pygments.style import Style + from pygments.styles.friendly import FriendlyStyle + from pygments.token import Generic, Comment +except ImportError: + pygments = None +else: + class PythonDocStyle(Style): + """ + Like friendly, but a bit darker to enhance contrast on the green background. + """ + background_color = '#eeffcc' + default_style = '' -class PythonDocStyle(Style): - """ - Like friendly, but a bit darker to enhance contrast on the green background. - """ + styles = FriendlyStyle.styles + styles.update({ + Generic.Output: 'italic #333', + Comment: 'italic #408090', + }) - background_color = '#eeffcc' - default_style = '' + lexers = defaultdict(TextLexer, + none = TextLexer(), + python = PythonLexer(), + pycon = PythonConsoleLexer(), + rest = RstLexer(), + c = CLexer(), + ) + for _lexer in lexers.values(): + _lexer.add_filter('raiseonerror') - styles = FriendlyStyle.styles - styles.update({ - Generic.Output: 'italic #333', - Comment: 'italic #408090', - }) - -lexers = defaultdict(TextLexer, - none = TextLexer(), - python = PythonLexer(), - pycon = PythonConsoleLexer(), - rest = RstLexer(), - c = CLexer(), -) -for _lexer in lexers.values(): - _lexer.add_filter('raiseonerror') - -fmter = HtmlFormatter(style=PythonDocStyle) + fmter = HtmlFormatter(style=PythonDocStyle) def highlight_block(source, lang): + if not pygments: + return '<pre>' + cgi.escape(source) + '</pre>\n' if lang == 'python': if source.startswith('>>>'): lexer = lexers['pycon'] @@ -60,7 +65,7 @@ def highlight_block(source, lang): return highlight(source, lexer, fmter) except ErrorToken: # this is most probably not Python, so let it pass textonly - return '<pre>' + source + '</pre>\n' + return '<pre>' + cgi.escape(source) + '</pre>\n' def get_stylesheet(): return fmter.get_style_defs() |