Skip to content

Instantly share code, notes, and snippets.

@taozle
Last active November 20, 2015 07:42
Show Gist options
  • Save taozle/a7c146588cbaad43e31d to your computer and use it in GitHub Desktop.
Save taozle/a7c146588cbaad43e31d to your computer and use it in GitHub Desktop.
Using pygments to highlight source code to html
from pygments import highlight
from pygments.lexers import guess_lexer
from pygments.formatters.html import HtmlFormatter
def highlight_code(src, style='manni'):
"""This function highlight :param:`src` into html format using the style provided.
:param src: The source code to highlight.
:param style: The color style, valid style to see:
from pygments.styles import STYLE_MAP
valid_style = [k for k in STYLE_MAP]
print valid_style
:return: The highlighted code in html format.
"""
opts = {'style': style, 'full': True}
lexer = guess_lexer(src, **opts)
formatter = HtmlFormatter(**opts)
return highlight(src, lexer, formatter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment