Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for github style tables #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CommonMark.Tests/CommonMark.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="HtmlTests.cs" />
<Compile Include="SettingsTests.cs" />
<Compile Include="GeneralTests.cs" />
<Compile Include="TableTests.cs" />
<Compile Include="UrlTests.cs" />
<Compile Include="EmphasisTests.cs" />
<Compile Include="Helpers.cs" />
Expand Down
336 changes: 336 additions & 0 deletions CommonMark.Tests/TableTests.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CommonMark/CommonMark.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Syntax\StringContent.cs" />
<Compile Include="Syntax\StringContentPart.cs" />
<Compile Include="Syntax\TableHeaderAlignment.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions CommonMark/CommonMarkAdditionalFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public enum CommonMarkAdditionalFeatures
/// </summary>
StrikethroughTilde = 1,

/// <summary>
/// The parser will recognize
///
/// First Header | Second Header
/// ------------- | -------------
/// Content Cell | Content Cell
/// Content Cell | Content Cell
///
/// style tables.
///
/// Refer to https://help.github.com/articles/organizing-information-with-tables/ for more examples
/// </summary>
GithubStyleTables = 2,

/// <summary>
/// All additional features are enabled.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions CommonMark/CommonMarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static Syntax.Block ProcessStage1(TextReader source, CommonMarkSettings s
reader.ReadLine(line);
while (line.Line != null)
{
BlockMethods.IncorporateLine(line, ref cur);
BlockMethods.IncorporateLine(line, ref cur, settings);
reader.ReadLine(line);
}
}
Expand All @@ -132,7 +132,7 @@ public static Syntax.Block ProcessStage1(TextReader source, CommonMarkSettings s
{
do
{
BlockMethods.Finalize(cur, line);
BlockMethods.Finalize(cur, line, settings);
cur = cur.Parent;
} while (cur != null);
}
Expand Down
99 changes: 99 additions & 0 deletions CommonMark/Formatters/HtmlFormatterSlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,101 @@ internal static void PrintPosition(HtmlTextWriter writer, Inline inline)
writer.WriteConstant("\"");
}

static void WriteTable(Block table, HtmlTextWriter writer, CommonMarkSettings settings, Stack<InlineStackEntry> stack)
{
if ((settings.AdditionalFeatures & CommonMarkAdditionalFeatures.GithubStyleTables) == 0)
{
throw new CommonMarkException("Table encountered in AST, but GithubStyleTables are not enabled");
}

var header = table.FirstChild;
var firstRow = table.FirstChild.NextSibling;

writer.WriteConstant("<table>");
writer.WriteConstant("<thead>");
writer.WriteConstant("<tr>");

var numHeadings = 0;

var curHeaderCell = header.FirstChild;
while (curHeaderCell != null)
{
var alignment = table.TableHeaderAlignments[numHeadings];

numHeadings++;

if (alignment == TableHeaderAlignment.None)
{
writer.WriteConstant("<th>");
}
else
{
switch (alignment)
{
case TableHeaderAlignment.Center: writer.WriteConstant("<th align=\"center\">"); break;
case TableHeaderAlignment.Left: writer.WriteConstant("<th align=\"left\">"); break;
case TableHeaderAlignment.Right: writer.WriteConstant("<th align=\"right\">"); break;
default: throw new CommonMarkException("Unexpected TableHeaderAlignment [" + alignment + "]");
}
}
InlinesToHtml(writer, curHeaderCell.InlineContent, settings, stack);
writer.WriteConstant("</th>");

curHeaderCell = curHeaderCell.NextSibling;
}

writer.WriteConstant("</tr>");
writer.WriteConstant("</thead>");

writer.WriteConstant("<tbody>");
var curRow = firstRow;
while (curRow != null)
{
writer.WriteConstant("<tr>");
var curRowCell = curRow.FirstChild;

var numCells = 0;

while (curRowCell != null && numCells < numHeadings)
{
var alignment = table.TableHeaderAlignments[numCells];

numCells++;

if (alignment == TableHeaderAlignment.None)
{
writer.WriteConstant("<td>");
}
else
{
switch (alignment)
{
case TableHeaderAlignment.Center: writer.WriteConstant("<td align=\"center\">"); break;
case TableHeaderAlignment.Left: writer.WriteConstant("<td align=\"left\">"); break;
case TableHeaderAlignment.Right: writer.WriteConstant("<td align=\"right\">"); break;
default: throw new CommonMarkException("Unexpected TableHeaderAlignment [" + alignment + "]");
}
}
InlinesToHtml(writer, curRowCell.InlineContent, settings, stack);
writer.WriteConstant("</td>");

curRowCell = curRowCell.NextSibling;
}

while (numCells < numHeadings)
{
numCells++;
writer.WriteConstant("<td></td>");
}

writer.WriteConstant("</tr>");

curRow = curRow.NextSibling;
}
writer.WriteConstant("</tbody>");
writer.WriteConstant("</table>");
}

private static void BlocksToHtmlInner(HtmlTextWriter writer, Block block, CommonMarkSettings settings)
{
var stack = new Stack<BlockStackEntry>();
Expand Down Expand Up @@ -357,6 +452,10 @@ private static void BlocksToHtmlInner(HtmlTextWriter writer, Block block, Common

break;

case BlockTag.Table:
WriteTable(block, writer, settings, inlineStack);
break;

case BlockTag.ReferenceDefinition:
break;

Expand Down
Loading