Thursday, February 19, 2009

Source Code Highlighting - In Blogger!

There are a great many posts on how to post syntax highlighted code in blogger. I can't really understand why google doesn't have a ready made solution in place.

I think I've found a solution I'm comfortable with:
  1. Convert the code to html (":TOhtml" in vi)
  2. In the Compose tab, type: <pre> </pre>
  3. Paste you code between the tags
  4. Publish
That's it - no installation, not too much hassle.

For extra credit, you can modify the css for <pre> sections.

If you want line numbers:
  1. ":set nu" - show line numbers in vi
  2. ":TOhtml" - convert to html
  3. ":set nonu" - remove line numbers for easy copy & paste (or, use Ctrl-V)
1  #include <stdio.h>
2
3 int main( int argc, char *argv[] ) {
4 printf("Hello World!\n");
5 return 0;
6 }
All that's left is to find a good color scheme for vi.

edit: Ars Pythonica has added that in your .vimrc file, you can add: "let html_use_css = 1" to include css in the output.

Thanks to:
  1. Eli Bendersky - for the pre trick
  2. FluidBlog - For the vi TOHtml trick

5 comments:

  1. Good. That's what I'm looking for!

    ReplyDelete
  2. Actually the one used on my blog post (you linked) is from Google :)
    I like the fact that it is a general-language highlighter without the need to specify the language explicitly.

    Cheers.

    ReplyDelete