bodyOnly and noHeadline options for PocketReader.Read
This commit is contained in:
@@ -59,15 +59,17 @@ namespace PocketSharp
|
||||
/// The PocketReader is based on a custom PCL port of NReadability and SgmlReader.
|
||||
/// </summary>
|
||||
/// <param name="uri">An URI.</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].</param>
|
||||
/// <returns>A Pocket article with extracted content and title.</returns>
|
||||
/// <exception cref="PocketRequestException"></exception>
|
||||
public async Task<PocketArticle> Read(Uri uri)
|
||||
public async Task<PocketArticle> Read(Uri uri, bool bodyOnly = true, bool noHeadline = false)
|
||||
{
|
||||
return await Read(new PocketItem()
|
||||
{
|
||||
ID = 0,
|
||||
Uri = uri
|
||||
});
|
||||
}, bodyOnly, noHeadline);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,9 +80,13 @@ namespace PocketSharp
|
||||
/// The PocketReader is based on a custom PCL port of NReadability and SgmlReader.
|
||||
/// </summary>
|
||||
/// <param name="item">The pocket item.</param>
|
||||
/// <returns>A Pocket article with extracted content and title.</returns>
|
||||
/// <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].</param>
|
||||
/// <returns>
|
||||
/// A Pocket article with extracted content and title.
|
||||
/// </returns>
|
||||
/// <exception cref="PocketRequestException"></exception>
|
||||
public async Task<PocketArticle> Read(PocketItem item)
|
||||
public async Task<PocketArticle> Read(PocketItem item, bool bodyOnly = true, bool noHeadline = false)
|
||||
{
|
||||
// initialize transcoder
|
||||
NReadabilityTranscoder transcoder = new NReadabilityTranscoder(
|
||||
@@ -101,11 +107,12 @@ namespace PocketSharp
|
||||
Url = item.Uri.ToString(),
|
||||
DomSerializationParams = new DomSerializationParams()
|
||||
{
|
||||
BodyOnly = true,
|
||||
BodyOnly = bodyOnly,
|
||||
NoHeadline = noHeadline,
|
||||
PrettyPrint = true,
|
||||
DontIncludeContentTypeMetaElement = true,
|
||||
DontIncludeMobileSpecificMetaElements = true,
|
||||
DontIncludeDocTypeMetaElement = true,
|
||||
DontIncludeDocTypeMetaElement = false,
|
||||
DontIncludeGeneratorMetaElement = true
|
||||
}
|
||||
};
|
||||
|
||||
@@ -26,6 +26,23 @@ namespace PocketSharp.Tests
|
||||
Uri = new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation")
|
||||
});
|
||||
|
||||
Assert.DoesNotContain("<!DOCTYPE html>", result.Content);
|
||||
Assert.Contains("<h1>", result.Content);
|
||||
Assert.True(result.Content.Length > 15000);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ReadArticleWithContainerNoHeadlineTest()
|
||||
{
|
||||
PocketArticle result = await reader.Read(new PocketItem()
|
||||
{
|
||||
ID = 99,
|
||||
Uri = new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation")
|
||||
}, false, true);
|
||||
|
||||
Assert.Contains("<!DOCTYPE html>", result.Content);
|
||||
Assert.DoesNotContain("<h1>", result.Content);
|
||||
Assert.True(result.Content.Length > 15000);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
|
||||
|
||||
namespace PocketSharp.Ports.NReadability
|
||||
{
|
||||
public class DomSerializationParams
|
||||
@@ -48,6 +47,10 @@ namespace PocketSharp.Ports.NReadability
|
||||
/// </summary>
|
||||
public bool BodyOnly { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Remove headline of website
|
||||
/// </summary>
|
||||
public bool NoHeadline { get; set; }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,16 @@ namespace PocketSharp.Ports.NReadability
|
||||
}
|
||||
}
|
||||
|
||||
if (domSerializationParams.NoHeadline)
|
||||
{
|
||||
var h1 = document.Root.GetElementsByTagName("h1").FirstOrDefault();
|
||||
|
||||
if (h1 != null)
|
||||
{
|
||||
result = result.Replace(h1.ToString(), "");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user