.Net, Windows Live Writer

WLW Plugin for WordPress.com – New Version

Almost immediately after I published the WLW WordPress.com Syntax Highlighter to CodePlex a little voice inside my head was telling me that I can do better than that. Luckily for me, that voice was right.

You can find it here: WLW WordPress.com Syntax Highlighter Plugin

The problem with the initial version of the plugin was the fact that I could not get around the "auto-encoding" feature from WLW. This basically meant that any time you wanted to have quotes or angle brackets in your code it would get encoded. Worse still was you had to go to the "Source" tab, correct the encoded characters and then remember to not go back to the "Edit" tab. Sounds like a great plugin to me…

To resolve this issue I switched from using the ContentSource base class to using the SmartContentSource base class. This base class provides you with the ability to have two sets of content: the "Editor" version and the "Publish" version. The "Editor" version is obviously what you see within WLW while the "Publish" version is what is sent to your blog.

Two methods… how simple is that?

Well… when you start off using the ContentSource and WPF… not so simple. The SmartContentSource has what is called an "Editor" which appears in the right pane of WLW when the content is selected. This "Editor" must inherit from SmartContentEditor which in turn inherits from Windows.Forms.UserControl.

Rather than attempting to fight that battle I found it was simpler to just pull out the WPF stuff and go directly with a Forms version.

Funny how the original intent was to show how to create a plugin for WLW using WPF only to rip it out. Live and learn… I guess.

Advertisements
Standard
.Net, Windows Live Writer

WLW Plugin for WordPress.com on CodePlex

There have been some requests to have me publish the WordPress.com sourcecode highlighter plug-in that I created for Windows Live Writer. So I went ahead and created a CodePlex project for it and published the code and the assembly.

You can find it here: WLW WordPress.com Syntax Highlighter Plugin

There is no official release but you can go to the "Downloads" tab and select the alpha release.

Publishing this out on CodePlex was actually fun and nerve racking at the same time. Since this was the first project that I ever published out there I wanted to make sure that everything was in "tip top shape." I can’t tell you how much time I spent just getting the "summary" correct. I must have rewritten it about five or six times. I am sure I made some faux pas but how is a guy suppose to learn?

Then I realized… it is what it is. Either people are going to find it useful or people are going to think its junk. Either way… it is the spirit of it all that I am finding more interesting. I had a need, wrote a little plug-in to satisfy that need, and am now sharing it to those who are interested.

What I think is going to be even more fun is revisiting this code later and seeing what improvements can be made and how to make it better.

So enjoy and let me know your comments and suggestions.

Standard
.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

Standard
.Net, Windows Live Writer, WPF

Windows Live Writer Plugin for WordPress.com [sourcecode] Highlighter

Recently been wanting to blog more and more about code and give my posts some more punch by providing some code samples. The one thing that I always notice about other blog posts were the nice little syntax highlight boxes for code. You know the one that has the line numbers and allows you to copy to the clipboard or print.

So I set out to get my hands on a syntax highlight plugin that I could use and found a lot of them out there are really neat and well put together. One of the really important things for me was to also find a plugin for Windows Live Writer (which is simply one of the best tools). So long story short, I found my plugin’s and was ready to go.

One small problem, I am blogging on WordPress.com.

So that means, no pluggin directory, no JavaScript files, nothing. I did some reading on the WordPress.com website and found out that they do have and support a syntax highlighter for code. Wow… this was great. Now the only thing left was to find a plugin for Windows Live Writer that would work with this type of syntax highlighter.

If you try to search the web for “Syntax Highlighter”, “WordPress”, “Windows Live Writer”, and “Plugin” you will be presented with hundreds and hundreds of results. All of them talking about something that I couldn’t use, didn’t have, or didn’t want. I was really sad.

I came across a post by Rick Strahl covering some work he did on an Amazon book plugin for WLW using WPF and decided to rollup my sleeves and write my own. I mean think about it… writing a WLW plugin in WPF for a syntax highlighter that hasn’t been made yet (as far as I know anyway)… how cool is that!

So the next few posts will cover what I did to create the plugin and the issues that I ran into while creating it. And before I forget… I need to show of my plugin… The following code snippet was added using the plugin (of course) and shows one of the classes I created for the plugin. It is simply a name/value pair class that I use for the languages drop down list

public class SourceCodeLanguage
{
  private readonly string name;
  private readonly string value;

  public string Name
  {
    get { return name; }
  }

  public string Value
  {
    get { return value; }
  }

  public SourceCodeLanguage(string name, string value)
  {
    this.name = name;
    this.value = value;
  }
}

How cool is that? Well, maybe it isn’t something to write home about but… nah… it is. Currently the list of features this plugin has is really small. I really wanted to get it up and running and prove to myself that I could do it before getting into too many bells and whistles. Features:

  • Selectable indent size (2, 4, 6)
  • Changing the indent size re-formats the “code” sample
  • Selectable “code” language (e.g. C#, Ruby, Python, etc..)
  • All languages supported by WordPress.com are available
Standard