Friday, July 10, 2009

Syntax Highlighting - quick and easy

Edit: I recently made my life easier by switching over to http://code.google.com/p/google-code-prettify/ for syntax highlighting.

Since this blog is mainly about Python examples, I needed a way to post syntax highlighted Python code. There were a few solutions, including a hosted javascript option. I didn't like that one too much for a few reasons. After short struggle, I was able to install Pygments on my computer which is a Python module that can apply syntax highlighting to many different programming languages' source. I wanted a quick and easy way of writing Python code in my favorite text editor, Notepad++, and getting it up on this blog. I thought it would be neat to create a syntax_highlighter.py script, and use the NppExec plug-in to run the syntax highlighter on the current document, and then paste the results on the clipboard. To do that last part, I had to download and install the pywin32 library (you can find that on sourceforge). Here is my final script:


from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import win32clipboard
import sys,re

# Replace tab characters with 4 spaces for better look on webpage
def replaceTabs(matchobj):
return " "

# Open up and store the souce file contents
rawCode = open(sys.argv[1], "r").read()

# Apply syntax highlighting
highlightedCode = highlight(rawCode, PythonLexer(), HtmlFormatter())

# Replace tabs with spaces
finalCode = re.sub("\t", replaceTabs, highlightedCode)

win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(finalCode)
win32clipboard.CloseClipboard()


This will put on my clipboard code that has been surrounded with the appropriate html tags to make it appear nicely formatted and highlighted. I created another function to replace all tabs with 4 spaces to make it look better (I found it easier to do that with the regular expression module).

If you use Notepad++, I used this to run the script on the current open file:
python "FULL_PATH_TO_YOUR_HIGHLIGHTING_SCRIPT" "$(FULL_CURRENT_PATH)"
The first argument should be the full path to the syntax_highlight.py script - the next ,"$(FULL_CURRENT_PATH)", is a standard variable that the NppExec plug-in uses to define the current open file. Once run, the result will be on your clipboard ready to be pasted. Once set up, having it ready to paste on this blog is a CTRL-F6 keystroke away. This could work for any language that Pygments supports.

The clipboard portion of the script was a lot easier than I thought that it would be. The pywin32 library has many useful features in it - I know that similar solutions exist for Linux.

NOTE: I ran

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

print HtmlFormatter().get_style_defs('.highlight')


to get my CSS rules, which I was able to paste in my template.
blog comments powered by Disqus