extend NReadability to allow output of body only
This commit is contained in:
@@ -16,7 +16,7 @@ namespace PocketSharp.Tests
|
||||
{
|
||||
PocketReader reader = new PocketReader();
|
||||
|
||||
string result = await reader.Read(new Uri("https://github.com/ceee/PocketSharp"));
|
||||
string result = await reader.Read(new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation"));
|
||||
|
||||
Assert.True(result.Length > 0);
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ namespace PocketSharp
|
||||
Url = uri.ToString(),
|
||||
DomSerializationParams = new DomSerializationParams()
|
||||
{
|
||||
BodyOnly = true,
|
||||
PrettyPrint = true,
|
||||
DontIncludeContentTypeMetaElement = true,
|
||||
DontIncludeMobileSpecificMetaElements = true,
|
||||
@@ -82,7 +83,7 @@ namespace PocketSharp
|
||||
|
||||
// process/transcode HTML
|
||||
TranscodingResult transcodingResult = transcoder.Transcode(transcodingInput);
|
||||
|
||||
|
||||
return transcodingResult.ExtractedContent;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,11 @@ namespace NReadability
|
||||
/// </summary>
|
||||
public bool DontIncludeGeneratorMetaElement { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Render complete Website or only the Body
|
||||
/// </summary>
|
||||
public bool BodyOnly { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,15 @@ namespace NReadability
|
||||
/// </summary>
|
||||
/// <param name="document">System.Xml.Linq.XDocument instance containing the DOM to be serialized.</param>
|
||||
/// <param name="domSerializationParams">Contains parameters that modify the behaviour of the output serialization.</param>
|
||||
/// <returns>Serialized representation of the DOM.</returns>
|
||||
/// <param name="bodyOnly">if set to <c>true</c> [returns body only].</param>
|
||||
/// <returns>
|
||||
/// Serialized representation of the DOM.
|
||||
/// </returns>
|
||||
/// <exception cref="System.ArgumentException">
|
||||
/// The document must have a root.
|
||||
/// or
|
||||
/// The document's root must be an html element.
|
||||
/// </exception>
|
||||
public string SerializeDocument(XDocument document, DomSerializationParams domSerializationParams)
|
||||
{
|
||||
if (!domSerializationParams.DontIncludeContentTypeMetaElement
|
||||
@@ -74,6 +82,16 @@ namespace NReadability
|
||||
result = "<!DOCTYPE html>\r\n" + result;
|
||||
}
|
||||
|
||||
if (domSerializationParams.BodyOnly && document.Root != null)
|
||||
{
|
||||
var body = document.Root.GetChildrenByTagName("body").FirstOrDefault();
|
||||
|
||||
if (body != null)
|
||||
{
|
||||
result = body.GetInnerHtml();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Xml.Linq;
|
||||
namespace NReadability
|
||||
{
|
||||
public class TranscodingResult
|
||||
@@ -17,5 +18,7 @@ namespace NReadability
|
||||
public string ExtractedTitle { get; set; }
|
||||
|
||||
public string NextPageUrl { get; set; }
|
||||
|
||||
public XDocument RawDocument { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user