OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import sys | 5 import sys |
6 try: | 6 try: |
7 import pygments | 7 import pygments |
8 from pygments.lexers import CppLexer | 8 from pygments.lexers import CppLexer |
9 from pygments.formatters import HtmlFormatter | 9 from pygments.formatters import HtmlFormatter |
10 PYGMENTS_IMPORTED = True | 10 PYGMENTS_IMPORTED = True |
11 except ImportError: | 11 except ImportError: |
12 print('It appears that Pygments is not installed. ' | 12 print('It appears that Pygments is not installed. ' |
13 'Can be installed using easy_install Pygments or from http://pygments.org.') | 13 'Can be installed using easy_install Pygments or from http://pygments.org.') |
14 PYGMENTS_IMPORTED = False | 14 PYGMENTS_IMPORTED = False |
15 | 15 |
16 class PygmentsHighlighter(object): | 16 class PygmentsHighlighter(object): |
| 17 def __init__(self): |
| 18 if not PYGMENTS_IMPORTED: |
| 19 raise ImportError('Pygments not installed') |
| 20 |
17 """Highlighter that uses the python pygments library to highlight code. | 21 """Highlighter that uses the python pygments library to highlight code. |
18 """ | 22 """ |
19 def GetCSS(self, style): | 23 def GetCSS(self, style): |
20 if PYGMENTS_IMPORTED: | 24 formatter = HtmlFormatter(linenos=True, |
21 formatter = HtmlFormatter(linenos=True, | 25 style=pygments.styles.get_style_by_name(style)) |
22 style=pygments.styles.get_style_by_name(style)) | 26 return formatter.get_style_defs('.highlight') |
23 return formatter.get_style_defs('.highlight') | |
24 | 27 |
25 def GetCodeElement(self, code, style): | 28 def GetCodeElement(self, code, style): |
26 if PYGMENTS_IMPORTED: | 29 formatter = HtmlFormatter(linenos=True, |
27 formatter = HtmlFormatter(linenos=True, | 30 style=pygments.styles.get_style_by_name(style)) |
28 style=pygments.styles.get_style_by_name(style)) | 31 return pygments.highlight(code, CppLexer(), formatter) |
29 return pygments.highlight(code, CppLexer(), formatter) | |
30 else: | |
31 return '<pre>Pygments highlighter not installed</pre>' | |
32 | 32 |
33 def DisplayName(self): | 33 def DisplayName(self): |
34 return 'pygments' + ('' if PYGMENTS_IMPORTED else ' (not installed)') | 34 return 'pygments' + ('' if PYGMENTS_IMPORTED else ' (not installed)') |
35 | 35 |
36 def GetStyles(self): | 36 def GetStyles(self): |
37 return list(pygments.styles.get_all_styles()) | 37 return list(pygments.styles.get_all_styles()) |
OLD | NEW |