CSharpFeeds - All your C# feeds in one place.

Sponsors

Thursday, December 07, 2006

XFN Link Plugin for Windows Live Writer

by Keyvan Nayyeri via Keyvan Nayyeri on 12/7/2006 5:04:44 PM

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

email it!bookmark it!digg it!

Original Post: XFN Link Plugin for Windows Live Writer

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us