diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-26 02:22:18 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-26 02:22:18 +0000 |
commit | 4d14d9c53ff54dd3bfa56dd319cc45123d92e0bd (patch) | |
tree | 92057ce6cb3d201794dfbd24d0039c50d549f9fd /tools/json_schema_compiler | |
parent | aaa2a5087707dcc03edd7796c42adc9f6c620bc7 (diff) | |
download | chromium_src-4d14d9c53ff54dd3bfa56dd319cc45123d92e0bd.zip chromium_src-4d14d9c53ff54dd3bfa56dd319cc45123d92e0bd.tar.gz chromium_src-4d14d9c53ff54dd3bfa56dd319cc45123d92e0bd.tar.bz2 |
Fix syntax highlighting in JSON Schema Compiler's preview server: don't crash
if Pygments isn't installed, and escape HTML (especially obvious with generics)
when syntax highlighting is turned off.
Review URL: https://chromiumcodereview.appspot.com/10833024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148482 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
3 files changed, 26 insertions, 17 deletions
diff --git a/tools/json_schema_compiler/highlighters/none_highlighter.py b/tools/json_schema_compiler/highlighters/none_highlighter.py index 873a00b..ac1cc2b 100644 --- a/tools/json_schema_compiler/highlighters/none_highlighter.py +++ b/tools/json_schema_compiler/highlighters/none_highlighter.py @@ -2,6 +2,8 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import cgi + class NoneHighlighter(object): """Highlighter that just wraps code in a <pre>. """ @@ -9,7 +11,7 @@ class NoneHighlighter(object): return '' def GetCodeElement(self, code, style): - return '<pre>' + code + '</pre>' + return '<pre>' + cgi.escape(code) + '</pre>' def DisplayName(self): return 'none' diff --git a/tools/json_schema_compiler/highlighters/pygments_highlighter.py b/tools/json_schema_compiler/highlighters/pygments_highlighter.py index 569fcc5..06abd33 100644 --- a/tools/json_schema_compiler/highlighters/pygments_highlighter.py +++ b/tools/json_schema_compiler/highlighters/pygments_highlighter.py @@ -14,21 +14,21 @@ except ImportError: PYGMENTS_IMPORTED = False class PygmentsHighlighter(object): + def __init__(self): + if not PYGMENTS_IMPORTED: + raise ImportError('Pygments not installed') + """Highlighter that uses the python pygments library to highlight code. """ def GetCSS(self, style): - if PYGMENTS_IMPORTED: - formatter = HtmlFormatter(linenos=True, - style=pygments.styles.get_style_by_name(style)) - return formatter.get_style_defs('.highlight') + formatter = HtmlFormatter(linenos=True, + style=pygments.styles.get_style_by_name(style)) + return formatter.get_style_defs('.highlight') def GetCodeElement(self, code, style): - if PYGMENTS_IMPORTED: - formatter = HtmlFormatter(linenos=True, - style=pygments.styles.get_style_by_name(style)) - return pygments.highlight(code, CppLexer(), formatter) - else: - return '<pre>Pygments highlighter not installed</pre>' + formatter = HtmlFormatter(linenos=True, + style=pygments.styles.get_style_by_name(style)) + return pygments.highlight(code, CppLexer(), formatter) def DisplayName(self): return 'pygments' + ('' if PYGMENTS_IMPORTED else ' (not installed)') diff --git a/tools/json_schema_compiler/previewserver.py b/tools/json_schema_compiler/previewserver.py index 2d4105a..b965469a 100755 --- a/tools/json_schema_compiler/previewserver.py +++ b/tools/json_schema_compiler/previewserver.py @@ -321,12 +321,19 @@ if __name__ == '__main__': print('') print(' http://localhost:%d/chrome/common/extensions/api' % opts.port) print('') - server = PreviewHTTPServer(('', int(opts.port)), CompilerHandler, - { - 'pygments': pygments_highlighter.PygmentsHighlighter(), - 'hilite': hilite_me_highlighter.HiliteMeHighlighter(), - 'none': none_highlighter.NoneHighlighter(), - }) + + highlighters = { + 'hilite': hilite_me_highlighter.HiliteMeHighlighter(), + 'none': none_highlighter.NoneHighlighter() + } + try: + highlighters['pygments'] = pygments_highlighter.PygmentsHighlighter() + except ImportError as e: + pass + + server = PreviewHTTPServer(('', int(opts.port)), + CompilerHandler, + highlighters) server.serve_forever() except KeyboardInterrupt: server.socket.close() |