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 widget.ascx.cs.zip (2.89 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 or Bookmark this Post…