Best Code Syntax Highlighter for Snippets in your Blog
I get a few emails a day of folks asking what Syntax Highlighter I use in my blog for my code samples. Specifically, the newer code samples, as some of the old ones sucked as I was experimenting, trying to find the best one to settle on.
The tool I use is actually called SyntaxHighlighter and it's from Alex Gorbatchev. The trick is that the syntax highlighter is all javascript on the client side.
I was having all sorts of troubles with other code highlighters. First, there were ones that put css classes and stuff all through your code, trying to decorate each keyword. This just bloated my feed and site and made the code look weird in some Feed Readers. Then I tried using images for code, like ScottGu does, but that is just wrong. You can't copy paste the code, you can't search it, it's disrespectful for the blind, etc. Meh.
How I post code to my blog
I use Windows Live Writer to post all my blog posts, and it has a great plugin model. I've actually written a WLW plugin for the CueCat...it's really easy. I use a plugin from DasBlog contributor Anthony Bouch called PreCode that directly targets/supports SyntaxHighlighter from within WLW.
That means I see this from inside Live Writer. I slick Insert PreCode Snippet, and paste in my code.
If you're reading this blog post from inside an aggregator or feed reader, the next two code snippets look identical to you. However, if you visit my blog, you'll see that one is different.
// Hello3.cs
using System;
public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
// Hello3.cs
using System;
public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
One looks like this, as HTML:
// Hello3.cs
using System;
public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered
the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
See the 'class="c#" name="code"' part? Alex's Javascript SyntaxHighlighter is looking for those and parsing them on the client side. I choose to add
breaks, but that's an option in PreCode. Other options for SyntaxHighlighter include line numbering, gutters, copy/paste support, a toolbar and more.
P.S. If you don't use Windows Live Writer (and seriously, stop and ask yourself, WHY NOT?) and use instead a web interface, you can integrate SyntaxHighlighter into your web-based rich text editor. For example, Darren made a SyntaxHighlighter Plugin for the popular FCKeditor. Perhaps we'll put that in DasBlog.
Installing SyntaxHighlighter to Your Blog
You install the SyntaxHighlighter by adding it to your blog's template. It doesn't care what blog engine you run, as it doesn't need anything on the server:
Just add the shCore library and just the languages you require. If you want your blog to feel snappy and you have some control over your server, don't forget to set the files/directories to cache on the client by making them expire far in the future. You don't want your user's browsers to keep asking for these scripts each page view.
Even better, you can create your own plugins for SyntaxHighlighter if you use a language Alex hasn't supported officially. This guy threw together a Scala SyntaxHighlighter file by editing the Java one and adding a regex.
There are a few bugs but I think folks forget that Alex is doing this all alone, so I have to give him mad props for the effort. It can be lonely and unforgiving when you do something awesome and either no one cares, or folks only care to complain.
UPDATE: There's some great un-bundled brushes collected here.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Since there wasn't a built in F# brush, I went ahead and created one.
If you are interested you can find the F# brush on my tech blog.
For a primitive example, in the sample you provice "Console" isn't highlighted, and while not crucial in this case it can sometimes, as I say, become confusing.
Those corner cases probably aren't enough to stop this technique being useful though :)
Mohamed Meligy:
I have been using another alternative, CopySourceAsHtml Visual Studio 2005 plug-in.
I agree. I really like the options in CopySourceAsHtml. Even with all the style applied, it's easy to copy and paste the snippet generated (as long as you have line numbers turned off). Previously I have fought with several MovableType plugins, but found them lacking for the current C# specification.
It would be nice if Microsoft could tack a webservice onto MSDN that would return marked-up HTML with all the latest language bits from passed-in plain text. It would be dead simple to call with jQuery and then inject into the DOM on the fly.
Will it also highlight if it the data is coming from a database and the returned text contained code.
Like the following:
<%# Eval("Body") %> //
P.S. If you don't use Windows Live Writer (and seriously, stop and ask yourself, WHY NOT?)
An install program that: 1) tried to install 5 other things; 2) tried to steal search provider and home page; 3) wanted me to make a new account.
What the heck do those have to do with a post editor?!
Mark - The issue was that I couldn't find a syntax highlighting solution that worked in EVERY feed reader (there are lots of problems with online ones like Google Reader and BlogLines if I, as the publisher, try to get tricky with the CSS. Every solution I had would also make the feed 10x larger with tags and styles around the code.
SGraham - Sure, totally agreed the whole Windows Live Virus thing is lame, but uncheck those boxes and move on.
http://james.newtonking.com/archive/2008/05/13/display-nicely-formatted-net-source-code-on-your-blog.aspx
I prefer the Code Snippet plugin for WLW, as discussed and shown here: http://blog.jeffhandley.com/archive/0001/01/01/posting-code-samples.aspx
I have used it in an open source IDE project at http://globalsystembuilder.com and found it works extremely well.
Cheers!
I'm going to be looking at Google Prettify which seems more lightweight and robust than SyntaxHighlighter. We currently use SyntaxHighlighter and enjoy it, but need something faster.
http://code.google.com/p/google-code-prettify/
I eventually found CodeHtmler (http://www.codeplex.com/CodeHtmler) and really liked what I saw. It could handle code from any number of languages and properly handle colorizing them. Furthermore it could be configured to use my existing CSS classes rather than creating new ones of its own. It was a standalone library so I put a wrapper around it for use in VS and now that is all I use.
Better yet, how about you use your insider position at microsoft to have them put an option in IE, to only show debug messages for domains of my choosing?
I too use Alex's highlighter and like it for one more reason.
If CSS and Javascript is disabled, the code still shows up nicely and is usable by CTRL C + CTRL V....
I found that I had to add these lines to my blog template to get the highlighting to work:
<script language='javascript'>
dp.SyntaxHighlighter.ClipboardSwf = 'http://howard.ross.work.googlepages.com/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>
i.e.
<pre class="code csharp"
I've made a start at adding brushes for F#. and <a href="http://stevegilham.blogspot.com/2008/12/syntax-highlight-brush-for-erlang.html>Erlang</a>, as my two "most wanted".
Scott: because it support only 2 operating systems (xp,vista) ? This is WRONG. In these days of .NET/Mono everybody expect that it runs on more than 2 OS.
I know you are ms guy and trying to propagate ms products, but seriously - such "marketing" makes that kind of products not usable.
http://www.codeplex.com/wlwPostCode
greetings Daniel
Comments are closed.
a) it supposes that you have JS on (not always the case, especially when using NoScript)
b) it doesn't work in Google Reader (and possible other feedreaders)