Merge pull request #288 from MihaZupan/rewrite-descendants-search

Rewrite Descendants iteratively
This commit is contained in:
Alexandre Mutel
2019-01-19 19:27:26 +01:00
committed by GitHub
5 changed files with 248 additions and 147 deletions

View File

@@ -58,6 +58,7 @@
</Compile>
<Compile Include="Specs\TestEmphasisPlus.cs" />
<Compile Include="TestEmphasisExtraOptions.cs" />
<Compile Include="TestDescendantsOrder.cs" />
<Compile Include="TestConfigureNewLine.cs" />
<Compile Include="TestHtmlAttributes.cs" />
<Compile Include="TestHtmlHelper.cs" />

View File

@@ -0,0 +1,85 @@
using NUnit.Framework;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using System.Linq;
using System.Collections.Generic;
namespace Markdig.Tests
{
[TestFixture]
public class TestDescendantsOrder
{
[Test]
public void TestSchemas()
{
foreach (var markdown in TestParser.SpecsMarkdown)
{
AssertSameDescendantsOrder(markdown);
}
}
private void AssertSameDescendantsOrder(string markdown)
{
var syntaxTree = Markdown.Parse(markdown, new MarkdownPipelineBuilder().UseAdvancedExtensions().Build());
var descendants_legacy = Descendants_Legacy(syntaxTree).ToList();
var descendants_new = syntaxTree.Descendants().ToList();
Assert.AreEqual(descendants_legacy.Count, descendants_new.Count);
for (int i = 0; i < descendants_legacy.Count; i++)
{
Assert.AreSame(descendants_legacy[i], descendants_new[i]);
}
}
private static IEnumerable<MarkdownObject> Descendants_Legacy(MarkdownObject markdownObject)
{
// TODO: implement a recursiveless method
var block = markdownObject as ContainerBlock;
if (block != null)
{
foreach (var subBlock in block)
{
yield return subBlock;
foreach (var sub in Descendants_Legacy(subBlock))
{
yield return sub;
}
// Visit leaf block that have inlines
var leafBlock = subBlock as LeafBlock;
if (leafBlock?.Inline != null)
{
foreach (var subInline in Descendants_Legacy(leafBlock.Inline))
{
yield return subInline;
}
}
}
}
else
{
var inline = markdownObject as ContainerInline;
if (inline != null)
{
var child = inline.FirstChild;
while (child != null)
{
var next = child.NextSibling;
yield return child;
foreach (var sub in Descendants_Legacy(child))
{
yield return sub;
}
child = next;
}
}
}
}
}
}

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Markdig.Extensions.JiraLinks;
@@ -155,5 +156,20 @@ namespace Markdig.Tests
html = html.Normalize(NormalizationForm.FormKD);
return html;
}
public static readonly string[] SpecsMarkdown;
static TestParser()
{
string assemblyDir = Path.GetDirectoryName(typeof(TestParser).Assembly.Location);
string specsDir = Path.GetFullPath(Path.Combine(assemblyDir, "../../Specs"));
var files = Directory.GetFiles(specsDir).Where(file => file.EndsWith(".md", StringComparison.Ordinal)).ToList();
SpecsMarkdown = new string[files.Count];
for (int i = 0; i < files.Count; i++)
{
SpecsMarkdown[i] = File.ReadAllText(files[i]);
}
}
}
}

View File

@@ -94,25 +94,36 @@ namespace Markdig.Syntax.Inlines
/// <returns>An enumeration of T</returns>
public IEnumerable<T> FindDescendants<T>() where T : Inline
{
var child = FirstChild;
while (child != null)
{
var next = child.NextSibling;
// Fast-path an empty container to avoid allocating a Stack
if (LastChild == null) yield break;
if (child is T)
{
yield return (T)child;
}
Stack<Inline> stack = new Stack<Inline>();
if (child is ContainerInline)
{
foreach (var subChild in ((ContainerInline) child).FindDescendants<T>())
{
yield return subChild;
}
}
child = next;
var child = LastChild;
while (child != null)
{
stack.Push(child);
child = child.PreviousSibling;
}
while (stack.Count > 0)
{
child = stack.Pop();
if (child is T childT)
{
yield return childT;
}
if (child is ContainerInline containerInline)
{
child = containerInline.LastChild;
while (child != null)
{
stack.Push(child);
child = child.PreviousSibling;
}
}
}
}

View File

@@ -1,131 +1,119 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System.Collections.Generic;
using Markdig.Syntax.Inlines;
namespace Markdig.Syntax
{
/// <summary>
/// Extensions for visiting <see cref="Block"/> or <see cref="Inline"/>
/// </summary>
public static class MarkdownObjectExtensions
{
/// <summary>
/// Iterates over the descendant elements for the specified markdown element, including <see cref="Block"/> and <see cref="Inline"/>.
/// </summary>
/// <param name="markdownObject">The markdown object.</param>
/// <returns>An iteration over the descendant elements</returns>
public static IEnumerable<MarkdownObject> Descendants(this MarkdownObject markdownObject)
{
// TODO: implement a recursiveless method
var block = markdownObject as ContainerBlock;
if (block != null)
{
foreach (var subBlock in block)
{
yield return subBlock;
foreach (var sub in subBlock.Descendants())
{
yield return sub;
}
// Visit leaf block that have inlines
var leafBlock = subBlock as LeafBlock;
if (leafBlock?.Inline != null)
{
foreach (var subInline in Descendants(leafBlock.Inline))
{
yield return subInline;
}
}
}
}
else
{
var inline = markdownObject as ContainerInline;
if (inline != null)
{
var child = inline.FirstChild;
while (child != null)
{
var next = child.NextSibling;
yield return child;
foreach (var sub in child.Descendants())
{
yield return sub;
}
child = next;
}
}
}
}
/// <summary>
/// Iterates over the descendant elements for the specified markdown <see cref="Inline" /> element and filters by the type {T}.
/// </summary>
/// <typeparam name="T">Type to use for filtering the descendants</typeparam>
/// <param name="inline">The inline markdown object.</param>
/// <returns>
/// An iteration over the descendant elements
/// </returns>
public static IEnumerable<T> Descendants<T>(this ContainerInline inline) where T : Inline
{
var child = inline.FirstChild;
while (child != null)
{
var next = child.NextSibling;
var childT = child as T;
if (childT != null)
{
yield return childT;
}
var subContainer = child as ContainerInline;
if (subContainer != null)
{
foreach (var sub in subContainer.Descendants<T>())
{
yield return sub;
}
}
child = next;
}
}
/// <summary>
/// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}.
/// </summary>
/// <typeparam name="T">Type to use for filtering the descendants</typeparam>
/// <param name="block">The markdown object.</param>
/// <returns>
/// An iteration over the descendant elements
/// </returns>
public static IEnumerable<T> Descendants<T>(this ContainerBlock block) where T : Block
{
foreach (var subBlock in block)
{
var subBlockT = subBlock as T;
if (subBlockT != null)
{
yield return subBlockT;
}
var subBlockContainer = subBlock as ContainerBlock;
if (subBlockContainer != null)
{
foreach (var sub in subBlockContainer.Descendants<T>())
{
yield return sub;
}
}
}
}
}
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System.Collections.Generic;
using Markdig.Syntax.Inlines;
namespace Markdig.Syntax
{
/// <summary>
/// Extensions for visiting <see cref="Block"/> or <see cref="Inline"/>
/// </summary>
public static class MarkdownObjectExtensions
{
/// <summary>
/// Iterates over the descendant elements for the specified markdown element, including <see cref="Block"/> and <see cref="Inline"/>.
/// <para>The descendant elements are returned in DFS-like order.</para>
/// </summary>
/// <param name="markdownObject">The markdown object.</param>
/// <returns>An iteration over the descendant elements</returns>
public static IEnumerable<MarkdownObject> Descendants(this MarkdownObject markdownObject)
{
// Fast-path an object with no children to avoid allocating Stack objects
if (!(markdownObject is ContainerBlock) && !(markdownObject is ContainerInline)) yield break;
// TODO: A single Stack<(MarkdownObject block, bool push)> when ValueTuples are available
Stack<MarkdownObject> stack = new Stack<MarkdownObject>();
Stack<bool> pushStack = new Stack<bool>();
stack.Push(markdownObject);
pushStack.Push(false);
while (stack.Count > 0)
{
var block = stack.Pop();
if (pushStack.Pop()) yield return block;
if (block is ContainerBlock containerBlock)
{
int subBlockIndex = containerBlock.Count;
while (subBlockIndex-- > 0)
{
var subBlock = containerBlock[subBlockIndex];
if (subBlock is LeafBlock leafBlock)
{
if (leafBlock.Inline != null)
{
stack.Push(leafBlock.Inline);
pushStack.Push(false);
}
}
stack.Push(subBlock);
pushStack.Push(true);
}
}
else if (block is ContainerInline containerInline)
{
var child = containerInline.LastChild;
while (child != null)
{
stack.Push(child);
pushStack.Push(true);
child = child.PreviousSibling;
}
}
}
}
/// <summary>
/// Iterates over the descendant elements for the specified markdown <see cref="Inline" /> element and filters by the type {T}.
/// </summary>
/// <typeparam name="T">Type to use for filtering the descendants</typeparam>
/// <param name="inline">The inline markdown object.</param>
/// <returns>
/// An iteration over the descendant elements
/// </returns>
public static IEnumerable<T> Descendants<T>(this ContainerInline inline) where T : Inline
=> inline.FindDescendants<T>();
/// <summary>
/// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}.
/// </summary>
/// <typeparam name="T">Type to use for filtering the descendants</typeparam>
/// <param name="block">The markdown object.</param>
/// <returns>
/// An iteration over the descendant elements
/// </returns>
public static IEnumerable<T> Descendants<T>(this ContainerBlock block) where T : Block
{
// Fast-path an empty container to avoid allocating a Stack
if (block.Count == 0) yield break;
Stack<Block> stack = new Stack<Block>();
int childrenCount = block.Count;
while (childrenCount-- > 0)
{
stack.Push(block[childrenCount]);
}
while (stack.Count > 0)
{
var subBlock = stack.Pop();
if (subBlock is T subBlockT)
{
yield return subBlockT;
}
if (subBlock is ContainerBlock subBlockContainer)
{
childrenCount = subBlockContainer.Count;
while (childrenCount-- > 0)
{
stack.Push(subBlockContainer[childrenCount]);
}
}
}
}
}
}