I recently switched from TweetSharp to LinqToTwitter and the one thing I'm missing is a way to retrieve a tweet as HTML.
TweetSharp had a method called .TextAsHtml()
which automatically linked mentions, hash tags, and hyperlinks.
Does anyone know if such a feature exist in LinqtoTwitter? Any insight into how TweetSharp was able to accomplish this would be much appricated.
UPDATE:
It looks as though TweetSharp used Regular Expressions to match URLs, mentions, and hash tags. Here is a sample:
private static readonly Regex _parseUrls = new Regex("\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^\\p{P}\\s]|/)))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseMentions = new Regex("(^|\\W)@([A-Za-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseHashtags = new Regex("[#]+[A-Za-z0-9-_]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Here is my final solution which uses some logic from TweetSharp's library. It's working out nicely:
/// <summary>
/// Extends the LinqToTwitter Library
/// </summary>
public static class TwitterExtensions
{
private static readonly Regex _parseUrls = new Regex("\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^\\p{P}\\s]|/)))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseMentions = new Regex("(^|\\W)@([A-Za-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseHashtags = new Regex("[#]+[A-Za-z0-9-_]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// Parse Status Text to HTML equivalent
/// </summary>
/// <param name="status">The LinqToTwitter <see cref="Status"/></param>
/// <returns>Formatted HTML string</returns>
public static string TextAsHtml(this Status status)
{
string tweetText = status.Text;
if (!String.IsNullOrEmpty(tweetText))
{
// Replace URLs
foreach (var urlMatch in _parseUrls.Matches(tweetText))
{
Match match = (Match)urlMatch;
tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", match.Value));
}
// Replace Mentions
foreach (var mentionMatch in _parseMentions.Matches(tweetText))
{
Match match = (Match)mentionMatch;
if (match.Groups.Count == 3)
{
string value = match.Groups[2].Value;
string text = "@" + value;
tweetText = tweetText.Replace(text, String.Format("<a href=\"http://twitter.com/{0}\" target=\"_blank\">{1}</a>", value, text));
}
}
// Replace Hash Tags
foreach (var hashMatch in _parseHashtags.Matches(tweetText))
{
Match match = (Match)hashMatch;
string query = Uri.EscapeDataString(match.Value);
tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"http://search.twitter.com/search?q={0}\" target=\"_blank\">{1}</a>", query, match.Value));
}
}
return tweetText;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With