Yet Another Windows Live Writer Code Plug-in
I took some time this weekend to get our company blog site in order. I have neglected my blog long enough and I am going to start posting more. One of my biggest issues with blogging was the poor web user interface we had be using. So I started digging into the rich client blogging tools. Once I found out Live Writer had a plug-in model, I was sold over Office 2007.
After several hours of upgrading our Community Server to a version which supported the full MetaWeblog API including media objects I started to look for a Code Plug-in. There were several ones I liked but they didn't have the exact features I wanted.
The plug-ins I looked at were:
I like that Mike's wrapped everything in a pre tag which allowed me to control the style through CSS style sheets. But Rick's was hands down much better, because it kept the colors used in Visual Studio as opposed to parsing my code and coloring keywords. It was fairly simple to add support for classes to Rick's plug-in.
Rick's plug-in uses JT Leigh's Copy as HTML plug in which is an excellently written add-in for Visual Studio. The one thing that Rick left out of the plug-in was JT Leigh's dialog for modifying the formatting settings. The dialog has many options which I fell should be available to Live Writer user. The plug-in options are available from Tools -> Options -> Plug-ins -> Insert Visual Studio Code -> Options
I modified the dialog window and formatter to support a user supplied class for the div which wraps the source code. This gave me the ability to control my div formatting in Community Servers 'Override CSS' feature.
Here is the code for the plug-in with the ability to edit the options.
[WriterPluginAttribute("97C8235B-7F91-4f2e-921B-7F77AB515BD5", "Paste Code From Visual Studio",
ImagePath = "Images.PasteCode.png",
PublisherUrl = "http://blogs.catalystss.com/blogs/josh_heyse/default.aspx",
Description = "Paste Code From Visual Studio",
HasEditableOptions = true)]
[InsertableContentSourceAttribute("Visual Studio Code")] public class VsCodePlugin : ContentSource
{ public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
{ DialogResult result = DialogResult.OK;
if (System.Windows.Forms.Clipboard.ContainsText() == true)
{ Copier rtfImporter = new Copier();
newContent = rtfImporter.CopyAsHtml(System.Windows.Forms.Clipboard.GetText(TextDataFormat.Rtf)) + "<p></p>\r\n";
}
else
{ result = MessageBox.Show(dialogOwner, "Copy your HTML to the clipboard.", "Paste as Html", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK && System.Windows.Forms.Clipboard.ContainsText() == true)
{ newContent = System.Windows.Forms.Clipboard.GetText();
}
}
return result;
}
public override void EditOptions(IWin32Window dialogOwner)
{ (new CopierPreferencesDialog()).ShowDialog(dialogOwner);
}
}
I am attaching the source code and plug-in.
Alright, so now I have no more excuses; time to start publishing content. Happy Blogging!