06th Nov 2009
How to post source code on your blog
For a long time I have been looking for a tool to help me post source code on my blog. Obviously many people had solved this because we have all seen well formatted source code on many blogs. I did try several solutions including Syntax Highlighter, Copy Source as Html, C# code format from Manoli and even tried posting screen shots of the code (got this idea from Josh Smith’s blog). They didn’t work well for me (as you can see from several postings it did work for others, it is just a matter of personal taste). The screen shot idea is a good one except for the fact that the readers cannot copy the source from your blog and search engines cannot index the code. I had a workaround for this by linking to a text file which contained the source code. This made the process all mucky and cumbersome.
Some time in the recent past I stumbled on Live Writer. It was quite nifty and did a pretty good job with identifying your blog engine and templates and previewing your post. But when you copy pasted code from Visual Studio, it was not so pretty. Thankfully Douglas Stockwell had written a plugin for Live Writer to paste your code from Visual Studio. Together they did an amazing job in formatting source code as you can see below:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Security.Principal; using System.ServiceModel; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Happy blogging.
I use Highlight (http://www.andre-simon.de/doku/highlight/en/highlight_demo.html). It is simple to use and works well for blogs, S5 presentations etc. If you use it remember to leave the original code in HTML comments, so that you can edit it later. People have had success with http://shjs.sourceforge.net/ also, but since it is JavaScript based it might not work well for blogs.