Thursday, 5 September 2013

Cool tip of the day: Formatting source code for your blog

Oh finally!
I have pasted code into my blog & it has been a real pain to get it right.
While looking around I found a resource on blogspot http://formatmysourcecode.blogspot.co.uk/ which provides an online utility to format the script into html to include on your blog.
It has two outputs, one with inline CSS and the other with a set of styles used.
The "linked style sheet" option depends on a PRE.source-code style.

An example of this formatting is as follows:

/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

A second resource can be found at http://codeformatter.blogspot.co.uk/ which has similar options  but allows you to define whether you have line numbers on your code. When you don't use embedded styles it creates a style element which defines two classes pre.CICodeFormatter and code.CICodeFormatter.

An example of this output is as follow:

1:  /**  
2:   * The HelloWorldApp class implements an application that  
3:   * simply prints "Hello World!" to standard output.  
4:   */  
5:  class HelloWorldApp {  
6:    public static void main(String[] args) {  
7:      System.out.println("Hello World!"); // Display the string.  
8:    }  
9:  }  

A third which I have yet to check is "How to use PrettyPrint to format source code in Blogger" which is a utility on blogger.com which is where this site is.
But more on this an other day.

No comments:

Post a Comment