Exemplo n.º 1
0
        private void SetHeadersValue(StringParameters strParams, string tagName, bool value)
        {
            switch (tagName.ToLower())
            {
            case "h1":
                strParams.H1 = value;
                return;

            case "h2":
                strParams.H2 = value;
                return;

            case "h3":
                strParams.H3 = value;
                return;

            case "h4":
                strParams.H4 = value;
                return;

            case "h5":
                strParams.H5 = value;
                return;

            case "h6":
                strParams.H6 = value;
                return;
            }
        }
Exemplo n.º 2
0
        private double GetFontSize(StringParameters strParams)
        {
            if (strParams.H1)
            {
                return(_appSettings.FontSizeH1);
            }

            if (strParams.H2)
            {
                return(_appSettings.FontSizeH2);
            }

            if (strParams.H3)
            {
                return(_appSettings.FontSizeH3);
            }

            if (strParams.H4)
            {
                return(_appSettings.FontSizeH4);
            }

            if (strParams.H5)
            {
                return(_appSettings.FontSizeH5);
            }

            if (strParams.H6)
            {
                return(_appSettings.FontSizeH6);
            }

            return(_appSettings.FontSize);
        }
Exemplo n.º 3
0
 private bool IsHtmlHeader(StringParameters strParams)
 {
     return(strParams.H1 ||
            strParams.H2 ||
            strParams.H3 ||
            strParams.H4 ||
            strParams.H5 ||
            strParams.H6);
 }
Exemplo n.º 4
0
        public List <Paragraph> GetParagraphs(string html, out IList <Image> images)
        {
            if (html == null)
            {
                images = new Image[0];
                return(new List <Paragraph>());
            }

            var allParagraphs = new List <Paragraph>();

            try
            {
                var parser  = new HtmlParser();
                var lexemes = parser.Parse(html);

                var paragraph = new Paragraph {
                    TextAlignment = _appSettings.TextAlignment
                };
                allParagraphs.Add(paragraph);

                for (int lexemeIndex = 0; lexemeIndex < lexemes.Length; lexemeIndex++)
                {
                    var lexeme = lexemes[lexemeIndex];

                    var literalLexeme = lexeme as LiteralLexeme;
                    if (literalLexeme != null)
                    {
                        paragraph.Inlines.Add(new Run {
                            Text = literalLexeme.Text, FontSize = _appSettings.FontSize
                        });
                        continue;
                    }

                    var tagLexeme = (HtmlTagLexeme)lexeme;
                    if (String.Equals(tagLexeme.Name, "br", StringComparison.OrdinalIgnoreCase))
                    {
                        paragraph.Inlines.Add(new LineBreak());
                        continue;
                    }

                    TryAddYoutubeVideo(tagLexeme, paragraph.Inlines);

                    if (String.Equals(tagLexeme.Name, "img", StringComparison.OrdinalIgnoreCase))
                    {
                        AddImage(paragraph.Inlines, tagLexeme, null);
                        continue;
                    }

                    if (tagLexeme.IsOpen && !tagLexeme.IsClose)
                    {
                        //paragraph = new Paragraph { TextAlignment = _appSettings.TextAlignment };
                        //allParagraphs.Add(paragraph);

                        var closeIndex = GetCloseIndex(lexemeIndex, lexemes);
                        if (closeIndex != -1)
                        {
                            var strParams = new StringParameters();
                            AddBeginEnd(paragraph.Inlines, lexemes, lexemeIndex, closeIndex, strParams);
                            lexemeIndex = closeIndex;
                        }
                    }
                }

                images = _allImages;
                return(allParagraphs);
            }
            catch (Exception e)
            {
                _telemetry.TrackException(e);
                images = _allImages;
                return(new List <Paragraph>());
            }
        }
Exemplo n.º 5
0
        private void AddBeginEnd(InlineCollection inlines, ILexeme[] lexemes, int lexemeIndex, int closeIndex, StringParameters strParams)
        {
            var startL = (HtmlTagLexeme)lexemes[lexemeIndex];

            if (String.Equals(startL.Name, "p", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new LineBreak());
            }

            if (String.Equals(startL.Name, "li", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new Run()
                {
                    Text = "• ", FontSize = _appSettings.FontSize
                });
            }

            if (String.Equals(startL.Name, "div", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new LineBreak());
            }

            if (String.Equals(startL.Name, "a", StringComparison.OrdinalIgnoreCase))
            {
                string href;
                if (startL.Attributes.TryGetValue("href", out href))
                {
                    // skip Inoreader AD
                    if (href.StartsWith(@"https://www.inoreader.com/b/", StringComparison.OrdinalIgnoreCase))
                    {
                        return;
                    }

                    Uri tmp;
                    if (Uri.TryCreate(href, UriKind.Absolute, out tmp))
                    {
                        strParams.NavigateUri = href;
                    }
                }
            }

            if (String.Equals(startL.Name, "strong", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Bold = true;
            }

            SetHeadersValue(strParams, startL.Name, true);
            if (String.Equals(startL.Name, "em", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Italic = true;
            }

            if (String.Equals(startL.Name, "i", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Italic = true;
            }

            TryAddYoutubeVideo(startL, inlines);

            for (int index = lexemeIndex + 1; index < closeIndex; index++)
            {
                var lexeme = lexemes[index];

                var literalLexeme = lexeme as LiteralLexeme;
                if (literalLexeme != null)
                {
                    var fontSize = GetFontSize(strParams);

                    if (String.IsNullOrWhiteSpace(strParams.NavigateUri))
                    {
                        var item = new Run {
                            Text = literalLexeme.Text, FontSize = fontSize
                        };

                        if (IsHtmlHeader(strParams))
                        {
                            item.FontWeight = FontWeights.Bold;
                        }

                        if (strParams.Italic)
                        {
                            item.FontStyle = FontStyle.Italic;
                        }

                        inlines.Add(item);
                    }
                    else
                    {
                        var navigateUri = new Uri(strParams.NavigateUri);
                        var hyperlink   = new Hyperlink {
                            NavigateUri = navigateUri
                        };
                        hyperlink.Inlines.Add(new Run {
                            Text = literalLexeme.Text, FontSize = _appSettings.FontSize
                        });
                        inlines.Add(hyperlink);
                        inlines.Add(new Run {
                            Text = " ", FontSize = _appSettings.FontSize
                        });
                    }
                    continue;
                }

                var tagLexeme = (HtmlTagLexeme)lexeme;
                if (String.Equals(tagLexeme.Name, "br", StringComparison.OrdinalIgnoreCase))
                {
                    inlines.Add(new LineBreak());
                    continue;
                }

                if (String.Equals(tagLexeme.Name, "img", StringComparison.OrdinalIgnoreCase) &&
                    tagLexeme.IsOpen &&
                    tagLexeme.IsClose)
                {
                    AddImage(inlines, tagLexeme, strParams.NavigateUri);
                }

                TryAddYoutubeVideo(tagLexeme, inlines);

                if (tagLexeme.IsOpen && !tagLexeme.IsClose)
                {
                    var closeIndex2 = GetCloseIndex(index, lexemes);
                    if (closeIndex2 != -1)
                    {
                        AddBeginEnd(inlines, lexemes, index, closeIndex2, strParams);
                        index = closeIndex2;
                    }
                }
            }

            SetHeadersValue(strParams, startL.Name, false);
            if (String.Equals(startL.Name, "em", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Italic = false;
            }

            if (String.Equals(startL.Name, "i", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Italic = false;
            }

            if (String.Equals(startL.Name, "p", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new LineBreak());
            }

            if (String.Equals(startL.Name, "li", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new LineBreak());
            }

            if (String.Equals(startL.Name, "div", StringComparison.OrdinalIgnoreCase))
            {
                inlines.Add(new LineBreak());
            }

            if (String.Equals(startL.Name, "a", StringComparison.OrdinalIgnoreCase))
            {
                strParams.NavigateUri = null;
            }

            if (String.Equals(startL.Name, "strong", StringComparison.OrdinalIgnoreCase))
            {
                strParams.Bold = false;
            }
        }