Saturday, October 15, 2011

Color your Code?

Took me a while to figure out that blogger.com does not come with syntax highlighting - duh! Obviously, there's a way to beautify your code:
trait Friendly { def greet() = ":)" }
trait Dog extends Friendly { override def greet() = "Woof" }
trait Cat extends Friendly { override def greet() = "Meow" }
def f[G <: { def greet() : String }](g:G) = println(g.greet())
f(new Dog with Cat)
The twist is to enhance your blog template by a client-side javascript library:
  1. Reference Google's prettify library:
    <link href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
    
  2. Initialize it:
    ...
    <body onload='prettyPrint()'>
    ...
    
  3. Escape your code with a regular <pre> tag:
    <pre class="prettyprint">
    ... # Put your code here
    </pre>
    
One last thing, if escaping html code (&lt; &gt;) drives you crazy, this might come handy:
$ echo '<html>' | sed s/\</\\\<\;/g | sed s/\>/\\\>\;/g

1 comment:

  1. Hey there seems to be some html scrambeling when you view the page in a smaller browser window :)

    Congrats for starting the blog,
    Thorsten

    ReplyDelete