Ejemplo n.º 1
0
        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));
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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 user mention in a status message, description or similar.
 /// </summary>
 /// <param name="text">The text being replaced.</param>
 /// <param name="mention"></param>
 public override string FormatMention(string text, TwitterMentionEntity mention)
 {
     return(Mentions == null?base.FormatMention(text, mention) : Mentions(text, mention));
 }
 /// <summary>
 /// Formats a user mention in a status message, description or similar.
 /// </summary>
 /// <param name="text">The text being replaced.</param>
 /// <param name="mention"></param>
 public virtual string FormatMention(string text, TwitterMentionEntity mention) {
     return "<a href=\"https://twitter.com/" + mention.ScreenName + "\">@" + mention.ScreenName + "</a>";
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Formats a user mention in a status message, description or similar.
 /// </summary>
 /// <param name="text">The text being replaced.</param>
 /// <param name="mention"></param>
 public virtual string FormatMention(string text, TwitterMentionEntity mention)
 {
     return("<a href=\"https://twitter.com/" + mention.ScreenName + "\">@" + mention.ScreenName + "</a>");
 }