Windows Live Writer Source Code plug-in

2. April 2009

I use Windows Live Writer for authoring my blog posts. It is very user friendly application that allows you to forget about the ugly web interface your blog engine has for authoring posts. As a developer I wanted to be able to paste source code samples into my posts and be able to format them accordingly. Fortunately Alex Gorbatchev wrote a simple JavaScript application that will format source code according to parameters in the PRE element in which the code is placed. All we need to do is:

  • add support for Alex Gorbatchev’s SyntaxHighlighter to your blog (server)
  • teach Windows Live Writer to enter the right parameters whenever you insert a source code snippet in one of your posts

There are solutions for all of these but none of them suited my needs (I will try to cover the reasons for that later). Rather than complain I thought I would try to solve my problems.

Source Code Plug-in on CodePlex

I have created SourceCodePlugin project on CodePlex to include my solution to the above mentioned needs. The project has two parts: SyntaxHighligher for BlogEngine.NET and SourceCode plug-in for Windows Live Writer.

SyntaxHighligter for BlogEngine.NET

This part of the project allows you to integrate an updated SyntaxHighlighter with the BlogEngine.NET system. The highlighter is based on Alex Gorbatchev's code with a couple of updates:

 

Product: SyntaxHighlighter 
Author: Alex Gorbatchev 
Version: 2.0.296 
Link: http://alexgorbatchev.com/wiki/SyntaxHighlighter

 

Updates from Alex's code:

  • added PowerShell script
  • added FSharp script

 

Why did I have to code it myself? I had to update SyntaxHighlighter to add support for PowerShell and F# because I am used them frequently. Support for other my languages is already part of the product (C#, C++, SQL, XML, JavaScript, etc.). I found an integration into BlogEngine.NET but the extension was not compatible with the latest release of SyntaxHighlighter.

I have decided to update SyntaxHightlighter scripts library and create a new extension component for BlogEngine.NET compatible with the latest release of SyntaxHighlighter.

 

Examples

Sample F# code (this is a snippet from Tutorial.fs):

// Types: records
// ---------------------------------------------------------------

type Card = { Name  : string;
              Phone : string;
              Ok    : bool }
              
let cardA = { Name = "Alf" ; Phone = "(206) 555-8257" ; Ok = false }
let cardB = { cardA with Phone = "(206) 555-4112"; Ok = true }
let ShowCard c = 
  c.Name + " Phone: " + c.Phone + (if not c.Ok then " (unchecked)" else "")

 

Sample PowerShell code (this is a function I use for my nightly job):

#This method starts Windows service. It doesn't throw in case of an error.
function Start-ServiceNotThrowing($service, [bool]$displayWarning = $true)
{
    start-service $service -ErrorVariable errs 2>$null
    if (($errs -ne $null) -and ($displayWarning))
    {
        set-color "yellow"
        write-host "Warning: Service $service is not installed."
        set-color
    }
}

SourceCode plug-in for Windows Live Writer

This part of the project adds a SourceCode plug-in into Windows Live Writer to enable inserting code into your blog posts in a form compatible with SyntaxHighlighter.

To insert a code, click on Source code... item in Insert section (right side of the Windows Live Writer). The plug-in will use the currently selected text or (when configured accordingly) take the text from the clipboard.

 

SourceCodePlugin_MainForm

You can tweak the behavior of the plug-in by clicking Options button to update plug-in options. All values that you enter in this options dialog will be saved for future use (you don’t have to enter the same configuration over and over again).

 

 

SourceCodePlugin_Options

 

Why did I have to code it myself? I found several plug-ins for SyntaxHighlighter. One of them was not able to keep option values between invocations that led to a situation where you had to update your options every time you wanted to touch your source code. Another one saved the options but when I was editing source code that was already inserted into a post, the plug-in wouldn’t display the currently selected text. Instead I had to select the text, press Ctrl+C, open the plug-in and press Ctrl+V.

I have decided I will create a simple plug-in that behave the way I expected.

Resources

As I mentioned above the whole project is now available at CodePlex: http://sourceCodePlugin.codeplex.com. Feel free to check it out and if you find a problem, let me know or take the source code and fix it yourself.

Blog , , ,