25
Windows Live Writer Plugin for Pasting Source Code from Visual Studio
2 Comments · Posted by nosey in .NET General, Code
Having been through all the plugins available on Windows Live™ Gallery for showing syntax highlighted code snippets with Windows Live Writer, I gave up: there simply did/does not exist a plugin that 1) fulfills my needs and 2) displays properly on my blog.
I have tried Insert Code for Windows Live Writer, Paste from Visual Studio, Code Snippet plugin for Windows Live Writer and Insert Source Code Snippet. My favorite amongst those is definitely the Code Snippet plugin for Windows Live Writer. Many of them succeeds in meeting point #1, but they all without exception fail point #2.
What goes wrong for me is that I use the "Normal" in Live Writer for semi-WYSIWYG-ish interface. This however makes WLW reformat code, and all plugins does syntax highlighting using (amongst others) a <pre></pre> tag. This means that all white-space is significant. However WLW does not care about this, even though, it is within a <pre></pre> tag, so all line breaks and space for indentation is lost.
What to do???
Being the code geek I am, why not write my own WLW plugin that fulfills my needs/requirements:
Allow me to copy code from Visual Studio into WLW and keep formatting
And so be it, behold my first WLW plugin, it is sooooooo easy.
- Install the Windows Live Writer SDK (beta)
- Create a new class library in VS
- Add a reference to WindowsLive.Writer.Api.dll (default is C:\Program Files\Windows Live\Writer\)
- Create a class similar to the one below
- Build and copy .DLL to Windows Live Writer plugin folder (default is c:\Program Files\Windows Live Writer\Plugins\)
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
namespace Veggerby.LiveWriter.CodeSnippetPlugin
{
[WriterPlugin("a202a095-7d8b-4e10-8cb6-4fd51035d354", "Paste Code from Visual Studio",
Description = "Paste Code from Visual Studio",
HasEditableOptions = false,
Name = "Paste Code from Visual Studio",
PublisherUrl = "http://veggerby.com/livewriter/")]
[InsertableContentSource("Paste Code from Visual Studio")]
public class PasteCodePlugin : ContentSource
{
public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
{
if (Clipboard.ContainsData(DataFormats.Rtf))
{
RtfToHtml converter = new RtfToHtml();
converter.RtfDocument = Clipboard.GetData(DataFormats.Rtf) as string;
newContent = string.Format("<div class=\"vscode\">{0}</div>", converter.ConvertToHtml());
return DialogResult.OK;
}
return DialogResult.Cancel;
}
}
}
Where the tricky part lies in taking the code from the clipboard/Visual Studio, which is formatted in RTF and transform to HTML, i.e. the RtfToHtml class.
Syntax highlighting is achieved using <span></span> tags and the style="color: #ffffff" attribute, other formatting (font, etc) is done using CSS on the div.vscode class.
One problem remains still:
WLW will replace <br /><br /> (i.e. two consecutive <br /> tags) with a <p></p> tag. This is not as such a problem, but might require a little more CSS.
If you are interested in the complete source code, drop me a comment.

Joe Cheng [MSFT]
· September 26, 2007 at 1:35
The fact that WLW strips line breaks and extra whitespace within pre tags is specific to the XHTML markup style. You can get around this by going to Weblog | Edit Weblog Settings | Advanced and choosing HTML.
Although glad to see you fixed the problem yourself through a new plugin! You can probably fix the problem by sticking a non-breaking space between them.
Author comment by nosey
· September 26, 2007 at 8:07
I kind of gathered that actually, however I don’t want to compromise the XHTML (okay, the site doesn’t validate as XHTML, but…).
And still the plugin’s should (if possible) support or check for XHTML and add the break tags so that output still renders correct.
But thanks for the input, this was just as much just to try creating a plugin