2011-10-21 26 views
5

Gần đây tôi đã chuyển từ TweetSharp sang LinqToTwitter và một điều tôi thiếu là một cách để truy xuất một tweet dưới dạng HTML.Làm cách nào để nhận HTML của tweet bằng LinqToTwitter?

TweetSharp có phương thức được gọi là .TextAsHtml() tự động liên kết các đề cập, thẻ băm và siêu liên kết.

Có ai biết tính năng này tồn tại trong LinqtoTwitter không? Bất kỳ cái nhìn sâu sắc về cách TweetSharp đã có thể thực hiện điều này sẽ được nhiều appricated.

UPDATE:

Có vẻ như TweetSharp sử dụng Regular Expressions để phù hợp với URL, đề cập đến, và các thẻ băm. Đây là mẫu:

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); 
+0

bạn không thể sử dụng phương thức thay thế regex trên _parseHashtags? –

Trả lời

17

Đây là giải pháp cuối cùng của tôi sử dụng logic nào đó từ thư viện TweetSharp. Tính năng này hoạt động độc đáo:

/// <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; 
    } 
} 
+0

cảm ơn, điều này đã giúp tôi! – Roger

+0

Chắc chắn làm việc cho tôi. Cảm ơn bạn rất nhiều! – Nickmaovich

+0

Một sự kết hợp của LinqToTwitter và các phần mở rộng của bạn làm cho một giải pháp tuyệt vời để quản lý tweets. – avantprime

Các vấn đề liên quan