Syntax Highlighter using Pygments and Python

I was using this web site to highlight and format my SQL code to HTML to insert into our wiki at work on Sharepoint, when all of a sudden the website stopped working. After some searching I discovered the site was using Pygments, a Syntax Highlighting Library for Python. So I decided to try and roll my own solution. This article will detail the steps I went through to make it happen.



  • Installed Python 2.7 with the defaults which creates a folder called C:\Python27
  • Installed Easy Setup using the EXE taking the defaults
  • Installed Pygments by hitting a console window and running this from the C:\Python27\Scripts folder. easy_install Pygments, more info here:



  • First I setup a basic HTML page with a simple form.The page had a single form with a TEXTAREA named code.
    <form method='post' action=''>
    <TEXTAREA cols=100 rows=25 name='code'></TEXTAREA>
    <select name='type'>
    <option value='SqlLexer'>SQL</option>
    <option value='AutoHotkeyLexer'>Autohotkey</option>
    <option value='BatchLexer'>Batch</option>
    <option value='PowerShellLexer'>PowerShell</option>
    <option value='CSharpLexer'>CSharp</option>
    <option value='VbNetLexer'>VbNet</option>
    <input type='submit'>
  • Second I pieced a Python file together using code snippets from the Pygments web site, StackOvervlow, and Python help files.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cgi
# enable debugging
import cgitb

#print "Content-Type: text/plain;charset=utf-8"

from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from pygments.styles import get_all_styles

form = cgi.FieldStorage()
code = form["code"].value

#code = 'SELECT * from dbo.Customers'
print "<HTML>"
print "<TEXTAREA cols=100 rows=20>"
print "<div style='border-width: 1px 1px 1px 8px; border-style: solid; border-color: #555; padding-left: 10px;'>"
print highlight(code, SqlLexer(), HtmlFormatter(noclasses=True,style='default'))
print "</div>"
print "</TEXTAREA>"
print "</HTML>"
#print HtmlFormatter().get_style_defs('.highlight')

print highlight(code,SqlLexer(),HtmlFormatter(noclasses=True,style='default'))

#print list(get_all_styles())
  • SqlLexer is hardcoded here meaning that we can only convert SQL code with this page.
  • Working on allowing for other languages
  • Pygments Formatter has several options, this code is only using the noclasses and style options.
  • There are several builtin Styles and you can create your own styles.

Next Steps

  • Allow for different types of code
  • Create a custom style (don’t like the default background color)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s