Keyvan Nayyeri

God breathing through me

XFN Link Plugin for Windows Live Writer

We're close to a new release of our Windows Live Writer plugins project and I wanted to add a new plugin to this release.

One of things that I use on my blog is XFN relationships.  Inserting an XFN relationship link requires adding appropriate relationships as text values to rel attribute of a link.  I was putting these texts manually but due to the number of defined relationships in XFN I decided to write a simple plugin for Windows Live Writer to make this process easier and help myself (and probably others) in remembering all these relationships.

Plugin is simple.  It enables same options as default Insert Link plugin provided by Windows Live Writer and adds all available XFN relationships as CheckBoxes which you can check to use.

If you need more information about writing a Windows Live Writer plugin in .NET, refer to my post here.

Code for plugin class is very simple:

using System;

using System.Collections.Generic;

using System.Text;

using WindowsLive.Writer.Api;

using System.Windows.Forms;

 

 

namespace XFNLink

{

    [WriterPluginAttribute

       ("557BC8EC-BC09-4b3a-91F3-B0AFF8554042",

        "XFN Link",

        ImagePath = "Images.XFN.png",

        PublisherUrl = "http://nayyeri.net",

        Description = "Inserts XFN Links in Blog Posts")]

 

    [InsertableContentSourceAttribute("XFN Link")]

    public class Plugin : ContentSource

    {

        public override DialogResult CreateContent(IWin32Window dialogOwner,

            ref string newContent)

        {

            using (InsertForm insertForm = new InsertForm())

            {

                DialogResult result = insertForm.ShowDialog();

 

                if (result == DialogResult.OK)

                {

                    newContent = insertForm.GetLink();

                }

                return result;

            }

        }

    }

}

Main logic for this plugin is lain in a user control named InsertUC.  Code for this user control is simple.  When user clicks on Ok button following code runs to generate HTML code for link based on what user has chosen.  InsertUC class has a Link property which keeps string value of HTML code.

private void btnOk_Click(object sender, EventArgs e)

{

    string html = "<a href=\"";

 

    if ((!string.IsNullOrEmpty(txtText.Text)) &&

        (!string.IsNullOrEmpty(txtUrl.Text)))

    {

        html += txtUrl.Text + "\"";

 

        if (!string.IsNullOrEmpty(txtTitle.Text))

        {

            html += " title=\"" + txtTitle.Text + "\"";

        }

 

        if (chkNewWin.Checked)

        {

            html += " target=\"_new\"";

        }

 

        List<string> relations = GetRelations();

        if ((relations.Count > 0) ||

            (!string.IsNullOrEmpty(cbRel.Text)))

        {

            html += " rel=\"";

 

            if (!string.IsNullOrEmpty(cbRel.Text))

                html += cbRel.Text;

 

            if (relations.Count > 0)

            {

                string relationsText = string.Join(" ",

                    (string[])relations.ConvertAll

                    (new Converter<string, string>(delegate(string value)

                    {

                        return Convert.ToString(value);

                    }

                    )).ToArray());

 

                if (html.EndsWith("\""))

                    html += relationsText;

                else

                    html += " " + relationsText;

            }

 

            html += "\"";

        }

 

        html += ">" + txtText.Text + "</a>";

 

        this.link = html;

    }

}

In this class there is a GetRelations function which returns a list of string values for all chosen relations:

private List<string> GetRelations()

{

    List<string> relations = new List<string>();

    foreach (Control control in gbRelations.Controls)

    {

        CheckBox chkRelation = control as CheckBox;

        if (chkRelation.Checked)

        {

            relations.Add(chkRelation.Text);

        }

    }

    return relations;

}

Compiling this code and deploying it to Windows Live Writer provides a new option to insert XFN links easily.  A demo?!  Who is this this guy?!

You can download this plugin with source code from here.

Now playing: Jesse Cook - La Paloma

7 Comments

Anand
Dec 07, 2006 11:59 PM
#

Definitely an interesting feature to put into Windows Live Writer. Looking forward to using it.


Keyvan Nayyeri
Dec 11, 2006 8:03 AM
#

I'm pleased to announce the second version of our Windows Live Writer plugins collection, codenamed Gustnado.


Steve Harman
Jan 20, 2007 8:59 PM
#
Keyvan, First off, thanks for the great plug-in! I just recently started using it to post to my Subtext blog - and it works like a dream. I just finished updating the Subtext XFN Highlighter to make use of some really nice XFN specific icons. Check out the full story here: http://stevenharman.net/blog/archive/2007/01/20/XFN_Microformat__with_Icon_Goodness.aspx

TrackBack
Jan 20, 2007 9:48 PM
#

Tonight while working on some blog posts about CodeMash I decided to make use of a new Windows Live Writer plug-in, Insert XNF Link.


Community Blogs
Feb 09, 2007 6:58 AM
#
UPDATE: How could I have overlooked the Plugin Keyvan Nayyeri wrote two months ago. Sorry buddy! You

Gurkan Yeniceri
Feb 24, 2007 6:06 AM
#
My home made WLW plugins

portrait painting
Nov 28, 2007 9:15 PM
#
May I ask for your link to Gustnado? I’ve been using XFN for several months now and despite of its simplicity, it really appeals to me because it’s a great support for weblogs that I handle. But sometimes it mixes the model (the relationship data) with the view (the front-end web page) and that really pisses me off.

Leave a Comment





Ads Powered by Lake Quincy Media Network