-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
31 lines (27 loc) · 991 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pprint
import uuid
import six
try:
import pygments
except ImportError:
pygments = None
else:
import pygments.lexers
import pygments.formatters
from IPython.display import HTML
def highlight(code, lexer='html', formatter='html', output_wrapper=None):
"""Highlights given code using pygments."""
if not pygments:
raise TypeError("pygments module required")
if not isinstance(code, six.string_types):
code = pprint.pformat(code)
if isinstance(lexer, six.string_types):
lexer = pygments.lexers.get_lexer_by_name(lexer)
if isinstance(formatter, six.string_types):
formatter = pygments.formatters.get_formatter_by_name(formatter)
if formatter.name.lower() == 'html':
formatter.full = True
formatter.cssclass = "pygments-%s" % uuid.uuid4()
if output_wrapper is None:
output_wrapper = HTML
return output_wrapper(pygments.highlight(code, lexer, formatter))