C# 如何获得tweet';LinqToTwitter的HTML?
我最近从TweetSharp切换到LinqToTwitter,我缺少的一件事是将tweet检索为HTML的方法 TweetSharp有一个名为C# 如何获得tweet';LinqToTwitter的HTML?,c#,asp.net,twitter,tweetsharp,C#,Asp.net,Twitter,Tweetsharp,我最近从TweetSharp切换到LinqToTwitter,我缺少的一件事是将tweet检索为HTML的方法 TweetSharp有一个名为.TextAsHtml()的方法,可以自动链接提及、哈希标记和超链接 有人知道LinqtoTwitter中是否存在这样的功能吗?任何关于TweetSharp是如何做到这一点的洞察都将非常值得赞赏 更新: 看起来TweetSharp使用正则表达式来匹配URL、提及和哈希标记。以下是一个示例: private static readonly Regex _pa
.TextAsHtml()
的方法,可以自动链接提及、哈希标记和超链接
有人知道LinqtoTwitter中是否存在这样的功能吗?任何关于TweetSharp是如何做到这一点的洞察都将非常值得赞赏
更新:
看起来TweetSharp使用正则表达式来匹配URL、提及和哈希标记。以下是一个示例:
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);
private static readonly Regex\u parseUrls=new Regex(\\b([\\w-]+:/?;www[.])[^\\s()+(?:\([\\w\\d]+\\)|([^\\p{p}\\s]\/)”,RegexOptions.IgnoreCase | RegexOptions.Compiled);
私有静态只读Regex | parseantions=new Regex((^ |\\W)@([A-Za-z0-9|)+),RegexOptions.IgnoreCase | RegexOptions.Compiled);
私有静态只读Regex _parseHashtags=newregex(“[#]+[A-Za-z0-9-"]+”,RegexOptions.IgnoreCase | RegexOptions.Compiled);
这是我的最终解决方案,它使用了TweetSharp库中的一些逻辑。结果很好:
/// <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;
}
}
//
///扩展LinqToTwitter库
///
公共静态类TwitterExtensions
{
私有静态只读Regex\u parseUrls=newregex(\\b([\\w-]+:/?)[^\\s()]+(?:\([\\w\\d]+\\)|([^\\p{p}\\s]\/)”,RegexOptions.IgnoreCase | RegexOptions.Compiled);
私有静态只读Regex | parseantions=new Regex((^ |\\W)@([A-Za-z0-9|)+),RegexOptions.IgnoreCase | RegexOptions.Compiled);
私有静态只读Regex _parseHashtags=newregex(“[#]+[A-Za-z0-9-"]+”,RegexOptions.IgnoreCase | RegexOptions.Compiled);
///
///将状态文本解析为HTML等效文本
///
///林克托维特酒店
///格式化HTML字符串
公共静态字符串TextAsHtml(此状态)
{
字符串tweetText=status.Text;
如果(!String.IsNullOrEmpty(tweetText))
{
//替换URL
foreach(var urlMatch in_parseUrls.Matches(tweetText))
{
匹配=(匹配)urlMatch;
tweetText=tweetText.Replace(match.Value,String.Format(“,match.Value));
}
//替换提及
foreach(var-sinetingmatch in_parsesinetions.Matches(tweetText))
{
匹配=(匹配)匹配;
if(match.Groups.Count==3)
{
字符串值=匹配。组[2]。值;
字符串文本=“@”+值;
tweetText=tweetText.Replace(text,String.Format(“,value,text));
}
}
//替换散列标签
foreach(var hashMatch in_parseHashtags.Matches(tweetText))
{
Match=(Match)hashMatch;
string query=Uri.EscapeDataString(match.Value);
tweetText=tweetText.Replace(match.Value,String.Format(“,query,match.Value));
}
}
返回tweet文本;
}
}
您不能在_parseHashtags上使用regex replace方法吗?LinqToTwitter和您的扩展的组合是管理tweet的一个很好的解决方案。