Exemplo n.º 1
0
        private async Task <Inline> pA(HtmlNode node)
        {
            bool isContainsStrangeElemets = false; // Специальный флаг, который определяет, использовать ли Hyperlink или Button для отрисовки ссылки

            foreach (HtmlNode n in node.ChildNodes.ToArray())
            {
                if (n.Name != "#text")
                {
                    isContainsStrangeElemets = true;
                    break;
                }
            }

            if (isContainsStrangeElemets)
            {
                InlineUIContainer container = new InlineUIContainer();
                Button            hpButton  = new Button();
                hpButton.Style  = App.Current.Resources["HyperLinkInvisibleButton"] as Style;
                hpButton.Margin = new Thickness(2, 0, 2, 0);

                RichTextBlock contentBlock = new RichTextBlock();
                contentBlock.IsTextSelectionEnabled = false;
                contentBlock.Blocks.Add(await convertNodeToParagraph(node));
                hpButton.Content = contentBlock;

                string link = normalizeUri(node.Attributes["href"].Value);
                UriParser.isInnerLink(node.Attributes["href"].Value); // Ну и нафига это тут?

                hpButton.Click += (s, e) =>
                {
                    UriParser.GoToPage(link, frame);
                };

                container.Child = hpButton;
                return(container);
            }
            else
            {
                Hyperlink     hyperlink   = new Hyperlink();
                List <Inline> parseResult = await convertNodeToListOfInline(node);

                foreach (Inline i in parseResult)
                {
                    hyperlink.Inlines.Add(i);
                }

                string link = normalizeUri(node.Attributes["href"].Value);
                UriParser.isInnerLink(node.Attributes["href"].Value); // Ну и нафига это тут?

                hyperlink.Click += (s, e) =>
                {
                    UriParser.GoToPage(link, frame);
                };
                return(hyperlink);
            }
        }
Exemplo n.º 2
0
        public async Task <List <Post> > getPosts()
        {
            List <Post> resultPosts = new List <Post>();

            if (rootNode == null)
            {
                return(null);
            }

            var articles = rootNode.SelectNodes("//article");

            foreach (HtmlNode article in articles)
            {
                var articleHeader = article.SelectSingleNode(".//header");

                var articleTitle = articleHeader.SelectSingleNode(".//h1/a").InnerText;

                var articleUri = new Uri(articleHeader.SelectSingleNode(".//h1/a")
                                         .Attributes["href"].Value);

                var articleId = Int32.Parse(articleUri
                                            .Segments.Last()
                                            .Replace(".html", String.Empty));

                var articleRating = articleHeader.SelectSingleNode(".//span/i").InnerText;

                var articleAuthor = articleHeader.SelectSingleNode(".//a[@rel]").InnerText;

                var articleAuthorImageUri = articleHeader.SelectSingleNode(".//img")
                                            .Attributes["src"].Value;

                var articleBlog = articleHeader.SelectSingleNode(".//a[@class]").InnerText;

                var articleBlogId = articleHeader.SelectSingleNode(".//*[@class='topic-blog']")
                                    .Attributes["href"].Value;
                articleBlogId = UriParser.getLastPart(articleBlogId);

                var articleBody = await htmlParser.convertNodeToParagraph(
                    article.SelectSingleNode(".//div[@class='topic-content text']"));

                var articleFooter = article.SelectSingleNode(".//footer[@class='topic-footer']");

                var    articleTags_tmp = articleFooter.SelectNodes(".//a[@rel]");
                string articleTags     = "";
                foreach (HtmlNode node in articleTags_tmp)
                {
                    articleTags += node.InnerText + " ";
                }

                var articleDatatime = articleFooter.SelectSingleNode(".//time").InnerText;

                var articleCommentsCount = articleFooter.SelectSingleNode(".//li[@class='topic-info-comments']").InnerText.Trim();

                SoftwareBitmapSource source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(
                    await webManager.getCachedImageAsync(normalizeImageUriDebug(articleAuthorImageUri)));

                resultPosts.Add(new Post
                {
                    id           = articleId,
                    title        = articleTitle,
                    author       = articleAuthor,
                    author_image = source,
                    blog         = " " + articleBlog, // Чтобы не сливался с "в блоге"
                    blog_id      = articleBlogId,
                    rating       = articleRating,
                    text         = articleBody,
                    //text = HtmlEntity.DeEntitize(articleBody).Trim(),
                    tags          = articleTags,
                    datatime      = articleDatatime,
                    commentsCount = articleCommentsCount,
                });
            }

            return(resultPosts);
        }