private static IEnumerable <Inline> GenerateInlines([NotNull] DependencyObject obj, [NotNull] string text, [CanBeNull] IEnumerable <TwitterEntity> entities) { if (entities == null) { foreach (var inline in GenerateInlines(obj, text)) { yield return(inline); } yield break; } foreach (var description in TextEntityResolver.ParseText(text, entities)) { if (!description.IsEntityAvailable) { yield return(GenerateText(description.Text)); } else { var entity = description.Entity; var display = entity.DisplayText; if (String.IsNullOrEmpty(display)) { display = entity.OriginalUrl; } switch (entity.EntityType) { case EntityType.Hashtags: yield return(GenerateHashtagLink(obj, display)); break; case EntityType.Media: case EntityType.Urls: yield return(GenerateLink(obj, display, ParsingExtension.ResolveEntity(entity.OriginalUrl))); break; case EntityType.UserMentions: yield return (GenerateUserLink(obj, display, ParsingExtension.ResolveEntity(entity.DisplayText))); break; } } } }
private static IEnumerable <Inline> GenerateInlines([CanBeNull] DependencyObject obj, [CanBeNull] string text, [CanBeNull] IEnumerable <TwitterEntity> entities) { if (entities == null) { foreach (var inline in GenerateInlines(obj, text)) { yield return(inline); } yield break; } foreach (var description in TextEntityResolver.ParseText(text, entities)) { if (description.Entity == null) { yield return(GenerateText(description.Text)); } else { var entity = description.Entity; if (entity is TwitterHashtagEntity he) { yield return(GenerateHashtagLink(obj, he.DisplayText)); } else if (entity is TwitterSymbolEntity se) { yield return(GenerateSymbolLink(obj, se.DisplayText)); } else if (entity is TwitterMediaEntity me) { yield return(GenerateLink(obj, me.DisplayText, ParsingExtension.ResolveEntity(me.ExpandedUrl))); } else if (entity is TwitterUrlEntity ue) { yield return(GenerateLink(obj, ue.DisplayText, ParsingExtension.ResolveEntity(ue.ExpandedUrl))); } else if (entity is TwitterUserMentionEntity re) { yield return(GenerateUserLink(obj, re.DisplayText, ParsingExtension.ResolveEntity(re.DisplayText))); } } } }
public string GetEntityAidedText(EntityDisplayMode displayMode = EntityDisplayMode.DisplayText) { try { var builder = new StringBuilder(); var status = this; if (status.RetweetedOriginal != null) { // change target status = status.RetweetedOriginal; } foreach (var description in TextEntityResolver.ParseText(status)) { if (!description.IsEntityAvailable) { builder.Append(description.Text); } else { var entity = description.Entity; switch (entity.EntityType) { case EntityType.Hashtags: builder.Append("#" + entity.DisplayText); break; case EntityType.Urls: // url entity: // display_url: example.com/CUTTED OFF... // original_url => expanded_url: example.com/full_original_url builder.Append(displayMode == EntityDisplayMode.DisplayText ? ParsingExtension.ResolveEntity(entity.DisplayText) : ParsingExtension.ResolveEntity(entity.OriginalUrl)); break; case EntityType.Media: // media entity: // display_url: pic.twitter.com/IMAGE_ID // media_url: pbs.twimg.com/media/ACTUAL_IMAGE_RESOURCE_ID // url: t.co/IMAGE_ID builder.Append( displayMode == EntityDisplayMode.DisplayText ? ParsingExtension.ResolveEntity(entity.DisplayText) : displayMode == EntityDisplayMode.LinkUri ? ParsingExtension.ResolveEntity(entity.DisplayText) : ParsingExtension.ResolveEntity(entity.MediaUrl)); break; case EntityType.UserMentions: builder.Append("@" + entity.DisplayText); break; } } } return(builder.ToString()); } catch (ArgumentOutOfRangeException ex) { var sb = new StringBuilder(); sb.AppendLine("Parse Error! : " + Text); if (Entities == null) { sb.AppendLine("Entities: null"); } else { sb.Append("Entities: "); Entities.OrderBy(e => e.StartIndex) .Select(e => e.StartIndex + "- " + e.EndIndex + " : " + e.DisplayText) .ForEach(s => sb.AppendLine(" " + s)); } throw new ArgumentOutOfRangeException(sb.ToString(), ex); } }
public string GetEntityAidedText(bool showFullUrl = false) { try { var builder = new StringBuilder(); var status = this; if (status.RetweetedOriginal != null) { // change target status = status.RetweetedOriginal; } foreach (var description in TextEntityResolver.ParseText(status)) { if (!description.IsEntityAvailable) { builder.Append(description.Text); } else { var entity = description.Entity; switch (entity.EntityType) { case EntityType.Hashtags: builder.Append("#" + entity.DisplayText); break; case EntityType.Urls: builder.Append(showFullUrl ? ParsingExtension.ResolveEntity(entity.OriginalUrl) : ParsingExtension.ResolveEntity(entity.DisplayText)); break; case EntityType.Media: builder.Append(showFullUrl ? ParsingExtension.ResolveEntity(entity.MediaUrl) : ParsingExtension.ResolveEntity(entity.DisplayText)); break; case EntityType.UserMentions: builder.Append("@" + entity.DisplayText); break; } } } return(builder.ToString()); } catch (ArgumentOutOfRangeException ex) { var sb = new StringBuilder(); sb.AppendLine("Parse Error! : " + Text); if (Entities == null) { sb.AppendLine("Entities: null"); } else { sb.Append("Entities: "); Entities.OrderBy(e => e.StartIndex) .Select(e => e.StartIndex + "- " + e.EndIndex + " : " + e.DisplayText) .ForEach(s => sb.AppendLine(" " + s)); } throw new ArgumentOutOfRangeException(sb.ToString(), ex); } }