Skip to content

Commit

Permalink
Fix pipetable parsing (issue xoofx#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Sep 18, 2016
1 parent a593212 commit fa281f1
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 227 deletions.
44 changes: 42 additions & 2 deletions src/Markdig.Tests/Specs/PipeTableSpecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ c no d
c no d</p>
````````````````````````````````

The number of columns in the first row determine the number of columns for the whole table. Any extra columns delimiter `|` for sub-sequent lines are converted to literal strings instead:
If a row contains more column than the header row, it will still be added as a column:

```````````````````````````````` example
a | b
Expand All @@ -141,7 +141,8 @@ a | b
<tbody>
<tr>
<td>0</td>
<td>1 | 2</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
Expand Down Expand Up @@ -507,3 +508,42 @@ a | b
</table>
````````````````````````````````

** Tests **

Tests trailing spaces after pipes

```````````````````````````````` example
| abc | def |
|---|:---|
| cde| ddd|
| eee| fff|
| fff | fffff |
|gggg | ffff |
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>cde</td>
<td>ddd</td>
</tr>
<tr>
<td>eee</td>
<td>fff</td>
</tr>
<tr>
<td>fff</td>
<td>fffff</td>
</tr>
<tr>
<td>gggg</td>
<td>ffff</td>
</tr>
</tbody>
</table>
````````````````````````````````
59 changes: 56 additions & 3 deletions src/Markdig.Tests/Specs/Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16515,7 +16515,7 @@ public void Example006()
TestParser.TestSpec("a | b\nc no d", "<p>a | b\nc no d</p>", "pipetables|advanced");
}
}
// The number of columns in the first row determine the number of columns for the whole table. Any extra columns delimiter `|` for sub-sequent lines are converted to literal strings instead:
// If a row contains more column than the header row, it will still be added as a column:
[TestFixture]
public partial class TestExtensionsPipeTable
{
Expand Down Expand Up @@ -16543,7 +16543,8 @@ public void Example007()
// <tbody>
// <tr>
// <td>0</td>
// <td>1 | 2</td>
// <td>1</td>
// <td>2</td>
// </tr>
// <tr>
// <td>3</td>
Expand All @@ -16556,7 +16557,7 @@ public void Example007()
// </table>

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 |", "<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 | 2</td>\n</tr>\n<tr>\n<td>3</td>\n<td>4</td>\n</tr>\n<tr>\n<td>5</td>\n</tr>\n</tbody>\n</table>", "pipetables|advanced");
TestParser.TestSpec("a | b \n-- | --\n0 | 1 | 2\n3 | 4\n5 |", "<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<td>2</td>\n</tr>\n<tr>\n<td>3</td>\n<td>4</td>\n</tr>\n<tr>\n<td>5</td>\n</tr>\n</tbody>\n</table>", "pipetables|advanced");
}
}
// **Rule #2**
Expand Down Expand Up @@ -17078,6 +17079,58 @@ public void Example021()
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 21, "Extensions Pipe Table");
TestParser.TestSpec("a | b\n-- | --\n[This is a link with a | inside the label](http://google.com) | 1", "<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><a href=\"http://google.com\">This is a link with a | inside the label</a></td>\n<td>1</td>\n</tr>\n</tbody>\n</table>", "pipetables|advanced");
}
}
// ** Tests **
//
// Tests trailing spaces after pipes
[TestFixture]
public partial class TestExtensionsPipeTable
{
[Test]
public void Example022()
{
// Example 22
// Section: Extensions Pipe Table
//
// The following CommonMark:
// | abc | def |
// |---|:---|
// | cde| ddd|
// | eee| fff|
// | fff | fffff |
// |gggg | ffff |
//
// Should be rendered as:
// <table>
// <thead>
// <tr>
// <th>abc</th>
// <th>def</th>
// </tr>
// </thead>
// <tbody>
// <tr>
// <td>cde</td>
// <td>ddd</td>
// </tr>
// <tr>
// <td>eee</td>
// <td>fff</td>
// </tr>
// <tr>
// <td>fff</td>
// <td>fffff</td>
// </tr>
// <tr>
// <td>gggg</td>
// <td>ffff</td>
// </tr>
// </tbody>
// </table>

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 | ", "<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>cde</td>\n<td>ddd</td>\n</tr>\n<tr>\n<td>eee</td>\n<td>fff</td>\n</tr>\n<tr>\n<td>fff</td>\n<td>fffff</td>\n</tr>\n<tr>\n<td>gggg</td>\n<td>ffff</td>\n</tr>\n</tbody>\n</table>", "pipetables|advanced");
}
}
// # Extensions
//
Expand Down
1 change: 1 addition & 0 deletions src/Markdig.Tests/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static IEnumerable<KeyValuePair<string, MarkdownPipeline>> GetPipeline(s
foreach (var extensionsText in extensionGroups)
{
var builder = new MarkdownPipelineBuilder();
builder.DebugLog = Console.Out;
var pipeline = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText);
yield return new KeyValuePair<string, MarkdownPipeline>(extensionsText, pipeline.Build());
}
Expand Down
27 changes: 0 additions & 27 deletions src/Markdig.Tests/TestPlayParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,6 @@ public void TestSimple()
Console.WriteLine(result);
}

[Test]
public void TestPipeTables()
{
TestParser.TestSpec(@"
| abc | def | ghi |
|:---:|-----|----:|
| 1 | 2 | 3 |
", @"
<table>
<thead>
<tr>
<th style=""text-align: center;"">abc</th>
<th>def</th>
<th style=""text-align: right;"">ghi</th>
</tr>
</thead>
<tbody>
<tr>
<td style=""text-align: center;"">1</td>
<td>2</td>
<td style=""text-align: right;"">3</td>
</tr>
</tbody>
</table>
", "advanced");
}

[Test]
public void TestSelfPipeline1()
{
Expand Down
5 changes: 4 additions & 1 deletion src/Markdig/Extensions/Tables/HtmlTableRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ protected override void Write(HtmlRenderer renderer, Table table)
}
if (table.ColumnDefinitions != null)
{
var columnIndex = cell.ColumnIndex == -1 || cell.ColumnIndex > table.ColumnDefinitions.Count ? i : cell.ColumnIndex;
var columnIndex = cell.ColumnIndex < 0 || cell.ColumnIndex >= table.ColumnDefinitions.Count
? i
: cell.ColumnIndex;
columnIndex = columnIndex >= table.ColumnDefinitions.Count ? table.ColumnDefinitions.Count - 1 : columnIndex;
switch (table.ColumnDefinitions[columnIndex].Alignment)
{
case TableColumnAlign.Center:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Markdig.Extensions.Tables
/// The delimiter used to separate the columns of a pipe table.
/// </summary>
/// <seealso cref="Markdig.Syntax.Inlines.DelimiterInline" />
public class PiprTableDelimiterInline : DelimiterInline
public class PipeTableDelimiterInline : DelimiterInline
{
public PiprTableDelimiterInline(InlineParser parser) : base(parser)
public PipeTableDelimiterInline(InlineParser parser) : base(parser)
{
}

Expand Down
Loading

0 comments on commit fa281f1

Please sign in to comment.