diff --git a/ReadSharp.Tests/ReadTests.cs b/ReadSharp.Tests/ReadTests.cs index 5576b1c..e66d2ff 100644 --- a/ReadSharp.Tests/ReadTests.cs +++ b/ReadSharp.Tests/ReadTests.cs @@ -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("", result.Content); Assert.DoesNotContain("

", 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("", 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("", result.Content); Assert.Contains("", result.Content); diff --git a/ReadSharp/IReader.cs b/ReadSharp/IReader.cs index e12266d..8ef5502 100644 --- a/ReadSharp/IReader.cs +++ b/ReadSharp/IReader.cs @@ -9,18 +9,11 @@ namespace ReadSharp /// Reads article content from the given URI. /// /// An URI to extract the content from. - /// if set to true [only body is returned]. - /// if set to true [no headline (h1) is included in generated HTML]. - /// if set to true [deep links with hashes are not transformed to absolute URIs]. + /// The transform options. /// /// An article with extracted content and meta information. /// - /// - Task
Read( - Uri uri, - bool bodyOnly = true, - bool noHeadline = false, - bool useDeepLinks = false - ); + /// + Task
Read(Uri uri, ReadOptions options = null); } } \ No newline at end of file diff --git a/ReadSharp/Models/ReadOptions.cs b/ReadSharp/Models/ReadOptions.cs new file mode 100644 index 0000000..523fd43 --- /dev/null +++ b/ReadSharp/Models/ReadOptions.cs @@ -0,0 +1,44 @@ + +namespace ReadSharp +{ + public class ReadOptions + { + /// + /// Is only the body is returned + /// + /// + /// true if [has only body]; otherwise, false. + /// + public bool HasOnlyBody { get; set; } + + /// + /// Is no headline (h1) is included in generated HTML + /// + /// + /// true if [has no headline]; otherwise, false. + /// + public bool HasNoHeadline { get; set; } + + /// + /// Are deep links with hashes not transformed to absolute URIs + /// + /// + /// true if [use deep links]; otherwise, false. + /// + public bool UseDeepLinks { get; set; } + + /// + /// Creates the default options. + /// + /// + public static ReadOptions CreateDefault() + { + return new ReadOptions() + { + HasOnlyBody = true, + HasNoHeadline = false, + UseDeepLinks = false + }; + } + } +} diff --git a/ReadSharp/ReadSharp.csproj b/ReadSharp/ReadSharp.csproj index 91bf9fe..cf9b0f3 100644 --- a/ReadSharp/ReadSharp.csproj +++ b/ReadSharp/ReadSharp.csproj @@ -63,9 +63,11 @@ + + diff --git a/ReadSharp/Reader.cs b/ReadSharp/Reader.cs index 50f5d14..a571025 100644 --- a/ReadSharp/Reader.cs +++ b/ReadSharp/Reader.cs @@ -98,23 +98,22 @@ namespace ReadSharp /// Reads article content from the given URI. /// /// An URI to extract the content from. - /// if set to true [only body is returned]. - /// if set to true [no headline (h1) is included in generated HTML]. - /// if set to true [deep links with hashes are not transformed to absolute URIs]. + /// The transform options. /// /// An article with extracted content and meta information. /// - /// - public async Task
Read( - Uri uri, - bool bodyOnly = true, - bool noHeadline = false, - bool useDeepLinks = false) + /// + public async Task
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 /// /// The URI. /// The text stream. - /// if set to true [body only]. - /// if set to true [no headline]. + /// The options. + /// The encoding. /// 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