Example #1
0
		private async Task<string> SaveImagesAsync(StorageFolder folder, string html)
		{
			var parser = new HtmlParser();
			var lexemes = parser.Parse(html);

			var fixedImages = new List<string>();
			var localHtml = new StringBuilder(html);

			foreach (var lexeme in lexemes.OfType<HtmlTagLexeme>())
			{
				if (!String.Equals(lexeme.Name, "img", StringComparison.OrdinalIgnoreCase))
					continue;

				var src = lexeme.Attributes["src"];
				if (fixedImages.Any(s => String.Equals(s, src, StringComparison.OrdinalIgnoreCase)))
					continue;

				var fileName = await DownloadImageAsync(src, folder).ConfigureAwait(false);
				if (fileName == null)
					continue;

				fixedImages.Add(src);

				var newSrc = String.Format("ms-appdata:///local/{0}/{1}/{2}", CacheFolderName, folder.Name, fileName);
				localHtml.Replace(src, newSrc);
			}

			return localHtml.ToString();
		}
Example #2
0
        private async Task <string> SaveImagesAsync(StorageFolder folder, string html)
        {
            var parser  = new HtmlParser();
            var lexemes = parser.Parse(html);

            var fixedImages = new List <string>();
            var localHtml   = new StringBuilder(html);

            foreach (var lexeme in lexemes.OfType <HtmlTagLexeme>())
            {
                if (!String.Equals(lexeme.Name, "img", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var src = lexeme.Attributes["src"];
                if (fixedImages.Any(s => String.Equals(s, src, StringComparison.OrdinalIgnoreCase)))
                {
                    continue;
                }

                var fileName = await DownloadImageAsync(src, folder).ConfigureAwait(false);

                if (fileName == null)
                {
                    continue;
                }

                fixedImages.Add(src);

                var newSrc = String.Format("ms-appdata:///local/{0}/{1}/{2}", CacheFolderName, folder.Name, fileName);
                localHtml.Replace(src, newSrc);
            }

            return(localHtml.ToString());
        }
Example #3
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>());
            }
        }