Displaying Code Markup in WordPress

Displaying code (whether it be javascript, java, VB, CSharp, etc) in an HTML page can be a cumbersome chore if you wish to display¬†the code in a format that highlight’s keywords of the code language that you are discussing.

Thanks to Alex Gorbatchev‘s WordPress plugin (aptly named SyntaxHighlighter), that chore is no longer an issue.

Once you download and install the plugin,¬†you simply need to surround your code with [sourcecode] tags and you’re good to go.

For example:

Here is some c# code marked-up with the plain-old HTML <code> tag.

public static string getFileAsString(string fileName) {
StreamReader sReader = null;
string contents = null;
try {
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
sReader = new StreamReader(fileStream);
contents = sReader.ReadToEnd();
} finally {
if(sReader != null) {
sReader.Close();
}
}
return contents;
}

And here is the same c# code marked-up by the SyntaxHighlighter styles.

public static string getFileAsString(string fileName) {
   StreamReader sReader = null;
   string contents = null;
   try {
      FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
      sReader = new StreamReader(fileStream);
      contents = sReader.ReadToEnd();
   } finally {
     if(sReader != null) {
         sReader.Close();
      }
   }
   return contents;
}

Advertisements