Sunday, February 8, 2009

ac2html: Syntax highlight your blog posts

If you are a developer and you post your code on blogger (or other blogs) then you are gonna love this small tool which I call ac2html (any code to html).

ac2html takes your code and highlights it using html thus allowing you to generate a syntax highlighted version of your code, which you can post on blogger or any place where html is accepted.

Sample run

========

Download ac2html from here. The code is platform independent and should run on all platforms (but I have only tested it on linux x86)

Untar it

tar -xvzf AnyCode2html.tar.gz

Compile it

cd AnyCode2html

make

Note that you might need to edit the Makefile to suit your system.

Now write some c++ code or pick an existing one. I am going to use a sample c++ file which has the following contents (copied and pasted from my source file)

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
// print a test message
cout<<endl<<"Hello world"<<endl;
return 0;
}

Ugly isn't it? Now run ac2html

./ac2html cpp_keywords cpp_code.cpp output.html

Before we show the results lets analyze the arguments that we just passed. All the arguments that we passed are required by ac2html.

  • cpp_keywords is the keywords file which ac2html uses to identify keywords.
  • cpp_code.cpp is the file that contains my c++ code
  • output.html is the file which the results should be saved

Now copy the the contents of output.html and paste it to the blogger's compose window. Publish your post and you will get this:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 // print a test message
cout<<endl<<"Hello world"<<endl;
 return 0;
}

Beautiful, isn't it :-)

ACKNOWLEDGEMENTS

================

I got this idea from Adrian's post at http://codecentrix.blogspot.com/2007/05/blogs-and-c-syntax-highlighting.html. However his tool was too windows and c++ centric, hence I decided to rewrite a generic one.

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!