mirror of
https://github.com/xoofx/markdig.git
synced 2026-11-16 17:30:52 +00:00
Allow a pipe table header to be separated by a single -|- instead of at least -- | -
This commit is contained in:
@@ -38,18 +38,27 @@ a | b
|
||||
</table>
|
||||
````````````````````````````````
|
||||
|
||||
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
|
||||
.
|
||||
<p>a | b</p>
|
||||
<ul>
|
||||
<li>| -
|
||||
0 | 1</li>
|
||||
</ul>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>a</th>
|
||||
<th>b</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>0</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
````````````````````````````````
|
||||
|
||||
A pipe table with only one header row is allowed:
|
||||
|
||||
@@ -16358,7 +16358,7 @@ namespace Markdig.Tests
|
||||
TestParser.TestSpec("a | b\n-- | -\n0 | 1", "<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>1</td>\n</tr>\n</tbody>\n</table>", "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:
|
||||
// <p>a | b</p>
|
||||
// <ul>
|
||||
// <li>| -
|
||||
// 0 | 1</li>
|
||||
// </ul>
|
||||
// <table>
|
||||
// <thead>
|
||||
// <tr>
|
||||
// <th>a</th>
|
||||
// <th>b</th>
|
||||
// </tr>
|
||||
// </thead>
|
||||
// <tbody>
|
||||
// <tr>
|
||||
// <td>0</td>
|
||||
// <td>1</td>
|
||||
// </tr>
|
||||
// </tbody>
|
||||
// </table>
|
||||
|
||||
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, "Extensions Pipe Table");
|
||||
TestParser.TestSpec("a | b\n- | -\n0 | 1", "<p>a | b</p>\n<ul>\n<li>| -\n0 | 1</li>\n</ul>", "pipetables");
|
||||
TestParser.TestSpec("a | b\n- | -\n0 | 1", "<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>0</td>\n<td>1</td>\n</tr>\n</tbody>\n</table>", "pipetables");
|
||||
}
|
||||
}
|
||||
// A pipe table with only one header row is allowed:
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Markdig.Extensions.AutoIdentifiers
|
||||
headingBlockParser.Closed -= HeadingBlockParser_Closed;
|
||||
headingBlockParser.Closed += HeadingBlockParser_Closed;
|
||||
}
|
||||
var paragraphBlockParser = pipeline.BlockParsers.Find<ParagraphBlockParser>();
|
||||
var paragraphBlockParser = pipeline.BlockParsers.FindExact<ParagraphBlockParser>();
|
||||
if (paragraphBlockParser != null)
|
||||
{
|
||||
// Install a hook on the ParagraphBlockParser when a HeadingBlock is actually processed as a Setex heading
|
||||
|
||||
@@ -208,7 +208,7 @@ namespace Markdig.Extensions.Tables
|
||||
// with the 2 slices
|
||||
if (gridTable.Count == 0)
|
||||
{
|
||||
var parser = processor.Parsers.Find<ParagraphBlockParser>();
|
||||
var parser = processor.Parsers.FindExact<ParagraphBlockParser>();
|
||||
// Discard the grid table
|
||||
var parent = gridTable.Parent;
|
||||
processor.Discard(gridTable);
|
||||
|
||||
69
src/Markdig/Extensions/Tables/PipeTableBlockParser.cs
Normal file
69
src/Markdig/Extensions/Tables/PipeTableBlockParser.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <seealso cref="Markdig.Parsers.BlockParser" />
|
||||
public class PipeTableBlockParser : BlockParser
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PipeTableBlockParser"/> class.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,10 @@ namespace Markdig.Extensions.Tables
|
||||
|
||||
public void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
if (!pipeline.BlockParsers.Contains<PipeTableBlockParser>())
|
||||
{
|
||||
pipeline.BlockParsers.Insert(0, new PipeTableBlockParser());
|
||||
}
|
||||
if (!pipeline.InlineParsers.Contains<PipeTableParser>())
|
||||
{
|
||||
pipeline.InlineParsers.InsertBefore<EmphasisInlineParser>(new PipeTableParser(Options));
|
||||
|
||||
Reference in New Issue
Block a user