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!

12 comments:

Anonymous said...

hey, u have done a pretty useful utility. Thanks for it ...
but one nuisciance is that it purges all the new lines from the code. Can u please remedy it ...

CodeCentrix said...

Can you give me a short example of what is going wrong? Thanks!

Sid said...
This comment has been removed by the author.
Sid said...

Awesomo 5000. Very useful for C++ coders who post code on their blogs. (like me)

Karl said...

Thank you!!

CodeCentrix said...

You are welcome!

Anonymous said...

Please post the .y yacc file to the world

CodeCentrix said...

Unfortunately there is no .y yacc file to be posted. The program only uses Lex and not Yacc.

Anonymous said...

You have really great taste on catch article titles, even when you are not interested in this topic you push to read it

Anonymous said...

Well your article helped me altogether much in my college assignment. Hats off to you send, will look progressive in the direction of more cognate articles soon as its united of my choice topic to read.

Rachel said...

thanks for an awesome program :) can i modify this?

Rachel said...
This comment has been removed by the author.