Last active
November 20, 2015 07:42
-
-
Save taozle/a7c146588cbaad43e31d to your computer and use it in GitHub Desktop.
Using pygments to highlight source code to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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