.Net, Windows Live Writer

WLW Plugin for WordPress.com – Part 1

As stated in my previous post, I have been looking for a plug-in for WLW that would allow me to take advantage of the built in source code highlighter that is available from WordPress.com (the free on-line version). Not being able to find one I decided to write one for myself.

Not knowing where to begin or how to start, I searched for how to build a plug-in for WLW and came across Rick Strahl’s post about creating a WLW plug-in using WPF and decided to do a similar thing. I was fairly confident in my abilities to write a little WPF app but knew nothing about the WLW plug-in architecture so I started there.

For what I needed to do, writing a plug-in was actually really straight forward. I simply added a reference to the “WindowsLive.Writer.Api” assembly, created a class and inherited from the ContentSource class (which inherits from the WriterPlugin class). Looking at the ContentSource class I found that there were three virtual methods that I could choose from: 1) CreateContent, 2) CreateContentFromLiveClipboard, and 3) CreateContentFromUrl. Not knowing what any of these methods did, I choose to override the CreateContent method.

using System;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace SourceCodePlugin {
  public class Plugin : ContentSource {
    public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content) {
      return DialogResult.OK;
    }
  }
}

Now knowing that I could create a WLW plug-in I thought about what I wanted the plug-in to do and how it was going to act. In really basic terms, I wanted to wrap what ever “code” I wrote with “[ sourcecode language=”csharp” ]<–code–>[ /sourcecode ]” as the WordPress.com site had suggested.

Thinking about it a little more, I wanted to ensure that the “code” appeared exactly the way I entered it so… it was clear that I needed to wrap the whole thing with “<pre></pre>” tags. So I wrote a little method to do just that.

public class Plugin : ContentSource {
  ...
  private string FormatOutput(string language, string code) {
    var builder = new StringBuilder();
    builder.AppendLine("&lt;pre&gt;");
    builder.AppendFormat("[ sourcecode language=\"{0}\" ]{1}[ /sourcecode ]",
      language, code);
    builder.AppendLine();
    builder.AppendLine("&lt;/pre&gt;");
    return builder.ToString();
  }
}

I decided to give that a shot so I built the project, deployed the assembly to my “Plugins” folder and started Windows Live Writer.

Sure enough… it didn’t work. I totally forgot about the two attributes that you need to add to your class in order for WLW to load your plug-in. Referencing back to Rick’s post I also realized that I should create an image to make it look like all of the others.

[WriterPlugin(
  "9EDA38F6-AE13-4003-B74A-83E8474F528D", 
  "Wordpress Sourcecode Plugin",
  Description = "A  plugin for WordPress.com blogs",
  PublisherUrl = "https://meisinger2.wordpress.com/",
  ImagePath = "Images.SourceCode.png",
  HasEditableOptions = false)]
[InsertableContentSource("Wordpress Sourcecode Plugin")]
public class Plugin : ContentSource {
  ...
}

After adding that and building it again, sure enough I had my “Hello World” code getting inserted into WLW through my plug-in

Next steps… integrating WPF

Edit: It appears that WordPress.com “sourcecode” bracket magic is a little too good… I had to edit this post and put a space between the bracket (“[” and “]”) and the word “sourcecode” in order for it to come up propertly

Advertisements
Standard

8 thoughts on “WLW Plugin for WordPress.com – Part 1

    • mmeisinger says:

      @Chris
      The code is currently not available but it will be soon.
      The hope is that I will be able to post all of the code used to create the plugin so others can create their own.
      The series should be picking back up soon

    • mmeisinger says:

      @Chris
      My latest post has the details on how to get it out on CodePlex

      But with everything you are doing now… this has got to be childs play ๐Ÿ™‚
      (Really good stuff… I might add)

  1. Hey there Mike,

    How did you go with this? Did you ever get a working version? I am hanging out for something like this soooooo badly right now! Happy to help test/debug code if it helps. Shoot me an email if you like

    – Brian

    • mmeisinger says:

      @Brian
      My latest post has details about how to get it out on CodePlex
      Test, debug, suggest, comment… um… yes please
      I hope it is what you are looking for

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s