Syntax Highlighting In Blog Posts

by Matt 1. December 2009 23:27

One thing I’ve never been happy with was the ability to highlight code syntax in blogengine.net.  I was using a plug-in called Copy Source As HTML 3.0 which was a plug-in for VS2008.  It did the job in that it coloured the text, but I hated the spacing and the look of the code section.  Here’s an example of the ugliness it would leave me with:

   public int GetSunday()

   {

       int i = 0;

       for (i = 0; i < 7; i++)

       {

           Sunday.Text = WeekFirstDate.AddDays(i).ToString("dddd, dd MMMM");

 

           if (Sunday.Text.Contains("Sunday"))

           {

               break;

           }

       }

       return i;

   }

 

Who the hell wants to look at code samples that look like that?  But I’ve found a new way to go about it.  I’ve installed another plug-in for VS2008 called Paste From Visual Studio.  This plug-in works great with Windows Live Writer, which is a Microsoft product that I was able to easily configure for my hosted blogengine.net blog.  After installing both of these and a restart of VS2008 later, I was able to copy and paste great looking code into my blog posts.  Here’s the same code as above, but with formatting much less likely to make your eyes bleed:

public int GetSunday()
{
    int i = 0;
    for (i = 0; i < 7; i++)
    {
        Sunday.Text = WeekFirstDate.AddDays(i).ToString("dddd, dd MMMM");

        if (Sunday.Text.Contains("Sunday"))
        {
            break;
        }
    }
    return i;
}

I love it.  I love it so much, that I’ve gone through my (small) archive and updated all my old posts to insert code via this function.  If you’re a blogger and are having the same frustration I had until about an hour ago, I highly recommend this simple solution.

Tags:
Categories: .NET | General | Visual Studio