public static void LinkifyText() { OAuthTokens tokens = Configuration.GetTokens(); var StatusResult = TwitterStatus.Show(tokens, 10506615291641857); Assert.IsNotNull(StatusResult); Assert.That(StatusResult.Result == RequestResult.Success); Assert.IsNotNull(StatusResult.ResponseObject); if (StatusResult.ResponseObject.Entities == null) { return; } string linkedText = StatusResult.ResponseObject.Text; var entitiesSorted = StatusResult.ResponseObject.Entities.OrderBy(e => e.StartIndex).Reverse(); foreach (TwitterEntity entity in entitiesSorted) { if (entity is TwitterHashTagEntity) { TwitterHashTagEntity tagEntity = (TwitterHashTagEntity)entity; linkedText = string.Format( "{0}<a href=\"http://twitter.com/search?q=%23{1}\">{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), tagEntity.Text, linkedText.Substring(entity.EndIndex)); } if (entity is TwitterUrlEntity) { TwitterUrlEntity urlEntity = (TwitterUrlEntity)entity; linkedText = string.Format( "{0}<a href=\"{1}\">{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), urlEntity.Url, linkedText.Substring(entity.EndIndex)); } if (entity is TwitterMentionEntity) { TwitterMentionEntity mentionEntity = (TwitterMentionEntity)entity; linkedText = string.Format( "{0}<a href=\"http://twitter.com/{1}\">@{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), mentionEntity.ScreenName, linkedText.Substring(entity.EndIndex)); } } }
internal static string LinkifiedText(TwitterEntityCollection entities, string text) { if (entities == null || entities.Count == 0) { return(text); } string linkedText = text; var entitiesSorted = entities.OrderBy(e => e.StartIndex).Reverse(); foreach (TwitterEntity entity in entitiesSorted) { if (entity is TwitterHashTagEntity) { TwitterHashTagEntity tagEntity = (TwitterHashTagEntity)entity; linkedText = string.Format( "{0}<a href=\"https://twitter.com/search?q=%23{1}\">{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), tagEntity.Text, linkedText.Substring(entity.EndIndex)); } if (entity is TwitterUrlEntity) { TwitterUrlEntity urlEntity = (TwitterUrlEntity)entity; linkedText = string.Format( "{0}<a href=\"{1}\">{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), urlEntity.Url, linkedText.Substring(entity.EndIndex)); } if (entity is TwitterMentionEntity) { TwitterMentionEntity mentionEntity = (TwitterMentionEntity)entity; linkedText = string.Format( "{0}<a href=\"https://twitter.com/{1}\">@{1}</a>{2}", linkedText.Substring(0, entity.StartIndex), mentionEntity.ScreenName, linkedText.Substring(entity.EndIndex)); } } return(linkedText); }
public static string FormatEntities(string text, IEnumerable <TwitterBaseEntity> entities, ITwitterEntityFormatter formatter) { // Some input validation if (String.IsNullOrWhiteSpace(text) || entities == null || formatter == null) { return(text); } // Iterate through the entities foreach (TwitterBaseEntity entity in entities.OrderByDescending(x => x.StartIndex)) { string before = text.Substring(0, entity.StartIndex); string current = text.Substring(entity.StartIndex, entity.EndIndex - entity.StartIndex); string after = text.Substring(entity.EndIndex); string formatted = null; TwitterHashTagEntity hashtag = entity as TwitterHashTagEntity; TwitterUrlEntity url = entity as TwitterUrlEntity; TwitterMentionEntity mention = entity as TwitterMentionEntity; TwitterMediaEntity media = entity as TwitterMediaEntity; if (hashtag != null) { formatted = formatter.FormatTag(current, hashtag); } else if (url != null) { formatted = formatter.FormatUrl(current, url); } else if (mention != null) { formatted = formatter.FormatMention(current, mention); } else if (media != null) { formatted = formatter.FormatMedia(current, media); } if (formatted != null) { text = before + formatted + after; } } return(text); }
/// <summary> /// Formats a hashtag in a status message, description or similar. /// </summary> /// <param name="text">The text being replaced.</param> /// <param name="tag">The hashtag.</param> public override string FormatTag(string text, TwitterHashTagEntity tag) { return(Tags == null?base.FormatTag(text, tag) : Tags(text, tag)); }
/// <summary> /// Formats a hashtag in a status message, description or similar. /// </summary> /// <param name="text">The text being replaced.</param> /// <param name="tag">The hashtag.</param> public virtual string FormatTag(string text, TwitterHashTagEntity tag) { return "<a href=\"https://twitter.com/hashtag/" + tag.Text + "?src=hash\">#" + tag.Text + "</a>"; }
/// <summary> /// Formats a hashtag in a status message, description or similar. /// </summary> /// <param name="text">The text being replaced.</param> /// <param name="tag">The hashtag.</param> public virtual string FormatTag(string text, TwitterHashTagEntity tag) { return("<a href=\"https://twitter.com/hashtag/" + tag.Text + "?src=hash\">#" + tag.Text + "</a>"); }