Option to replace images with placeholders
This commit is contained in:
@@ -42,6 +42,11 @@ namespace ReadSharp.Ports.NReadability
|
||||
/// </summary>
|
||||
public bool DontIncludeGeneratorMetaElement { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If [true], replace all img-tags with placeholders.
|
||||
/// </summary>
|
||||
public bool ReplaceImagesWithPlaceholders { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Render complete Website or only the Body
|
||||
/// </summary>
|
||||
|
||||
@@ -253,6 +253,14 @@ namespace ReadSharp.Ports.NReadability
|
||||
out nextPageUrl);
|
||||
|
||||
IEnumerable<XElement> images = transcodedXmlDocument.GetElementsByTagName("img");
|
||||
List<XElement> 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
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -51,8 +51,11 @@ You can pass `HttpOptions` to the `Reader` constructor, which count for all requ
|
||||
|
||||
- `HttpMessageHandler` **CustomHttpHandler**<br>Use your own HTTP handler
|
||||
- `int?` **RequestTimeout**<br>Define a custom timeout _in seconds_, after which requests should cancel
|
||||
- `bool` **UseMobileUserAgent**<br>Gets or sets a value indicating whether [use mobile user agent]
|
||||
- `string` **UserAgent**<br>Override the user agent, which is passed to the destination server
|
||||
- `string` **UserAgentMobile**<br>Override the mobile user agent, which is passed to the destination server
|
||||
- `bool` **UseMobileUserAgent**<br>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**<br>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**<br>Removes `<h1>` title from the article
|
||||
- `bool` **UseDeepLinks**<br>If you check this option, deep-links (containing hashes, e.g. `href="#article"`) are not transformed into absolute URIs
|
||||
- `bool` **PrettyPrint**<br>Determines whether the HTML output will be formatted
|
||||
- `bool` **MultipageDownload**<br>Download all pages for articles with multiple pages (default: false)
|
||||
- `bool` **ReplaceImagesWithPlaceholders**<br>If true, replace all img-tags with placeholders
|
||||
|
||||
## Article Model
|
||||
|
||||
|
||||
@@ -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<ArticleImage> 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("<!--IMG_1-->", result.Content);
|
||||
Assert.DoesNotContain("<img ", result.Content);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ReadArticleWithNoImagesTest()
|
||||
{
|
||||
|
||||
@@ -43,6 +43,11 @@ namespace ReadSharp
|
||||
/// </value>
|
||||
public bool MultipageDownload { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If true, replace all img-tags with placeholders.
|
||||
/// </summary>
|
||||
public bool ReplaceImagesWithPlaceholders { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the default options.
|
||||
/// </summary>
|
||||
@@ -55,7 +60,8 @@ namespace ReadSharp
|
||||
HasHeadline = false,
|
||||
UseDeepLinks = false,
|
||||
PrettyPrint = false,
|
||||
MultipageDownload = false
|
||||
MultipageDownload = false,
|
||||
ReplaceImagesWithPlaceholders = false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
[assembly: AssemblyVersion("6.2.0")]
|
||||
[assembly: AssemblyFileVersion("6.2.0")]
|
||||
+6
-5
@@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user