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; }
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); }
public static string FormatEntities(string text, ITwitterEntities entities, ITwitterEntityFormatter formatter) { return(FormatEntities(text, entities.GetAll(), formatter)); }
public static string FormatEntities(string text, ITwitterEntities entities, ITwitterEntityFormatter formatter) { return FormatEntities(text, entities.GetAll(), formatter); }