From 9eb84cf95eaecb11edad85f6899450f8376e184f Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sun, 11 Dec 2016 19:01:49 +0100 Subject: [PATCH] Normalize pipe tables so they contain the same number of columns for all rows --- src/Markdig.Tests/Specs/PipeTableSpecs.md | 32 +++++++++++++ src/Markdig.Tests/Specs/Specs.cs | 45 ++++++++++++++++++- .../Extensions/Tables/PipeTableParser.cs | 3 ++ src/Markdig/Extensions/Tables/Table.cs | 28 ++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/Markdig.Tests/Specs/PipeTableSpecs.md b/src/Markdig.Tests/Specs/PipeTableSpecs.md index 39c6eaac..acddfbb4 100644 --- a/src/Markdig.Tests/Specs/PipeTableSpecs.md +++ b/src/Markdig.Tests/Specs/PipeTableSpecs.md @@ -136,6 +136,7 @@ a | b a b + @@ -147,9 +148,12 @@ a | b 3 4 + 5 + + @@ -548,3 +552,31 @@ Tests trailing spaces after pipes ```````````````````````````````` + +** Normalized columns count ** + +The tables are normalized to the maximum number of columns found in a table + + +```````````````````````````````` example +a | b +-- | - +0 | 1 | 2 +. + + + + + + + + + + + + + + + +
ab
012
+```````````````````````````````` \ No newline at end of file diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index 1deba4d3..826255a5 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -16633,6 +16633,7 @@ namespace Markdig.Tests // // a // b + // // // // @@ -16644,15 +16645,18 @@ namespace Markdig.Tests // // 3 // 4 + // // // // 5 + // + // // // // Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions Pipe Table"); - TestParser.TestSpec("a | b \n-- | --\n0 | 1 | 2\n3 | 4\n5 |", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
012
34
5
", "pipetables|advanced"); + TestParser.TestSpec("a | b \n-- | --\n0 | 1 | 2\n3 | 4\n5 |", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
012
34
5
", "pipetables|advanced"); } } // **Rule #2** @@ -17227,6 +17231,45 @@ namespace Markdig.Tests Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 22, "Extensions Pipe Table"); TestParser.TestSpec("| abc | def | \n|---|---|\n| cde| ddd| \n| eee| fff|\n| fff | fffff | \n|gggg | ffff | ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
abcdef
cdeddd
eeefff
ffffffff
ggggffff
", "pipetables|advanced"); } + } + // ** Normalized columns count ** + // + // The tables are normalized to the maximum number of columns found in a table + [TestFixture] + public partial class TestExtensionsPipeTable + { + [Test] + public void Example023() + { + // Example 23 + // Section: Extensions Pipe Table + // + // The following CommonMark: + // a | b + // -- | - + // 0 | 1 | 2 + // + // Should be rendered as: + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + //
ab
012
+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 23, "Extensions Pipe Table"); + TestParser.TestSpec("a | b\n-- | - \n0 | 1 | 2", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
012
", "pipetables|advanced"); + } } // # Extensions // diff --git a/src/Markdig/Extensions/Tables/PipeTableParser.cs b/src/Markdig/Extensions/Tables/PipeTableParser.cs index 38307f51..9397e845 100644 --- a/src/Markdig/Extensions/Tables/PipeTableParser.cs +++ b/src/Markdig/Extensions/Tables/PipeTableParser.cs @@ -458,6 +458,9 @@ namespace Markdig.Extensions.Tables // Clear cells when we are done cells.Clear(); + // Normalize the table + table.Normalize(); + // We don't want to continue procesing delimiters, as we are already processing them here return false; } diff --git a/src/Markdig/Extensions/Tables/Table.cs b/src/Markdig/Extensions/Tables/Table.cs index 25db3733..121ba8ab 100644 --- a/src/Markdig/Extensions/Tables/Table.cs +++ b/src/Markdig/Extensions/Tables/Table.cs @@ -69,5 +69,33 @@ namespace Markdig.Extensions.Tables } return true; } + + /// + /// Normalizes the number of columns of this table by taking the maximum columns and appending empty cells. + /// + public void Normalize() + { + var maxColumn = 0; + for (int i = 0; i < this.Count; i++) + { + var row = this[i] as TableRow; + if (row != null && row.Count > maxColumn) + { + maxColumn = row.Count; + } + } + + for (int i = 0; i < this.Count; i++) + { + var row = this[i] as TableRow; + if (row != null) + { + for (int j = row.Count; j < maxColumn; j++) + { + row.Add(new TableCell()); + } + } + } + } } } \ No newline at end of file