Showing posts with label prettyprint. Show all posts
Showing posts with label prettyprint. Show all posts

Sunday, 13 June 2010

Hello World

Is the Google pretty printer working? Will my code samples come out looking rather nice?

class HelloWorld{
public static void main(String[] args){
String hw="Hello World";
System.out.println(hw);
}
}


Looks like it is. Thanks to Luka Marinko's post on how to set it up.

I modified Luka's recipe slightly, as I found that setting the onload attribute on the body worked for the blog, but was giving me Javascript errors when I tried to use the blogger layout/design tools. All I did was move the onload up to a script tag in the head of the page, and check for the presence of the function before calling it. So, my template looks like this:

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css'
rel='stylesheet' type='text/css'/>
<style type='text/css'>
pre.prettyprint{
font-size: 0.9em;
padding: 4px; background-color:#adf;
border-radius: 8px; -webkit-border-radius: 8px; -moz-border-radius: 8px;
}
pre.lang-html{ background-color: #afd; }
</style>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js'
type='text/javascript'>
</script>
<script type='text/javascript'>
window.onload=function(){
if (prettyPrint){ prettyPrint(); }
}
</script>


which has the added advantage of keeping all the changes in one place.

In sequence, what I'm doing is:

  1. Importing the prettyprint CSS stylesheet

  2. Adding a few extra styles to put the text in funky rounded boxes (where the browser supports it), and make the text a bit smaller so I have to worry less about truncating lines

  3. Import the prettyprint library code from google code

  4. Add a script to safely invoke the pretty printer