New job and updated blog engine

17. February 2011

I haven’t posted anything new on this blog for over one and half years. During that time I have switched job. I am currently working in Windows organization in Windows Update team. I am working on Windows Update and other technologies and components built on top of Windows Update infrastructure.

Recently I have also updated the blog engine on this blog to BlogEngine.NET version 2.

Expect more news and some posts soon.

Blog ,

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 


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.



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."

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.



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).





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.


As I mentioned above the whole project is now available at CodePlex: 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 , , ,