本文介绍了如何获得鸣叫与LinqToTwitter HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从TweetSharp切换到LinqToTwitter和一件事,我缺少的是检索鸣叫为HTML的方式。



TweetSharp呼吁的方法 .TextAsHtml()自动链接提到,哈希标签和超链接。



有谁知道,如果这样的功能在LinqtoTwitter存在吗? 。任何洞察TweetSharp是如何能够做到这一点会非常appricated



更新:



看起来好像TweetSharp使用正则表达式匹配的URL,提到和哈希标签。这里有一个例子:

 私有静态只读正则表达式_parseUrls =新的正则表达式(\\b(([\\\ ??\\w-] +:// |万维网[])[^ \\s()&所述;>] +(:\\([\\w\\d] + \\)|([^ \\p {P} \\s] | /))),RegexOptions.IgnoreCase | RegexOptions.Compiled); 
私人静态只读正则表达式_parseMentions =新的正则表达式((^ | \\W)@([A-ZA-Z0-9 _] +),RegexOptions.IgnoreCase | RegexOptions.Compiled);
私人静态只读正则表达式_parseHashtags =新的正则表达式([#] + [A-ZA-Z0-9 -_] +,RegexOptions.IgnoreCase | RegexOptions.Compiled);


解决方案

下面是我使用了一些逻辑从TweetSharp的最终解决方案图书馆。它很好地工作了:

  ///<总结> 
///扩展了LinqToTwitter库
///< /总结>
公共静态类TwitterExtensions
{
私人静态只读正则表达式_parseUrls =新的正则表达式(\\b(([\\w-] +:// | WWW [ ])[^ \\s()&所述;]的计算值+(?: \\([\\w\\d] + \\)|([^ \ \p {P} \\s] | /))),RegexOptions.IgnoreCase | RegexOptions.Compiled);
私人静态只读正则表达式_parseMentions =新的正则表达式((^ | \\W)@([A-ZA-Z0-9 _] +),RegexOptions.IgnoreCase | RegexOptions.Compiled);
私人静态只读正则表达式_parseHashtags =新的正则表达式([#] + [A-ZA-Z0-9 -_] +,RegexOptions.IgnoreCase | RegexOptions.Compiled);

///<总结>
///解析状态文本到HTML相当于
///< /总结>
///< PARAM NAME =状态>该LinqToTwitter<见CREF =状态/>< /参数>
///<退货和GT;的HTML格式的字符串< /回报>
公共静态字符串TextAsHtml的状态(状态)
{
串tweetText = status.Text;

如果(!String.IsNullOrEmpty(tweetText))
{
//更换网址,
的foreach(VAR urlMatch在_parseUrls.Matches(tweetText))
{
匹配匹配=(匹配)urlMatch;
tweetText = tweetText.Replace(match.Value,的String.Format(< A HREF = \{0} \目标= \_blank\> {0}< /一个>中,match.Value));
}

//替换提及
的foreach(VAR mentionMatch在_parseMentions.Matches(tweetText))
{
匹配匹配=(匹配)mentionMatch;
如果(match.Groups.Count == 3)
{
字符串值= match.Groups [2] .value的;
字符串文本=@+价值;
tweetText = tweetText.Replace(文字,的String.Format(< A HREF = \http://twitter.com/{0}\TARGET = \_blank\> {1}&下; / A>中,值,文本));
}
}

//替换哈希标签
的foreach(VAR hashMatch在_parseHashtags.Matches(tweetText))
{
匹配匹配=(匹配)hashMatch;
查询字符串= Uri.EscapeDataString(match.Value);
tweetText = tweetText.Replace(match.Value,的String.Format(< A HREF = \http://search.twitter.com/search?q={0}\TARGET = \\ \\_blank\> {1}&下; / A>中,查询match.Value));
}
}

返回tweetText;
}
}


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;
    }
}

这篇关于如何获得鸣叫与LinqToTwitter HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 01:47