Esempio n. 1
0
		public async Task AddAsync(StreamItem item)
		{
			if (_items.Value.Any(t => t.Id == item.Id))
				return;

			var folderName = Guid.NewGuid().ToString("N");

			var parser = new HtmlParser();
			var plainText = parser.GetPlainText(item.Content, 200);

			var cacheFolder = await _rootCacheFolder.CreateFolderAsync(CacheFolderName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
			var folder = await cacheFolder.CreateFolderAsync(folderName).AsTask().ConfigureAwait(false);

			var newHtml = await SaveImagesAsync(folder, item.Content).ConfigureAwait(false);

			var savedItem = new SavedStreamItem
			{
				Id = item.Id,
				Title = item.Title,
				Published = item.Published,
				WebUri = item.WebUri,
				ShortContent = plainText,
				Content = newHtml,
				ImageFolder = folderName
			};

			_items.Value.Add(savedItem);
			_storageManager.Save(savedItem);
		}
Esempio n. 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();
		}
		public object Convert(object value, Type targetType, object parameter, string language)
		{
			try
			{
				var str = (string) value;
				var parser = new HtmlParser();
				return parser.GetPlainText(str, 200);
			}
			catch (Exception e)
			{
				_telemetryClient.TrackException(e);
				return String.Empty;
			}
		}
Esempio 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>());
            }
        }