WordPress Code Highlighting

For a long time I have been using the Code Snippet Plugin for formatting source code in the blog.  It uses the GeSHi engine which always seemed to work well.  Now that I’m moving some stuff around I went to load it and I see that it is no longer active and only supports up to WordPress 2.8.1 so I guess I need a new one.

Now it seems like the choice is WP-Syntax which also uses the GeSHi engine so I’m optimistic that it will handle the parsing for me.  However the syntax is different so it will take some work if I want to migrate old stuff forward.

The syntax for the various options is located on the WP-Syntax Usage page and includes line numbers (line=”42″), escaped html (“escaped="true"), highlighting (highlight="3"), and captions (src"https://github.com/shazahm1/Connections/blob/master/connections.php").  You can even do your own custom GeSHi configurations.

For instance:

<pre lang="<lang>" line="1" highlight="3" src="myfile.cs">
  [Some Code]
</pre>

For me the main languages are Java(java), C# (csharp), HTML(XML), and JavaScript (javascript) so here are a couple of examples in those:

Java:

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

C# (with line numbers and highlight line 4)

1
2
3
4
5
6
7
8
// Hello1.cs
public class Hello1
{
  public static void Main()
  {
    System.Console.WriteLine("Hello, World!");
  }
}

JavaScript (with a caption):

hello.js
function myFunction() {
alert("Hello World!");
}

HTML(XML – escaped):

<HTML>
<HEAD>
<TITLE>Hello, World Page!</TITLE>
</HEAD>
 
<BODY>
Hello, World!
</BODY>
</HTML>