There’s a great plugin for Windows Live Writer called VSPaste by Douglas Stockwell: http://www.11011.net/software/vspaste. It lets you copy and paste syntax-highlighted code from Visual Studio into your blog post while retaining the coloring and formatting and automatically removing extra tabs. Unfortunately, it always pastes the code as a new code block, even if you wanted it to be inline within your text. For example, if I want to write about the ref string newContent parameter of VSPaste’s implementation of ContentSource.CreateContent(), then it would come out looking like this:
I’m writing about about theref string newContentparameter ofVSPaste’s implementation ofContentSource.CreateContent()
You’d think it would only be mildly inconvenient to just hit backspace in the appropriate places, but Windows Live Writer doesn’t handle the boundaries between formatted text and unformatted text very well.
Another thing about VSPaste is that it doesn’t use the same font that you’re using in Visual Studio. It just assumes you have a stylesheet on your blog with a “code” style and uses that instead. I’d rather it used Visual Studio’s font, which may be something like Consolas or Courier or Verdana depending on what you’ve chosen in Visual Studio’s settings.
So, I did some debugging and discovered what it would take to solve both of these issues, and built a new Windows Live Writer plugin called InlineVSPaste that references the original VSPaste but incorporates those two changes.
For those interested, here is the new code of the method:
public class InlineVSPaste : ContentSourceI sent Douglas an email on November 11th, but I haven’t heard back from him. I’d love for him to update the VSPaste plugin on the Windows Live Gallery with these changes. But in the mean time, I’ve attached the compiled plugin to this post. Just put it in your C:\Program Files\Windows Live\Writer\Plugins directory and restart Windows Live Writer. (Use the x86 version of Program Files if you’re on a 64-bit system).
public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
string rtf = (string)Clipboard.GetData(DataFormats.Rtf);
string rtf2010 = Regex.Replace(rtf, @”\\par }$”, “}”);
string font = Regex.Match(rtf, @”\\fonttbl.*? (.+?);”).Groups.Value;
string html = HTMLRootProcessor.FromRTF(rtf2010);
string htmlundent = VSPaste.VSPaste.Undent(html);
// Replace “style=code’ with the direct font.
newContent = “<font face=\”” + font + “, Courier\”>” + htmlundent + “</font>”;
// If it’s multi-lined then put it in a <pre>.
// The <br/> is needed to stop Live Writer from using the previous font for blocks.
newContent = “<pre style=\’word-wrap: break-word; white-space: pre-wrap\’>’ + newContent + “</pre><br/>”;
// The ” ” is needed to stop Live Writer from using the previous font for single lines.
newContent += ” “;
MessageBox.Show(“VS Paste could not convert that content.”, “VS Paste Problem”, …);
[Edit: Updated the code to fix a bug that occurs when pasting from Visual Studio 2010]