Convert C++ Code to HTML

  As a blogger focusing on computer technologies, from time to time, I need to paste some programming code(most often, c/c++) to web. But very few blog service provider supports important features for code presentation, such as programming language syntax highlight, smart indention etc. So we have to convert the code to html by ourselves.

  Here, I collected some tips to help you do the convention:

  1. Use online converter.
  http://www.bedaux.net/cpp2html/ is a great online cpp->html converter. It's also highly customizable - you can choose the color scheme of the final html code, just by changing some variable values. But it uses CSS, so you must embed those CSS statements into your own blog template to make the final html code works in your blog pages.

  2. Use offline editor.
  Some powerful editors have build-in features to convert code into html format. For example, the vim editor. To do the convention, first, copy your code to VIM or use VIM to open your code file, make sure the syntax highlight is enabled(vim command: syntax enable), then run this command: "source $VIMRUNTIME/syntax/2html.vim". The html version of your original code will be opened in a new vim window. It's pure html code, no CSS used. So it's very handy to copy and paste them to your blog articles.

  If you don't like the geek style editor, you can use an UI friendly editor: notepad++. It also has an extension to convert in-editing code into html format. Suppose you are editing some source code file, from menu "plugin->export", choose "export as html".

  For other languages such as Java, C#, there are many similar tools such as java2html and csharp2html. I also found a good add-on called CopySourceAsHtml(CSAH) for Visual Studio 2008, it can convert code into html format easily.

  UPDATE @ 11/28/2008
1. You can use "TOhtml"(Be careful about the case) as shortcut for "source $VIMRUNTIME/syntax/2html.vim" in VIM;
2. 2html only works in VIM GUI version(gvim), the console version will get wrong final color shceme.
3. For this tool, you'd better configurate it with "let html_no_pre = 1" and "let html_number_lines = 1"(more info:help tohtml);
4. There is another great tool called HighLight, which can convert syntax highlighted code to html version.