Sunday, May 27, 2007

Blogs and C++ syntax highlighting

This wasn't supposed to be my first post. I wanted to start my blogging existence writing about something else. It proved that being a blogger is not as easy as it should be, especially when trying to post some C++ code. The online editor allows to directly modify the HTML code but this is not the simplest or most enjoyable thing to do. Because I wanted C++ syntax highlighting so hard I decided to create a little tool to automate this task as much as possible and to allow me to focus on the real subject.

The result is cpptohtml.exe that takes a
.cpp file as a command line argument and generates a .html file. If no file name is provided as argument, a standard "file open" dialog allows the user to specify the source .cpp file. The tool also accepts /C as command line arguments. When used the source code is taken from clipboard and the HTML result is put also in clipboard.

I found creating a windows shortcut to cpptohtml.exe very useful. This way I can launch the tool by pressing a shortcut key combination (CTR+ALT+T).
I'm gonna use this tool like this:
  • open preferred IDE and copy the C++ source code into the clipboard.
  • launch the tool using the shortcut key combination. The HTML result is in the clipboard.
  • paste the formated HTML in the online blog editor.
Download:
You can find here the full source code and the executable. I used Visual C++ and LEX. The archive also contains a .js script to automate the shortcut creation.
I am pretty happy with the final result and how C++, LEX and JScript combines to automate a boring repetitive task.

Beautiful the technology is!