コード例 #1
0
ファイル: StatusViewModel.cs プロジェクト: kb10uy/Kbtter3
        internal void AnalyzeText()
        {
            _Text = _Text
                .Replace("&", "&")
                .Replace("&lt;", "<")
                .Replace("&gt;", ">");
            TextElements = new List<StatusElement>();

            var el = new List<EntityInfo>();
            if (origin.Entities != null)
            {
                if (origin.Entities.Urls != null) el.AddRange(origin.Entities.Urls.Select(p => new EntityInfo { Indices = p.Indices, Text = p.DisplayUrl, Infomation = p.ExpandedUrl.ToString(), Type = "Url" }));
                if (origin.Entities.Media != null) el.AddRange(origin.Entities.Media.Select(p => new EntityInfo { Indices = p.Indices, Text = p.DisplayUrl, Infomation = p.ExpandedUrl.ToString(), Type = "Media" }));
                if (origin.Entities.UserMentions != null) el.AddRange(origin.Entities.UserMentions.Select(p => new EntityInfo { Indices = p.Indices, Text = "@" + p.ScreenName, Infomation = p.ScreenName, Type = "Mention" }));
                if (origin.Entities.HashTags != null) el.AddRange(origin.Entities.HashTags.Select(p => new EntityInfo { Indices = p.Indices, Text = "#" + p.Text, Infomation = p.Text, Type = "Hashtag" }));
                el.Sort((x, y) => x.Indices[0].CompareTo(y.Indices[0]));
            }
            int n = 0;
            string s = _Text;
            foreach (var i in el)
            {
                i.Indices[0] = i.Indices[0] >= s.Length ? s.Length - 1 : i.Indices[0];
                var ssi = i.Indices[0] - n;
                ssi = (i.Indices[0] + ssi > s.Length) ? (s.Length - n) : ssi;
                TextElements.Add(new StatusElement { Text = s.Substring(n, ssi), Type = "None" });
                TextElements.Add(new StatusElement { Text = i.Text, Infomation = i.Infomation, Type = i.Type });
                n = (i.Indices[1] >= s.Length - 1) ? s.Length - 1 : i.Indices[1];
            }
            if (n < s.Length - 1) TextElements.Add(new StatusElement { Text = s.Substring(n), Type = "None" });
        }
コード例 #2
0
        private void AnalyzeTextElements()
        {
            TextElements = new ObservableSynchronizedCollection<StatusTextElement>();
            var l = new List<Tuple<int[], StatusTextElement>>();

            if (Source.Entities != null)
            {
                if (Source.Entities.Urls != null)
                    foreach (var i in Source.Entities.Urls)
                    {
                        //Text = Text.Replace(i.Url.ToString(), i.DisplayUrl.ToString());
                        var e = new StatusTextElement();
                        e.Original = i.Url.ToString();
                        e.Action = main.View.OpenInDefault;
                        e.Type = StatusTextElementType.Uri;
                        e.Link = i.ExpandedUrl;
                        e.Surface = i.DisplayUrl;
                        l.Add(new Tuple<int[], StatusTextElement>(i.Indices, e));
                    }
                /*
                //互換性上の理由で画像がUrlとMedia両方で送られてくるらしいので暫定的に削除
                if (Source.Entities.Media != null)
                    foreach (var i in Source.Entities.Media)
                    {
                        //Text = Text.Replace(i.Url.ToString(), i.DisplayUrl.ToString());
                        var e = new StatusTextElement();
                        e.Original = i.Url.ToString();
                        e.Action = main.View.OpenInDefault;
                        e.Type = StatusTextElementType.Media;
                        e.Link = i.ExpandedUrl;
                        e.Surface = i.DisplayUrl;
                        l.Add(new Tuple<int[], StatusTextElement>(i.Indices, e));
                    }
                */
                if (Source.Entities.UserMentions != null)
                    foreach (var i in Source.Entities.UserMentions)
                    {
                        var e = new StatusTextElement();
                        e.Action = async (p) =>
                        {
                            var user = await Kbtter.Token.Users.ShowAsync(id => i.Id);
                            Kbtter.AddUserToUsersList(user);
                            main.View.Notify(user.Name + "さんの情報");
                            main.View.ChangeToUser();
                        };
                        e.Type = StatusTextElementType.User;
                        e.Link = new Uri("https://twitter.com/" + i.ScreenName);
                        e.Surface = "@" + i.ScreenName;
                        e.Original = e.Surface;
                        l.Add(new Tuple<int[], StatusTextElement>(i.Indices, e));
                    }

                if (Source.Entities.HashTags != null)
                    foreach (var i in Source.Entities.HashTags)
                    {
                        var e = new StatusTextElement();
                        e.Action = (p) =>
                        {
                            main.View.ChangeToSearch();
                            main.View.SearchText = "#" + i.Text;
                            Kbtter.Search("#" + i.Text);
                        };
                        e.Type = StatusTextElementType.Hashtag;
                        e.Link = new Uri("https://twitter.com/search?q=%23" + i.Text);
                        e.Surface = "#" + i.Text;
                        e.Original = e.Surface;
                        l.Add(new Tuple<int[], StatusTextElement>(i.Indices, e));
                    }

                l.Sort((x, y) => x.Item1[0].CompareTo(y.Item1[0]));
            }

            int le = 0;
            foreach (var i in l)
            {
                var el = i.Item1[1] - i.Item1[0];
                var ntl = i.Item1[0] - le;
                if (ntl != 0)
                {
                    var nt = Text.Substring(le, ntl);
                    nt = nt
                        .Replace("&lt;", "<")
                        .Replace("&gt;", ">")
                        .Replace("&amp;", "&");
                    TextElements.Add(new StatusTextElement { Surface = nt, Type = StatusTextElementType.None });
                }
                TextElements.Add(i.Item2);
                le = i.Item1[1];
            }
            //foreach (var i in l) Text = Text.Replace(i.Item2.Original, i.Item2.Surface);
            if (Text.Length > le - 1)
            {
                var ls = Text.Substring(le);
                ls = ls
                        .Replace("&lt;", "<")
                        .Replace("&gt;", ">")
                        .Replace("&amp;", "&");
                TextElements.Add(new StatusTextElement { Surface = ls, Type = StatusTextElementType.None });
            }
        }