예제 #1
0
        internal static string LinkifiedText(EntityCollection entities, string text)
        {
            if (entities == null || entities.Count == 0)
            {
                return(text);
            }

            string linkedText = text;

            var entitiesSorted = entities.OrderBy(e => e.StartIndex).Reverse();

            foreach (Entity entity in entitiesSorted)
            {
                if (entity is HashTagEntity)
                {
                    HashTagEntity tagEntity = (HashTagEntity)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 UrlEntity)
                {
                    UrlEntity urlEntity = (UrlEntity)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 MentionEntity)
                {
                    MentionEntity mentionEntity = (MentionEntity)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));
                }
            }

            return(linkedText);
        }
예제 #2
0
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
            /// <param name="objectType">Type of the object.</param>
            /// <param name="existingValue">The existing value of object being read.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <returns>The object value.</returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                EntityCollection result = existingValue as EntityCollection;

                if (result == null)
                {
                    result = new EntityCollection();
                }

                int    startDepth = reader.Depth;
                string entityType = string.Empty;
                Entity entity     = null;

                try
                {
                    while (reader.Read() && reader.Depth > startDepth)
                    {
                        if (reader.TokenType == JsonToken.PropertyName && reader.Depth == startDepth + 1)
                        {
                            entityType = (string)reader.Value;
                            continue;
                        }

                        if (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.EndArray)
                        {
                            continue;
                        }

                        switch (entityType)
                        {
                        case "urls":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new UrlEntity();
                            }

                            UrlEntity tue = entity as UrlEntity;
                            if (tue != null)
                            {
                                ReadFieldValue(reader, "url", entity, () => tue.Url);
                                ReadFieldValue(reader, "display_url", entity, () => tue.DisplayUrl);
                                ReadFieldValue(reader, "expanded_url", entity, () => tue.ExpandedUrl);
                            }
                            break;

                        case "user_mentions":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new MentionEntity();
                            }

                            MentionEntity tme = entity as MentionEntity;
                            if (tme != null)
                            {
                                ReadFieldValue(reader, "screen_name", entity, () => tme.ScreenName);
                                ReadFieldValue(reader, "name", entity, () => tme.Name);
                                ReadFieldValue(reader, "id", entity, () => tme.UserId);
                            }
                            break;

                        case "hashtags":
                            if (reader.TokenType == JsonToken.StartObject)
                            {
                                entity = new HashTagEntity();
                            }

                            HashTagEntity the = entity as HashTagEntity;
                            if (the != null)
                            {
                                ReadFieldValue(reader, "text", entity, () => the.Text);
                            }
                            break;

                        case "media":
                            // Move to object start and parse the entity
                            reader.Read();
                            entity = parseMediaEntity(reader);

                            break;
                        }

                        // Read the indicies (for all entities except Media)
                        if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "indices" && entity != null)
                        {
                            reader.Read();
                            reader.Read();
                            entity.StartIndex = Convert.ToInt32((long)reader.Value);
                            reader.Read();
                            entity.EndIndex = Convert.ToInt32((long)reader.Value);
                        }

                        if ((reader.TokenType == JsonToken.EndObject && entity != null) || entity is MediaEntity)
                        {
                            result.Add(entity);
                            entity = null;
                        }
                    }
                }
                catch { }

                return(result);
            }