jump to navigation

Pasting Code into Blog Entries using Windows Live Writer April 1, 2007

Posted by Karl Hulme in .Net, C#, Windows Live Writer.

This blog is no longer actively maintained. The content is well over 5 years old – which is like 50 coder years. Use at your own risk!

I’ve tried a couple of different ways of getting code from Visual Studio onto a blog post using Windows Live Writer.  Unfortunately something has nearly always been lost on route.  I’m trying out a new method which seems to be working ok on my local machine, but the only way to test it properly is to upload something -so here goes!

First download the CopySourceAsHTML plugin for Visual Studio.  It’s written by Colin Coller, who I gather now works for Avanade.  This plugin adds a ‘Copy as HTML’ command.  Simply right-click on some selected text, hit the command, and it will convert the code to an HTML representation and place it on the clipboard.  It uses the colors as defined in Visual Studio which is much more useful than anything else I’ve seen before.

You can configure the css produced by the plugin at the file, line and block level. However for this to work properly you may need the CSS Upgrade from wordpress, otherwise it seems to strip out some of the styling on upload.  This $15 for the year.  Since WordPress has a maximum column width of 450px, I’ve specified this as a file level css rule.  So my code should sit in a scrollable DIV element.  I’ve limited the height too so that long scripts don’t disappear off the page.  I’ve also set the padding on the lines to be zero, because otherwise it spaces out the lines of code massively, this is probably specific my current theme. The finished settings that work for me are below.

Once the HTML is on the clipboard you can then flick to the HTML view in Windows Live Writer, paste, and then flick back to normal view.  This requires 6 mouse clicks in WLW, but you can get it down to 1 if (like me) you want it to be really quick.  To do this, create a trivial WLW Addin that inserts the clipboard contents directly into the HTML of the post.  You can get a built version of the plugin from here, but it’s just as quick to compile it yourself.  The source code is below.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
namespace InsertHtmlPlugin
    [WriterPlugin("58409179-ECA9-4BE4-8F4A-45F2D0DD79A6", "Insert Clipboard Contents as HTML", 
            HasEditableOptions = false, ImagePath = "SmallHtml.png",
            PublisherUrl = "https://karlhulme.wordpress.com",
            Description = "Plugin for quickly insterting HTML from the clipboard.")]
    [InsertableContentSource("Insert Clipboard as HTML", SidebarText="Clipboard as HTML")]
    public class InsertHtmlPlugin : ContentSource
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
            newContent = Clipboard.GetText();
            return DialogResult.OK;

The code should look reasonable on RSS feeds too.

Hopefully WordPress will get the missing pictures problem sorted very soon.  The lack of pictures makes everything look very bland. 

Tweaking the CodeFormatter plugin for Windows Live Writer February 13, 2007

Posted by Karl Hulme in .Net, Windows Live Writer.

This blog is no longer actively maintained. The content is well over 5 years old – which is like 50 coder years. Use at your own risk!

I downloaded the binaries of the CodeFormatter plugin for Windows Live Writer (the blogging tool) from Steve Dunn’s blog.  He’s provided the plugin in binary form so you can just extract the contents to your \Windows Live Writer\Plugins directory.

One small problem with it though, is that the code it produces is enormous.  I’m not talking about code bloat, i mean physically on the screen it’s huge.  To make matters worse it doesn’t scroll, which means you can end up losing half of the code.  Luckily the source is also available. 

Using the source you can easily edit the HTML that’s produced.  For some strange reason I can’t get it to pick up the font-family correct in the end result, but it looks ok in the editor so i think that’s something to do with the template i’m using on word press.

sb.Append(@"<div style=""width: 430px; max-height: 500px; overflow: scroll; font-family: 'Courier New', monospace; font-size: 7pt;""><pre style="""); if ( this.BackgroundColor != Color.Empty ) sb.AppendFormat( @"background-color:{0};", ColorTranslator.ToHtml( this.BackgroundColor) ); if ( this.WordWrap ) sb.Append( @"white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;" ); sb.AppendFormat( @""">{0}</pre></div>", s );

If you don’t have time to edit the files yourself then you can grab the binaries that I rebuilt.  If anyone has a problem with me reposting here then let me know and i’ll take them down.

There’s more plugins on the codeplex site here, which includes a PasteAsHtml plugin by Bryant Likes.  It installed easy enough but I didn’t have much luck with it.