A Better Twitter Widget for BlogEngine.NET 1.6



Tuesday, February 2, 2010

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!


Filed Under: Development

Comments (15) -

Asp.NetUnited StatesAsp.Net said:

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

PipemanHong Kong S.A.R.Pipeman said:

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?

Asp.NetUnited StatesAsp.Net said:

How yo got captcha protection in comments ?

Al BsharahUnited StatesAl Bsharah said:

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

PipemanHong Kong S.A.R.Pipeman said:

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

Al BsharahUnited StatesAl Bsharah said:

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.

Asp.NetRussiaAsp.Net said:

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

dotnetnoobUnited Statesdotnetnoob said:

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

dotnetnoobUnited Statesdotnetnoob said:

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

ThendralIndiaThendral said:

Hi Al,

Where is the Original Post that says how we can use this Widget on the Blog.......... Thanks a Lot

Al BsharahUnited StatesAl Bsharah said:

Hi Thendral,

When you're logged in as an admin, there's a drop-down-list to add widgets at the bottom of the widget zone.  One of them is the Twitter widget.

TilaUnited StatesTila said:

Hi, How to integrate twitter widget manually in custom theme. using code?

SexySelect.netUnited KingdomSexySelect.net said:

Hello, I know this is quite old. However i have created the TweetMe extension that will automatically tweet your new postings to twitter following oAuth requirements and size limiting.

www.sexyselect.net/.../...our-post-to-twitter.aspx

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading