Skip to content

Instantly share code, notes, and snippets.

@akarsh1995
Created August 31, 2020 17:17
Show Gist options
  • Select an option

  • Save akarsh1995/5a07ddb217dcab56c9fdb850e66dca25 to your computer and use it in GitHub Desktop.

Select an option

Save akarsh1995/5a07ddb217dcab56c9fdb850e66dca25 to your computer and use it in GitHub Desktop.
Use this syntax highlighter to highlight syntax of almost any language.
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
code = '''helloworld_string = 'Hello! World'
print f"{helloworld_string}"'''
template = '''<html>
<head>
<title>How to use Pygments</title>
<style type="text/css">
{}
</style>
</head>
<body>
{}
</body>
</html>
'''
if __name__ == '__main__':
formatted_html = highlight(code, PythonLexer(), HtmlFormatter())
css_style = HtmlFormatter().get_style_defs('.highlight')
print(template.format(css_style, formatted_html))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment