Skip to content

Commit

Permalink
Add initial graph implementation
Browse files Browse the repository at this point in the history
Contributes to #68
  • Loading branch information
baynezy committed Nov 21, 2017
1 parent 9801a4c commit 21b19bf
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Html2Markdown/ContentNodeList.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion src/Html2Markdown/Html2Markdown.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;net40;netstandard1.3;netstandard1.4</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.3;netstandard1.4</TargetFrameworks>
<Authors>Simon Baynes</Authors>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/baynezy/Html2Markdown</PackageProjectUrl>
Expand Down
25 changes: 25 additions & 0 deletions src/Html2Markdown/HtmlGraph.cs
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;
}
}
}
8 changes: 8 additions & 0 deletions src/Html2Markdown/IContentGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Html2Markdown {
public interface IContentGraph
{
ContentNodeList ChildNodes { get; }

bool HasChildren();
}
}
53 changes: 53 additions & 0 deletions test/Html2Markdown.Test/HtmlContentGraphTest.cs
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);
}
}
}

0 comments on commit 21b19bf

Please sign in to comment.