Thursday, July 28, 2011

Configured and tested Alex Gorbatchev's open source syntax highlighter with Blogger

I configured Alex Gorbatchev's SyntaxHighlighter in my Blogger Template according to the instructions on the following pages:

http://alexgorbatchev.com/SyntaxHighlighter/manual/installation.html
http://alexgorbatchev.com/SyntaxHighlighter/hosting.html
http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html

Here follows some test code that appears to render mostly OK (except line numbering gets out of sync with some brushes). My system setup is:
Ubuntu 11.04 Natty Narwhal (GNU/Linux i686, 2.6.38-10-generic)
Chromium 12.0.742.112 (90304)
Opera 11.50 Build 1074
Firefox 5.0 / Canonical - 1.0

Using the SQL brush:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
SELECT *
FROM users
WHERE user_id = 122333;

Using the JavaScript brush with the "pre class="brush: js"" method:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
/**
 * SyntaxHighlighter for JavaScript
 */
function foo() {
    if (counter <= 10)
        return;
    // it works!
}

Using the JavaScript brush with the "script type="syntaxhighlighter" class="brush: js"" method:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK

Using the Lua brush from:
http://www.undermyhat.org/blog/wp-content/uploads/2009/09/shBrushLua.js
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
--
-- SyntaxHighlighter brush for Lua
--
-- make a function the traditional way
function func(a)
    print("-- func: "..a)
end
-- make a function the functional way
local func2 = function(a)
    print("-- func2: "..a)
end
-- call them
func  ("test argument")
func2 ("test argument")
-- anonymous function called immediately
local oneplustwotimesfourisnine = 1 + (function(n) return n*2 end)(4)
-- generate random numbers/simulate a dice throw
local r = newrandom()
for i=1,6 do
    print("dice "..i.." rolled "..r:random(6))
end
-- an if
if android_sdk() == "9" then
    print "Gingerbread"
end

Some Java code:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
/**
 * SyntaxHighlighter for Java
 */
@LuaMethod(global = true)
public String android_sdk() {
    String sdk_level = String.format("%s", VERSION.SDK_INT);
    return sdk_level;
}

A touch of Groovy:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
/**
 * SyntaxHighlighter for Groovy
 */
import groovy.swing.SwingBuilder
import javax.swing.*

def swingBuilder = new SwingBuilder()
swingBuilder.frame(title:"Groovy SwingBuilder", 
                   defaultCloseOperation:JFrame.EXIT_ON_CLOSE, 
                   size:[444,333],
                   show:true) {
  panel(){
    label("Swinging with Groovy!")
  }
}

Using the Python brush:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
#!/usr/bin/env python
import sys

#-- some test functions -----------------------------
def fa(): pass
def fb(): pass
def fc(): pass

#-- a dull class ------------------------------------
class Skeleton():
    def __init__(value1, value2):
        self.field1 = value1
        self.field2 = value2
 
sl = str.lower # function alias ---------------------
print(sl("I_USED_TO_BE_UPPERCASE"))

av = sys.api_version
aa = sys.argv
print("-- testing 1 2 3...")    
print("-- version: %s" % av)
for a in aa: print a

The Perl brush:
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
#!/usr/bin/perl
# split path into seperate lines for easy viewing
print "\n>>\n>> $0: splitting PATH...\n>>\n";
my $i=0, $l=0, $t=0;
foreach ( split(/:/, $ENV{PATH}) ) {
  $i++; $l=length; $t+=$l;
  print "\n[$i]\t[$l]\t[$t]\t$_" ;
}
print "\n\n>>\n>> $0: DONE!.....\n>>\n";

AND! Getting totally carried away with some cool (Google) Go language GTK sample code using the brush at:
http://d.allistersanchez.com/js/shBrushGo.js
See the following post for more info:
http://hackgolang.blogspot.com/2010/05/syntax-highlighting-for-golang.html
Chromium: line numbers misaligned / Opera: OK / Firefox: OK
//
// SyntaxHighlighter for the Go programming language
//

package main

import (
  "os"
  "github.com/mattn/go-gtk/gtk"
  "strconv"
)

func main() {
  gtk.Init(&os.Args)
  window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
  window.SetTitle("GTK Notebook")
  window.Connect("destroy", gtk.MainQuit)

  notebook := gtk.Notebook()
  for n := 1; n <= 10; n++ {
    page := gtk.Frame("demo" + strconv.Itoa(n))
    notebook.AppendPage(page, gtk.Label("demo"+strconv.Itoa(n)))

    vbox := gtk.HBox(false, 1)

    prev := gtk.ButtonWithLabel("go prev")
    prev.Clicked(func() {
      notebook.PrevPage()
    })
    vbox.Add(prev)

    next := gtk.ButtonWithLabel("go next")
    next.Clicked(func() {
      notebook.NextPage()
    })
    vbox.Add(next)

    page.Add(vbox)
  }

  window.Add(notebook)
  window.SetSizeRequest(400, 200)
  window.ShowAll()

  gtk.Main()
}
For intrigued parties, check out Go-GTK at: http://mattn.github.com/go-gtk/