Technorati Control

Topics: ASP.NET 2.0
Jul 5, 2007 at 9:43 PM
I want to create a control that works like the Wordpress plugin, "SimpleTags": http://www.broobles.com/scripts/simpletags/.

Basically, you can put this in your post:

[tag] tag one, tag two, etc. [/tag]

and it will convert it in the post (when viewed) to:

Technorati Tags: http://technorati.com/tag/tag+one, http://technorati.com/tag/tag+two


Any good resources to do this quick?

Jul 6, 2007 at 8:11 AM
Since I didn't need to have tags and categories, I just converted the tag's to be technorati tags.

I just created a new PostView Control, then had my theme's PostView.ascx inherit from it instead. I then changed CategoryLinks' rel tag to "tag directory", which it should be anyway.

using System.Web;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
 
public class TechnoratiPostView : PostViewBase
{
    protected override string CategoryLinks(string separator)
    {
        string[] keywords = new string[Post.Categories.Count];
        string link = "<a href=\"{0}{1}.aspx\" rel=\"tag directory\">{2}</a>";
        string path = VirtualPathUtility.ToAbsolute("~/category/");
        for (int i = 0; i < Post.Categories.Count; i++)
        {
            if (CategoryDictionary.Instance.ContainsKey(Post.Categories[i]))
            {
                string category = CategoryDictionary.Instance[Post.Categories[i]];
                keywords[i] = string.Format(link, path, Utils.RemoveIlegalCharacters(category), category);
            }
        }
 
        return string.Join(separator, keywords);
    }
 
    protected virtual string TechnoratiTagLinks(string separator)
    {
        if (Post.Tags.Count == 0)
            return null;
 
        string[] tags = new string[Post.Tags.Count];
        string link = "<a href=\"http://technorati.com/tag/{0}\" rel=\"tag\">{0}</a>";
        for (int i = 0; i < Post.Tags.Count; i++)
        {
            string tag = Post.Tags[i];
            tags[i] = string.Format(link, tag);
        }
 
        return "Technorati Tags: " + string.Join(separator, tags);
    }
}
Jul 12, 2007 at 12:47 AM
Hi scottelkin,

I'm very interested in adding this in, as another option to tags and categories.

Is it possible to add this in by modifying just the theme files?

Thanks!
Jul 12, 2007 at 8:22 PM
Edited Jul 12, 2007 at 8:23 PM
you have to put the code above in the app_code directory, then have PostView.ascx in your theme inherit from it:

<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="false" Inherits="Controls.TechnoratiPostView" %>

Then just call the function somewhere in the HTML:

 <div class="bottom">
    <p class="tags"><%=TechnoratiTagLinks(", ") %></p>
    <p class="categories"><%=CategoryLinks(" | ") %></p>
  </div>

I found a bug in my last function. Here is the new one:

        protected virtual string TechnoratiTagLinks(string separator)
        {
            if (Post.Tags.Count == 0)
                return null;
 
            string[] tags = new string[Post.Tags.Count];
 
            //SBE 07/06/2007:  the link for technorati nees "+"'s instead of spaces
            string link = "<a href=\"http://technorati.com/tag/{0}\" rel=\"tag\">{1}</a>";
            for (int i = 0; i < Post.Tags.Count; i++)
            {
                string tag = Post.Tags[i];
                tags[i] = string.Format(link, tag.Replace(" ", "+"), tag);
            }
 
            return "Technorati Tags: " + string.Join(separator, tags);
        }