add ReadOptions

This commit is contained in:
2013-12-25 21:05:52 +01:00
parent d199c12457
commit 5c73d51068
5 changed files with 79 additions and 32 deletions
+13 -3
View File
@@ -30,7 +30,11 @@ namespace ReadSharp.Tests
[Fact]
public async Task ReadArticleWithContainerNoHeadlineTest()
{
Article result = await reader.Read(new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation"), false, true);
Article result = await reader.Read(new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation"), new ReadOptions()
{
HasNoHeadline = true,
HasOnlyBody = false
});
Assert.Contains("<!DOCTYPE html>", result.Content);
Assert.DoesNotContain("<h1>", result.Content);
@@ -78,11 +82,17 @@ namespace ReadSharp.Tests
[Fact]
public async Task DoesUseDeepLinksWork()
{
Article result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), true, false, true);
Article result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), new ReadOptions()
{
UseDeepLinks = true
});
Assert.Contains("<a href=\"#Browser_compatibility\">", result.Content);
result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), true, false, false);
result = await reader.Read(new Uri("https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering"), new ReadOptions()
{
UseDeepLinks = false
});
Assert.DoesNotContain("<a href=\"#Browser_compatibility\">", result.Content);
Assert.Contains("<a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering#Browser_compatibility\">", result.Content);
+3 -10
View File
@@ -9,18 +9,11 @@ namespace ReadSharp
/// Reads article content from the given URI.
/// </summary>
/// <param name="uri">An URI to extract the content from.</param>
/// <param name="bodyOnly">if set to <c>true</c> [only body is returned].</param>
/// <param name="noHeadline">if set to <c>true</c> [no headline (h1) is included in generated HTML].</param>
/// <param name="useDeepLinks">if set to <c>true</c> [deep links with hashes are not transformed to absolute URIs].</param>
/// <param name="options">The transform options.</param>
/// <returns>
/// An article with extracted content and meta information.
/// </returns>
/// <exception cref="Exception"></exception>
Task<Article> Read(
Uri uri,
bool bodyOnly = true,
bool noHeadline = false,
bool useDeepLinks = false
);
/// <exception cref="ReadException"></exception>
Task<Article> Read(Uri uri, ReadOptions options = null);
}
}
+44
View File
@@ -0,0 +1,44 @@
namespace ReadSharp
{
public class ReadOptions
{
/// <summary>
/// Is only the body is returned
/// </summary>
/// <value>
/// <c>true</c> if [has only body]; otherwise, <c>false</c>.
/// </value>
public bool HasOnlyBody { get; set; }
/// <summary>
/// Is no headline (h1) is included in generated HTML
/// </summary>
/// <value>
/// <c>true</c> if [has no headline]; otherwise, <c>false</c>.
/// </value>
public bool HasNoHeadline { get; set; }
/// <summary>
/// Are deep links with hashes not transformed to absolute URIs
/// </summary>
/// <value>
/// <c>true</c> if [use deep links]; otherwise, <c>false</c>.
/// </value>
public bool UseDeepLinks { get; set; }
/// <summary>
/// Creates the default options.
/// </summary>
/// <returns></returns>
public static ReadOptions CreateDefault()
{
return new ReadOptions()
{
HasOnlyBody = true,
HasNoHeadline = false,
UseDeepLinks = false
};
}
}
}
+2
View File
@@ -63,9 +63,11 @@
<Compile Include="IReader.cs" />
<Compile Include="Models\Article.cs" />
<Compile Include="Models\ArticleImage.cs" />
<Compile Include="Models\ReadOptions.cs" />
<Compile Include="Reader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Models\Response.cs" />
<Compile Include="ReadException.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Threading.Tasks">
+17 -19
View File
@@ -98,23 +98,22 @@ namespace ReadSharp
/// Reads article content from the given URI.
/// </summary>
/// <param name="uri">An URI to extract the content from.</param>
/// <param name="bodyOnly">if set to <c>true</c> [only body is returned].</param>
/// <param name="noHeadline">if set to <c>true</c> [no headline (h1) is included in generated HTML].</param>
/// <param name="useDeepLinks">if set to <c>true</c> [deep links with hashes are not transformed to absolute URIs].</param>
/// <param name="options">The transform options.</param>
/// <returns>
/// An article with extracted content and meta information.
/// </returns>
/// <exception cref="Exception"></exception>
public async Task<Article> Read(
Uri uri,
bool bodyOnly = true,
bool noHeadline = false,
bool useDeepLinks = false)
/// <exception cref="ReadException"></exception>
public async Task<Article> Read(Uri uri, ReadOptions options = null)
{
Response response;
TranscodingResult transcodingResult;
Encoding encoding;
if (options == null)
{
options = ReadOptions.CreateDefault();
}
// make async request
try
{
@@ -127,7 +126,7 @@ namespace ReadSharp
}
// handle deep links
if (useDeepLinks)
if (options.UseDeepLinks)
{
_transcoder.AnchorHrefTranformer = ReverseDeepLinks;
}
@@ -139,7 +138,7 @@ namespace ReadSharp
// readability
try
{
transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline);
transcodingResult = ExtractReadableInformation(uri, response.Stream, options);
encoding = _encoder.GetEncodingFromString(transcodingResult.Charset);
@@ -149,7 +148,7 @@ namespace ReadSharp
||
!String.Equals(response.Charset, transcodingResult.Charset, StringComparison.OrdinalIgnoreCase)))
{
transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline, encoding);
transcodingResult = ExtractReadableInformation(uri, response.Stream, options, encoding);
}
}
catch (Exception exc)
@@ -195,14 +194,13 @@ namespace ReadSharp
/// </summary>
/// <param name="uri">The URI.</param>
/// <param name="textStream">The text stream.</param>
/// <param name="bodyOnly">if set to <c>true</c> [body only].</param>
/// <param name="noHeadline">if set to <c>true</c> [no headline].</param>
/// <param name="options">The options.</param>
/// <param name="encoding">The encoding.</param>
/// <returns></returns>
protected TranscodingResult ExtractReadableInformation(
Uri uri,
Stream textStream,
bool bodyOnly = true,
bool noHeadline = false,
ReadOptions options,
Encoding encoding = null)
{
// response stream to text
@@ -216,8 +214,8 @@ namespace ReadSharp
Url = uri.ToString(),
DomSerializationParams = new DomSerializationParams()
{
BodyOnly = bodyOnly,
NoHeadline = noHeadline,
BodyOnly = options.HasOnlyBody,
NoHeadline = options.HasNoHeadline,
PrettyPrint = true,
DontIncludeContentTypeMetaElement = true,
DontIncludeMobileSpecificMetaElements = true,
@@ -282,7 +280,7 @@ namespace ReadSharp
}
catch (HttpRequestException exc)
{
throw new ReadException(exc.Message, exc);
throw new ReadException(exc.Message);
}
// validate HTTP response