-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contributes to #68
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using AngleSharp.Dom; | ||
|
||
namespace Html2Markdown | ||
{ | ||
public class ContentNodeList | ||
{ | ||
private INodeList _childNodes; | ||
|
||
public ContentNodeList(INodeList childNodes) | ||
{ | ||
this._childNodes = childNodes; | ||
} | ||
|
||
public int Size() | ||
{ | ||
return _childNodes.Length; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using AngleSharp.Dom.Html; | ||
using AngleSharp.Parser.Html; | ||
|
||
namespace Html2Markdown { | ||
public class HtmlGraph : IContentGraph | ||
{ | ||
private IHtmlDocument _document; | ||
|
||
public HtmlGraph(string html) | ||
{ | ||
var parser = new HtmlParser(); | ||
this._document = parser.Parse(html); | ||
|
||
if (HasChildren()) { | ||
ChildNodes = new ContentNodeList(_document.Body.ChildNodes); | ||
} | ||
} | ||
|
||
public ContentNodeList ChildNodes { get; private set; } | ||
public bool HasChildren() | ||
{ | ||
return _document.Body.HasChildNodes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Html2Markdown { | ||
public interface IContentGraph | ||
{ | ||
ContentNodeList ChildNodes { get; } | ||
|
||
bool HasChildren(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace Html2Markdown { | ||
|
||
[TestFixture] | ||
public class HtmlContentGraphTest | ||
{ | ||
[Test] | ||
public void HtmlContentGraph_ImplementsIContentGraph() { | ||
var graph = CreateGraph(""); | ||
|
||
Assert.That(graph, Is.InstanceOf<IContentGraph>()); | ||
} | ||
|
||
[Test] | ||
public void HasChildren_WhenHtmlLoadedThatHasChildren_ThenShouldReturnTrue() { | ||
const string html = @"<p>some content</p>"; | ||
var graph = CreateGraph(html); | ||
|
||
Assert.That(graph.HasChildren(), Is.True); | ||
} | ||
|
||
[Test] | ||
public void ChildNodes_WhenHtmlLoadedHasChildren_ThenShouldReturnNodes() { | ||
const string html = @"<p>some content</p>"; | ||
var graph = CreateGraph(html); | ||
|
||
Assert.That(graph.ChildNodes, Is.InstanceOf<ContentNodeList>()); | ||
} | ||
|
||
[Test] | ||
public void ChildNodes_WhenHtmlLoadedHasOneChild_ThenChildNodesSizeShouldBeOne() { | ||
const string html = @"<p>some content</p>"; | ||
var graph = CreateGraph(html); | ||
|
||
Assert.That(graph.ChildNodes.Size(), Is.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void ChildNodes_WhenHtmlLoadedHasTwoChild_ThenChildNodesSizeShouldBeTwo() { | ||
const string html = @"<p>some content</p><p>some more content</p>"; | ||
var graph = CreateGraph(html); | ||
|
||
Assert.That(graph.ChildNodes.Size(), Is.EqualTo(2)); | ||
} | ||
|
||
private IContentGraph CreateGraph(string html) | ||
{ | ||
return new HtmlGraph(html); | ||
} | ||
} | ||
} |