diff --git a/src/Markdig.Tests/Specs/PipeTableSpecs.md b/src/Markdig.Tests/Specs/PipeTableSpecs.md index e5da16cd..5f239128 100644 --- a/src/Markdig.Tests/Specs/PipeTableSpecs.md +++ b/src/Markdig.Tests/Specs/PipeTableSpecs.md @@ -38,18 +38,27 @@ a | b ```````````````````````````````` -While the following would be considered as a plain paragraph with a list item: +The following is also considered as a table, even if the second line starts like a list: ```````````````````````````````` example a | b - | - 0 | 1 . -

a | b

- + + + + + + + + + + + + + +
ab
01
```````````````````````````````` A pipe table with only one header row is allowed: diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index b17796e3..492fff80 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -16358,7 +16358,7 @@ namespace Markdig.Tests TestParser.TestSpec("a | b\n-- | -\n0 | 1", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
", "pipetables"); } } - // While the following would be considered as a plain paragraph with a list item: + // The following is also considered as a table, even if the second line starts like a list: [TestFixture] public partial class TestExtensionsPipeTable { @@ -16374,14 +16374,23 @@ namespace Markdig.Tests // 0 | 1 // // Should be rendered as: - //

a | b

- // + // + // + // + // + // + // + // + // + // + // + // + // + // + //
ab
01
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions Pipe Table"); - TestParser.TestSpec("a | b\n- | -\n0 | 1", "

a | b

\n", "pipetables"); + TestParser.TestSpec("a | b\n- | -\n0 | 1", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
", "pipetables"); } } // A pipe table with only one header row is allowed: diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs index 37939f74..8d5da215 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs @@ -50,7 +50,7 @@ namespace Markdig.Extensions.AutoIdentifiers headingBlockParser.Closed -= HeadingBlockParser_Closed; headingBlockParser.Closed += HeadingBlockParser_Closed; } - var paragraphBlockParser = pipeline.BlockParsers.Find(); + var paragraphBlockParser = pipeline.BlockParsers.FindExact(); if (paragraphBlockParser != null) { // Install a hook on the ParagraphBlockParser when a HeadingBlock is actually processed as a Setex heading diff --git a/src/Markdig/Extensions/Tables/GridTableParser.cs b/src/Markdig/Extensions/Tables/GridTableParser.cs index b0a651e5..c78831d7 100644 --- a/src/Markdig/Extensions/Tables/GridTableParser.cs +++ b/src/Markdig/Extensions/Tables/GridTableParser.cs @@ -208,7 +208,7 @@ namespace Markdig.Extensions.Tables // with the 2 slices if (gridTable.Count == 0) { - var parser = processor.Parsers.Find(); + var parser = processor.Parsers.FindExact(); // Discard the grid table var parent = gridTable.Parent; processor.Discard(gridTable); diff --git a/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs b/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs new file mode 100644 index 00000000..3a327d04 --- /dev/null +++ b/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs @@ -0,0 +1,69 @@ +// 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 Markdig.Helpers; +using Markdig.Parsers; +using Markdig.Syntax; + +namespace Markdig.Extensions.Tables +{ + /// + /// This block parsers for pipe tables is used to by-pass list items that could start by a single '-' + /// and would disallow to detect a pipe tables at inline parsing time, so we are basically forcing a line + /// that starts by a '-' and have at least a '|' (and have optional spaces) and is a continuation of a + /// paragraph. + /// + /// + public class PipeTableBlockParser : BlockParser + { + /// + /// Initializes a new instance of the class. + /// + public PipeTableBlockParser() + { + OpeningCharacters = new[] {'-'}; + } + + public override BlockState TryOpen(BlockProcessor processor) + { + // Only if we have already a paragraph + var paragraph = processor.CurrentBlock as ParagraphBlock; + if (processor.IsCodeIndent || paragraph == null) + { + return BlockState.None; + } + + // We require at least a pipe (and we allow only : - | and space characters) + var line = processor.Line; + var countPipe = 0; + while (true) + { + var c = line.NextChar(); + if (c == '\0') + { + if (countPipe > 0) + { + // Mark the paragraph as open (important, otherwise we would have an infinite loop) + paragraph.AppendLine(ref processor.Line, processor.Column, processor.LineIndex); + paragraph.IsOpen = true; + return BlockState.BreakDiscard; + } + + return BlockState.None; + } + + if (c.IsSpace() || c == '-' || c == '|' || c == ':') + { + if (c == '|') + { + countPipe++; + } + continue; + } + + return BlockState.None; + } + } + } +} \ No newline at end of file diff --git a/src/Markdig/Extensions/Tables/PipeTableExtension.cs b/src/Markdig/Extensions/Tables/PipeTableExtension.cs index 0a7de0d1..e29cc8df 100644 --- a/src/Markdig/Extensions/Tables/PipeTableExtension.cs +++ b/src/Markdig/Extensions/Tables/PipeTableExtension.cs @@ -28,6 +28,10 @@ namespace Markdig.Extensions.Tables public void Setup(MarkdownPipelineBuilder pipeline) { + if (!pipeline.BlockParsers.Contains()) + { + pipeline.BlockParsers.Insert(0, new PipeTableBlockParser()); + } if (!pipeline.InlineParsers.Contains()) { pipeline.InlineParsers.InsertBefore(new PipeTableParser(Options));