Al Bsharah Business Minded, Technically Inclined

A Better Twitter Widget for BlogEngine.NET 1.6

A

UPDATE 05/01/2010:  Per Donovan Olivier’s Suggestion, I’ve updated this widget to fix a problem in displaying Twitter usernames with an underscore in them.  Thanks Donovan!

I made some tweaks today to the new Twitter Widget that was released with BlogEngine.NET 1.6.  I noticed that @User and #Hash tags were not linked up, so I fixed that.

Below is the code you need to modify.  Any GREEN code is new, any BLACK code is code that was already there.  Should be pretty easy to cut/paste it in place.

Option #1: Download the version 1.1 widget.ascx.cs-v1.1 file directly, drop it into your site, refresh.

Option #2: Make the following changes in the file: <root>/Widgets/Twitter/widget.ascx.cs:

Comment this out, or remove it all-together:

//if (title.Contains(“@”))
//continue;

Add the two lines shown here:

twit.Title = ResolveLinks(title);
twit.Title = ResolveUserLinks(twit.Title);
twit.Title = ResolveHashLinks(twit.Title);

Add the green lines below:

    //URL
    private static readonly Regex regex = new Regex(“((http://|https://|www\\.)([A-Z0-9.\\-]{1,})\\.[0-9A-Z?;~&\\(\\)#,=\\-_\\./\\+]{2,})”, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const string link = “<a href=\”{0}{1}\” rel=\”nofollow\”>{1}</a>”;
    //@User
private static readonly Regex regex2 = new Regex(“@[a-zA-Z0-9_]*”, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const string link2 = “<a href=\”{0}{1}\” rel=\”nofollow\”>{2}</a>”;
//#Hash
private static readonly Regex regex3 = new Regex(“#[a-zA-Z0-9]*”, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const string link3 = “<a href=\”{0}{1}\” rel=\”nofollow\”>{2}</a>”;

Add the two green functions below:

    /// <summary>
/// The event handler that is triggered every time a comment is served to a client.
/// </summary>
private string ResolveLinks(string body)
{
return regex.Replace(body, new MatchEvaluator(Evaluator));
}
private string ResolveUserLinks(string body)
{
return regex2.Replace(body, new MatchEvaluator(EvaluatorUser));
}
private string ResolveHashLinks(string body)
{
return regex3.Replace(body, new MatchEvaluator(EvaluatorHash));
}

    /// <summary>
/// Evaluates the replacement for each link match.
/// </summary>
public string Evaluator(Match match)
{
CultureInfo info = CultureInfo.InvariantCulture;
if (!match.Value.Contains(“://”))
{
return string.Format(info, link, “
http://”, match.Value);
}
else
{
return string.Format(info, link, string.Empty, match.Value);
}
}
/// <summary>
/// Evaluates the replacement for each @User link match.
/// </summary>
public string EvaluatorUser(Match match)
{
CultureInfo info = CultureInfo.InvariantCulture;
String user = Regex.Replace(match.Value, “@”, “”);
return string.Format(info, link2, “
http://twitter.com/”, user, match.Value);
}
/// <summary>
/// Evaluates the replacement for each #Hash link match.
/// </summary>
public string EvaluatorHash(Match match)
{
CultureInfo info = CultureInfo.InvariantCulture;
String linktag = Regex.Replace(match.Value, “#”, “%23”);
return string.Format(info, link3, “
http://search.twitter.com/search?q=”, linktag, match.Value);
}

And that should do it!

About the author

Al Bsharah

Al’s been involved in multiple San Diego startups since 1999 after leaving the Detroit auto industry as an electrical engineer. He's started two of his own companies where he's raised capital from both VCs and angels, and sold one of them to both Seismic and Return Path. He resides on the board of Startup San Diego, is a Tech Coast Angels member, and has graduated both Techstars and Founder Institute accelerator programs where he now mentors. Al is currently the Vice President of Product Strategy at Seismic and in his free time he manages to play a little beach volleyball, trade stocks, and camp with his wife, son, dog, and friends.

18 comments

By Al Bsharah
Al Bsharah Business Minded, Technically Inclined