Archive for June, 2008

New Writer Plugins Using The New SDK

As mentioned, and demonstrated, there is a new SDK for Live Writer and true to form, I have been beavering away with them writing up new plugins. This post will showcase the 6 plugins I’ve written using the new SDK. So let’s jump straight into it.

Emotify Plugin

You might remember that the first plugin I ever wrote for Live Writer was my Insert Emoticon plugin, where you clicked on the insert link on the right, chose your emoticon and put it in your post. Well I always found that to just be tedious and a pain, so with this new SDK, I can now take it one step further. So I have added to the original plugin and now you can simply do a Live Messenger emoticon code in your post The plugin will ask you if you want to emotify your post, or you can set the options to let the plugin do it automatically. This plugin was made using the PublishNotificationHook base class. I must note, this plugin requires .NET 3.5 to run (I wanted to try out some LINQ to XML. Joe is trying to persuade me to create one that is .NET 2.0 friendly, and I’ll try to, but for now, this is .NET 3.5. Would also like to thank Inky for his invaluable help with the regular expressions that do all the gory work. Thanks dude

Now Playing

There are many different plugins that allow you to put what you’re listening to into your blog and I think I’ve used each one, but this is a perfect example of a plugin that could utilise the HeaderFooterSource base class. And that’s exactly what I’ve done. The plugin itself is nothing special, it doesn’t even have its own API, but what it does allow is for you to customize what you want it to say and what parts of the song to show:

Now, a couple of things about this plugin, for it to work, you need to have the Windows Media Player blogging plugin which you can download here for XP or here for Vista. If you’re using iTunes, then you don’t need to install anything else, the plugin will query iTunes itself for the currently playing information.

Capitalize Title

This is a very simple plugin that will check to see if your blog title is using capital letters for the first letter of each word, if this isn’t the case, then it changes that. So a blog title would look like this: A New Blog Post Entry With Capital Letters (rather than: a new blog post entry with capital letters).

Signature Plugin

This plugin is quite unique as it has per blog settings! What this plugin does is uses the HeaderFooterSource and at the end of the blog entry will put a signature (like SL in my case). Now sometimes you want to use different signatures for different blogs (if you have more than one configured), so this plugin allows you to have different signatures for each blog configured!!

Bold Hyperlink Plugin

This was a request from Steve Clayton who wanted all of his hyperlinks to be bold, so that’s what this plugin does. It checks the contents of the blog entry and makes all the hyperlinks in the blog post bold.

Profanity Checker

This plugin was something I discussed with the Writer team as a good example of the PublishNotificationHook base class for checking the contents of a post and cancelling the publish if certain criteria are met. So this allows you to configure what words would cancel a blog post from being published, and just to make sure they can’t easily be changed (say by a child), you have to password protect them

ScottIsAFool.WriterUtilities

For some of these plugins, they all required some common code, so I have started compiling my own WriterUtilities static class. Now one of the methods you’ll find is a nice little trick for your Writer plugins. In the IPostInfo interface, all the properties (like title, contents, keywords, etc) are read only, so you can’t actually change them using your code. But wait a minute, isn’t that what I’m doing with at least 3 of the above plugins? Yes, yes it is. Using this WriterUtilities class, you can also change it too, but be warned, it is unsupported by Microsoft (and me ). Here is a quick example of how to use the class:

        public override bool OnPrePublish(IWin32Window dialogOwner, 
            IProperties properties, IPublishingContext publishingContext,
            bool publish)
        {
            string newtitle = "My New Title";
            return ScottIsAFool.WriterUtils.ReplaceText(publishingContext.PostInfo,
                newtitle,
                PostItems.Title);
        }

I also have a another method which returns an array of Blogs configured on the system (which I use for my Signature plugin). I should note that this utilities class is only compatible with the CTP and should not be used in the 1.0 SDK!

Source Code

As always I am making the source code for all these available on CodePlex.

Codeplex page: http://www.codeplex.com/NewLiveWriterPlugins
Download page: https://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=NewLiveWriterPlugins&ReleaseId=14010

All of these plugins should be put in c:\program files\windows live\writer\plugins (or the locale/architecture equivalent).

SL

Whilst writing this, I was listening to AerosmithLay It Down

Advertisements

13 Comments

The New Live Writer SDK

In my LiveSide post, I mentioned that the Live Writer SDK had been updated as well as Live Writer itself. In this post I will show you how to build a couple of quick plugins that use the new APIs, but first, what’s changed?

The big thing to note is that all the API calls in the previous version of the SDK are unchanged; the changes have been additions to the SDK. So the main two new additions are two new plugin classes: PublishNotificationHook and HeaderFooterSource. This gives us four types of plugins: ContentSource, SmartContentSource, PublishNotificationHook and HeaderFooterSource. There are also a couple of other new smaller additions, like the TaskServices class that will let you perform a task in the background whilst still keeping the Writer UI in a responsive state.

But this post is going to concentrate on those two new Plugin base classes, which you can see after the jump.

PublishNotificationHook

This type of plugin can be used to access the post either before or after it’s published. The reason you might want to do this is to check the post for something, this can be the title, the contents or even the keywords (blog engine permitting). You would check the post for whatever you’re looking for and if it passes your check, you allow the post to be published, or else you tell Writer to stop publishing and the post remains unpublished.

Let’s look at some code. The plugin starts off the same way as a ContentSource or SmartContentSource plugin in that you have to declare some WriterPlugin attributes:

    [WriterPlugin("07b95fc3-887e-4558-b53a-0e2a5a47f431", 
"PublishNotification Example", PublisherUrl = "http://www.liveside.net", Description = " ", ImagePath = "writer.png", HasEditableOptions = false)]

So now we create the plugin class and mark it as a PublishNotificationHook class:

    public class PublishNotificationExample : PublishNotificationHook
    {

Like ContentSource or SmartContentSource plugins, you can override the Initialize() method (if your plugin requires settings), this is also true for HeaderFooter plugins too.

With PublishNotificationHook plugins, I mentioned that you can access the post before (or after) it’s published, and we do this by overriding the OnPrePublish and OnPostPublish methods. In this very quick example, I’m going to check whether my post’s contents (ie, the body of the blog entry) contains the word “liveside”:

        public override bool OnPrePublish(IWin32Window dialogOwner, 
            IProperties properties, IPublishingContext publishingContext, 
            bool publish)
        {
            // Check the post contents to see if liveside appears, if it does, 
// return true (publish),
// if it doesn't, return false (cancel publish) return publishingContext.PostInfo.Contents.ToLower().Contains("liveside"); }

Now, the publishingContext.PostInfo is where all the cool stuff is. In this property (which is an IPostInfo object), you have access to the Contents, Keywords, Title, Categories, and even tells you whether it’s a page or not (for WordPress users). Now with this quick check, if my post doesn’t contain the word liveside, then the OnPrePublish method will return false which will stop the post from publishing, if it does contain it, then it will return true and continue to publish.

The OnPostPublish method is slightly different, this method would allow you to do something once the post has been published, for example, update your Twitter status with the blog title and permalink:

        public override void OnPostPublish(IWin32Window dialogOwner, 
            IProperties properties, IPublishingContext publishingContext, 
            bool publish)
        {
            // If this post is a draft (false), don't do anything
            // if it's an actual publish, then publish = true;
            if (!publish)
                return;

            string updateTwitter = string.Format("{0} - {1}",
                publishingContext.PostInfo.Title,
                publishingContext.PostInfo.Permalink);

            // Code to update Twitter
        }

You’ll notice the variable publish that gets passed in both OnPostPublish and OnPrePublish, this will tell your plugin whether it’s being saved as a draft or as an actual published entry. This is the same for HeaderFooterSource plugins, too.

And there you have it, a quick plugin that uses the PublishNotificationHook base class.

HeaderFooterSource

This type of plugin can be used to automatically add some html code to a blog post as either a header, or a footer. Quick examples of this could be a Digg button (header), or Social Bookmarking widget (footer). Now the cool thing about this type of plugin is you can actually specify the permalink for the post to be used, even if the post has yet to be published (I’ll cover this when I start going through the code). If you want to see what your post will look like when using this type of plugin, you just go to the Preview tab and the plugin will get loaded (there are separate methods for the Preview HTML and the Published HTML, but again, I’ll go through that in the code).

Start off by creating the class and declaring it as a HeaderFooterSource plugin:

    public class HeaderFooterSourceExample : HeaderFooterSource
    {

Now, as I mentioned, the cool thing about this base class is you can tell the plugin to use the post’s permalink (even though it hasn’t been published yet), but what we need to do is tell Writer that the plugin will require a [currently] non-existent permalink, we do this by overriding the RequiresPermalink boolean (whose default is false):

        public override bool RequiresPermalink
        {
            get
            {
                return true;
            }
        }

The HeaderFooterSource base class will create two methods for us: GeneratePreviewHtml() and GeneratePublishHtml(). Now these both work in a similar way to GeneratePublishHtml() and GenerateEditorHtml() in SmartContentSource plugins. The GeneratePreviewHtml() is what shows when you switch to the Preview tab, and the GeneratePublishHtml() is what gets published.

So first off the GeneratePreviewHtml():

        public override string GeneratePreviewHtml(ISmartContent smartContent,
            IPublishingContext publishingContext, 
            out HeaderFooterSource.Position position)
        {
            // This tells the plugin where to put your code
            position = Position.Header;
            return "Nothing to see here yet!";
        }

And then the GeneratePublishHtml():

        public override string GeneratePublishHtml(IWin32Window dialogOwner, 
            ISmartContent smartContent, IPublishingContext publishingContext, 
            bool publish, out HeaderFooterSource.Position position)
        {
            position = Position.Header;
            string yeehaaaa = string.Format("This blog entry's link is: <br />" + 
                "<a href=\"{0}\">{0}</a>.", publishingContext.PostInfo.Permalink);

            return yeehaaaa;
        }

So if you now click on the Preview tab, your post would look like this:

Notes about these plugins

After you have have first added any of these plugins to Live Writer, when you click on the Publish button (or Preview for HeaderFooterSource), Writer will ask you if you want to use these plugins for the blog you’re currently using:

For these new types of plugins, you can enable or disable each plugin on a per blog basis, which you can do from the Accounts options window:

And that’s about all you need to know for now. I have made some plugins of my own using these new APIs, but I will cover those in another post.

As always, the source code for this can be found in my skydrive area:

http://cid-fabdddc5cad93494.skydrive.live.com/self.aspx/LiveSide%20-%20Public/SourceCode/NewAPIPlugin.zip

SL

Whilst writing this, I was listening to The Lightning SeedsChange

6 Comments