From 083fe224b82943859f17e59c43d6a10de1ef2da3 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 13 Jul 2014 15:03:30 +0200 Subject: [PATCH] Option to replace images with placeholders --- .../NReadability/DomSerializationParams.cs | 5 +++++ .../NReadability/NReadabilityTranscoder.cs | 10 +++++++++- README.md | 5 +++++ ReadSharp.Tests/ReadTests.cs | 17 +++++++++++++++++ ReadSharp/Models/ReadOptions.cs | 8 +++++++- ReadSharp/Properties/AssemblyInfo.cs | 4 ++-- ReadSharp/Reader.cs | 11 ++++++----- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/PortablePorts/NReadability/DomSerializationParams.cs b/PortablePorts/NReadability/DomSerializationParams.cs index 6831911..fb0d89c 100644 --- a/PortablePorts/NReadability/DomSerializationParams.cs +++ b/PortablePorts/NReadability/DomSerializationParams.cs @@ -42,6 +42,11 @@ namespace ReadSharp.Ports.NReadability /// public bool DontIncludeGeneratorMetaElement { get; set; } + /// + /// If [true], replace all img-tags with placeholders. + /// + public bool ReplaceImagesWithPlaceholders { get; set; } + /// /// Render complete Website or only the Body /// diff --git a/PortablePorts/NReadability/NReadabilityTranscoder.cs b/PortablePorts/NReadability/NReadabilityTranscoder.cs index 4ecce6b..f7d0125 100644 --- a/PortablePorts/NReadability/NReadabilityTranscoder.cs +++ b/PortablePorts/NReadability/NReadabilityTranscoder.cs @@ -253,6 +253,14 @@ namespace ReadSharp.Ports.NReadability out nextPageUrl); IEnumerable images = transcodedXmlDocument.GetElementsByTagName("img"); + List realImages = images.ToList(); + + if (transcodingInput.DomSerializationParams.ReplaceImagesWithPlaceholders) + { + int i = 1; + images.ForEach(ximage => ximage.AddAfterSelf(new XComment("IMG_" + (i++)))); + images.Remove(); + } string transcodedContent = _sgmlDomSerializer.SerializeDocument( @@ -297,7 +305,7 @@ namespace ReadSharp.Ports.NReadability ExtractedImage = image, NextPageUrl = nextPageUrl, Charset = charset, - Images = images + Images = realImages }; } diff --git a/README.md b/README.md index 671b2d5..715a1ca 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,11 @@ You can pass `HttpOptions` to the `Reader` constructor, which count for all requ - `HttpMessageHandler` **CustomHttpHandler**
Use your own HTTP handler - `int?` **RequestTimeout**
Define a custom timeout _in seconds_, after which requests should cancel +- `bool` **UseMobileUserAgent**
Gets or sets a value indicating whether [use mobile user agent] - `string` **UserAgent**
Override the user agent, which is passed to the destination server +- `string` **UserAgentMobile**
Override the mobile user agent, which is passed to the destination server - `bool` **UseMobileUserAgent**
There are desktop and mobile default user agents. By enabling this property, the mobile user agent is used. _If you pass a custom user agent, this property is ignored!_ +- `int` **MultipageLimit**
Gets or sets the download limit for articles with multiple pages (default: 10) ### ReadOptions @@ -62,6 +65,8 @@ There are also `ReadOptions` available, which are passed on every request: - `bool` **HasNoHeadline**
Removes `

` title from the article - `bool` **UseDeepLinks**
If you check this option, deep-links (containing hashes, e.g. `href="#article"`) are not transformed into absolute URIs - `bool` **PrettyPrint**
Determines whether the HTML output will be formatted +- `bool` **MultipageDownload**
Download all pages for articles with multiple pages (default: false) +- `bool` **ReplaceImagesWithPlaceholders**
If true, replace all img-tags with placeholders ## Article Model diff --git a/ReadSharp.Tests/ReadTests.cs b/ReadSharp.Tests/ReadTests.cs index 93ea536..624b124 100644 --- a/ReadSharp.Tests/ReadTests.cs +++ b/ReadSharp.Tests/ReadTests.cs @@ -52,6 +52,23 @@ namespace ReadSharp.Tests } + [Fact] + public async Task ReadArticleWithImagePlaceholdersTest() + { + ReadOptions options = ReadOptions.CreateDefault(); + options.ReplaceImagesWithPlaceholders = true; + Article result = await reader.Read(new Uri("https://hacks.mozilla.org/2013/12/application-layout-with-css3-flexible-box-module/"), options); + List images = result.Images.ToList(); + + Assert.True(images.Count >= 3); + Assert.True(images[0].Uri.ToString().StartsWith("https://hacks.mozilla.org")); + Assert.True(images[1].Uri.ToString().EndsWith(".gif")); + + Assert.Contains("", result.Content); + Assert.DoesNotContain(" public bool MultipageDownload { get; set; } + /// + /// If true, replace all img-tags with placeholders. + /// + public bool ReplaceImagesWithPlaceholders { get; set; } + /// /// Creates the default options. /// @@ -55,7 +60,8 @@ namespace ReadSharp HasHeadline = false, UseDeepLinks = false, PrettyPrint = false, - MultipageDownload = false + MultipageDownload = false, + ReplaceImagesWithPlaceholders = false }; } } diff --git a/ReadSharp/Properties/AssemblyInfo.cs b/ReadSharp/Properties/AssemblyInfo.cs index 16fee9e..9d0d991 100644 --- a/ReadSharp/Properties/AssemblyInfo.cs +++ b/ReadSharp/Properties/AssemblyInfo.cs @@ -22,5 +22,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("6.1.0")] -[assembly: AssemblyFileVersion("6.1.0")] \ No newline at end of file +[assembly: AssemblyVersion("6.2.0")] +[assembly: AssemblyFileVersion("6.2.0")] \ No newline at end of file diff --git a/ReadSharp/Reader.cs b/ReadSharp/Reader.cs index 099de85..f545015 100644 --- a/ReadSharp/Reader.cs +++ b/ReadSharp/Reader.cs @@ -165,10 +165,10 @@ namespace ReadSharp Title = image.GetAttributeValue("title", null), AlternativeText = image.GetAttributeValue("alt", null) }; - }) - .GroupBy(image => image.Uri) - .Select(g => g.First()) - .Where(image => image.Uri != null); + }); + //.GroupBy(image => image.Uri) + //.Select(g => g.First()) + //.Where(image => image.Uri != null); // get word count and plain text string plainContent; @@ -236,7 +236,8 @@ namespace ReadSharp DontIncludeContentTypeMetaElement = true, DontIncludeMobileSpecificMetaElements = true, DontIncludeDocTypeMetaElement = false, - DontIncludeGeneratorMetaElement = true + DontIncludeGeneratorMetaElement = true, + ReplaceImagesWithPlaceholders = options.ReplaceImagesWithPlaceholders } };