A Better Twitter Widget for BlogEngine.NET 1.6

by Al Bsharah | 02.02.2010 10:04 PM | Comments (12)

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.zip (2.90 kb) 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!

Share | | |

Categories: Development

Tags: , , , ,

Comments

2/20/2010 10:46:57 AM #

Asp.Net

That's nice, I must apply it to my blog also..
thanks Smile

Asp.Net United States | Reply

2/21/2010 8:30:03 PM #

Pipeman

Hi, I am using the Twitter Widget on the first page. I've no idea why I need to refresh the page if I want to display the twitter posts. Can you teach me to fix it?

Pipeman Hong Kong S.A.R. | Reply

2/22/2010 8:55:55 AM #

Al Bsharah

Hi Pipeman,

It appears the widget doesn't hold up page loads by waiting for the feed to generate, so the tweets you see will be from the last time someone hit the site.  In the background during a page view (and I believe after a cache is expired), it will refresh the feed and save it to disk so the next page load has the update.

As far as I can tell this means you'll have to hit the page twice to assure you're getting the latest feed.  However, if you have sufficient web traffic to your site, you'll likely have enough hits to keep it refreshed.

Seems to be how the BlogEngine folks built it.  It makes sense, so that the page isn't slowed down because of a slow Twitter API.

Cheers,
AL

Al Bsharah United States | Reply

2/22/2010 8:34:53 PM #

Pipeman

Thanks for your reply. But the problem occurs at everytime I visit, it seems so stupid.

Pipeman Hong Kong S.A.R. | Reply

2/23/2010 8:52:10 AM #

Al Bsharah

Hey Pipeman, have you checked to see if your App_Data directory has appropriate write permissions?  The Twitter feed needs to write an XML file into this directory...maybe it's unable to?

I'm guessing at this point, but it shouldn't act they way you describe it.

Al Bsharah United States | Reply

2/22/2010 7:35:14 AM #

Asp.Net

How yo got captcha protection in comments ?

Asp.Net United States | Reply

2/22/2010 8:50:22 AM #

Al Bsharah

Via this thread:
al.bsharah.com/.../...nstallation-Reduce-SPAM.aspx

Cheers,
AL

Al Bsharah United States | Reply

2/23/2010 9:34:52 PM #

Asp.Net

Where can I read about Comments "with answer" mode for BlogEngine, to make my skin support answers to comments?
Thanks.

Asp.Net Russia | Reply

3/1/2010 7:55:15 AM #

dotnetnoob

Hi Al Bsharah: Just curious how you are getting the "sociable" links on your posts? Can you share your BE code for that?

dotnetnoob United States | Reply

3/1/2010 8:09:32 AM #

Al Bsharah

Try this link:
gordon-breuer.de/.../...ion-for-BlogEngineNET.aspx

Cheers, AL

Al Bsharah United States | Reply

3/1/2010 1:11:34 PM #

dotnetnoob

Thanks! I ended up just pasting the "HTML" version into my theme's PostView.ascx...but I will check out the extension.

dotnetnoob United States | Reply

5/1/2010 9:31:15 AM #

trackback

BlogEngine.NET Twitter Widget

BlogEngine.NET Twitter Widget

planetdonovan | Reply

Add comment
Anything that hints of SPAM will be either caught by my filters or deleted by me, so please don't waste your time or mine.


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



RecentComments

Comment RSS

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Tag cloud