All My Brain Where stuff from my brain lands

June 17, 2008

Formatting Source Code with WordPress

Filed under: Miscellaneous — Tags: , , , , — Dennis @ 10:12 am

Sometimes you get used to doing things one way and you forget to take a step back now and then and see if there isn’t something you’re missing. In my case, I was formatting source code on this blog by putting html non-breaking spaces and < codes for angle brackets etc. Ouch. What was I thinking.

Here is a much better way to do it. Use the CodeHighLigher plugin. All you have to do after enabling the plugin, is place your source code in a pre tag. Add the lang attribute to the pre tag and you’ll end up with code formatted in a number of languages. The usage section of the plugin document has all the details and languages. I’ll not add that again here. Here is an example though:

int main(int argc, char* argv[]) {
 cout << "Hello Code Formatter Plugin" << endl;
< /pre>

You can include code that would otherwise be formatted by the plugin by putting a space between the end angle bracket and the forward slash for the pre tag. The plugin then outputs the ending pre tag correctly for formatting.

Here is the above code formatted by the plugin:

int main(int argc, char* argv[]) {
 cout << "Hello Code Formatter Plugin" << endl;

Again, I'm not sure why I didn't find this sooner.

1 Comment »

  1. Nice post..
    Also found a nice eclipse plugin to directly copy code as HTML from eclipse IDE.

    Comment by Maverik — December 3, 2012 @ 9:12 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

%d bloggers like this: