Friday, December 10, 2010

Code-Listings with Syntax-Highlighting - Google Code Prettifier

Probably I want to post code snippets in future. So I watched out for syntax-highlighters for code listings. The easiest to use was Google Code Prettifier.

A small tutorial is here.

Great things about this library:
  • I can use the necessary css and javascript files directly from the SVN repository.
  • It is easy to use, lightweigth and rather small (61KB for the CSS + JavaScript code)
    • I don't even have to specify the language - the prettifier "guesses". However, if a guess fails, it can be supported by specifying the language anyway.
Here are some tests that should show that it works:

Java:
public class Foo {
  private int x = 1;
  private String y = "foo";
}

Bash:
#!/bin/bash

# hey, just a test
echo "hello world"
cat foo.txt | head -10 | tail -1

HTML (the < and > symbols are HTML-escaped, but I'm not planning to post a lot of HTML/XML code anyway) - by the way: that's the structure of the code I used for the Java code sample above:
<html> 
 <head> 
  <link href="prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="prettify.js"></script>
 </head>

 <body onload='prettyPrint()'> 
   <pre class="prettyprint">
   public class Foo {
     private int x = 1;
     private String y = "foo";
   }
   </pre>
 </body> 
</html> 

No comments:

Post a Comment