As both a programmer and blogger, one needs to publish source code on blogs frequently. So how to do it? As a newcomer to blogger community, I looked into the problem for a while. There are two issues related to this if you only want to display your code plainly, without syntax highlighting:
- HTML treats consecutive spaces, tabs, line breaks as a single space, which removes indentations of the code.
- Special characters like &, <, >, and " need escaping, especially for HTML code itself.
Following is the tool to convert source code to HTML fragments to be inserted in blogs or normal web pages. Simply paste your source code into the 1st textarea, and copy the HTML code out from the 2nd textarea.
It should be noted that I use CSS to format the source code in <pre> element, therefore one attribute "code" appears in the 1st line of the HTML output. One may tweak it according to his/her own preference, e.g. change to inline style sheets.
The source code for above form and related script is shown below formatted with itself.
We use HTML element <pre> to solve the 1st issue listed
above. In the script, function
formatCode escapes special
characters; note that we should handle character & first.
Update (2008-1-17): for Emacs users, you may find htmlize useful for this task (with many cool features). Gentoo users can simple emerge htmlize. I just stumbled upon this cool extension when reading the comments for Steve Yegge's nice post.