Skip to content

Commit

Permalink
Allow custom blocks (tables) to be used in lists
Browse files Browse the repository at this point in the history
Enables #441 to be fixed
  • Loading branch information
jskeet committed Mar 18, 2022
1 parent 4eb5d21 commit d7644ed
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 7 deletions.
3 changes: 3 additions & 0 deletions tools/MarkdownConverter.Tests/MarkdownSourceConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MarkdownSourceConverterTests
[InlineData("antlr-with-line-comment")]
[InlineData("note")]
[InlineData("code-block-in-list")]
[InlineData("table-in-list")]
public void SingleResourceConversion(string name)
{
var reporter = new Reporter(TextWriter.Null);
Expand Down Expand Up @@ -55,6 +56,8 @@ public void SingleResourceConversion(string name)
diff.DifferenceListener += (comparison, outcome) => differences.Add(comparison);
diff.Compare(expectedDoc, actualDoc);
Assert.Empty(differences);
Assert.Equal(0, reporter.Warnings);
Assert.Equal(0, reporter.Errors);
}

[Theory]
Expand Down
12 changes: 12 additions & 0 deletions tools/MarkdownConverter.Tests/table-in-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 1 Heading

Before a list

- List item

<!-- Custom Word conversion: test -->
<table>
</table>

More list text
- Next list item
109 changes: 109 additions & 0 deletions tools/MarkdownConverter.Tests/table-in-list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<doc>
<p>
<pPr>
<pStyle val="Heading1" />
</pPr>
<bookmarkStart name="_Toc00001" id="1" />
<r>
<t space="preserve">Heading</t>
</r>
<bookmarkEnd id="1" />
</p>
<p>
<r>
<t space="preserve">Before a list</t>
</r>
</p>
<p>
<pPr>
<numPr>
<pStyle val="ListParagraph" />
<ilvl val="0" />
<numId val="1" />
</numPr>
</pPr>
<r>
<t space="preserve">List item</t>
</r>
</p>
<p>
<pPr>
<pStyle val="TableLineBefore" />
</pPr>
<r>
<t />
</r>
</p>
<tbl>
<tblPr>
<tblStyle val="TableGrid" />
<tblW w="8000" />
<tblInd w="900" type="dxa" />
<tblBorders>
<top val="single" />
<left val="single" />
<bottom val="single" />
<right val="single" />
<insideH val="single" />
<insideV val="single" />
</tblBorders>
<tblCellMar />
</tblPr>
<tr>
<tc>
<pPr>
<pStyle val="TableCellNormal" />
<jc val="center" />
</pPr>
<p>
<r>
<t>Normal cell</t>
</r>
</p>
</tc>
<tc>
<pPr>
<pStyle val="TableCellNormal" />
<jc val="center" />
</pPr>
<p>
<pPr>
<pStyle val="Code" />
<ind left="0" />
</pPr>
<r>
<t>Code cell</t>
</r>
</p>
</tc>
</tr>
</tbl>
<p>
<pPr>
<pStyle val="TableLineAfter" />
</pPr>
<r>
<t />
</r>
</p>
<p>
<pPr>
<ind left="540" />
</pPr>
<r>
<t space="preserve">More list text</t>
</r>
</p>
<p>
<pPr>
<numPr>
<pStyle val="ListParagraph" />
<ilvl val="0" />
<numId val="1" />
</numPr>
</pPr>
<r>
<t space="preserve">Next list item</t>
</r>
</p>
</doc>
30 changes: 23 additions & 7 deletions tools/MarkdownConverter/Converter/MarkdownSourceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
yield return table;
}
}
else if (content is MarkdownParagraph.InlineBlock inlineBlock && GetCustomBlockId(inlineBlock) is string customBlockId)
{
foreach (var element in GenerateCustomBlockElements(customBlockId, inlineBlock))
{
yield return element;
}
}
else
{
reporter.Error("MD08", $"Unexpected item in list '{content.GetType().Name}'");
Expand Down Expand Up @@ -459,13 +466,13 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
reporter.Error("MD11", $"Unrecognized markdown element {md.GetType().Name}");
yield return new Paragraph(new Run(new Text($"[{md.GetType().Name}]")));
}
}

string GetCustomBlockId(MarkdownParagraph.InlineBlock block)
{
Regex customBlockComment = new Regex(@"^<!-- Custom Word conversion: ([a-z0-9_]+) -->");
var match = customBlockComment.Match(block.code);
return match.Success ? match.Groups[1].Value : null;
}
static string GetCustomBlockId(MarkdownParagraph.InlineBlock block)
{
Regex customBlockComment = new Regex(@"^<!-- Custom Word conversion: ([a-z0-9_]+) -->");
var match = customBlockComment.Match(block.code);
return match.Success ? match.Groups[1].Value : null;
}

IEnumerable<FlatItem> FlattenList(MarkdownParagraph.ListBlock md)
Expand Down Expand Up @@ -565,7 +572,7 @@ IEnumerable<FlatItem> FlattenList(MarkdownParagraph.ListBlock md, int level)
yield return subitem;
}
}
else if (mdp.IsTableBlock)
else if (mdp.IsTableBlock || mdp is MarkdownParagraph.InlineBlock inline && GetCustomBlockId(inline) is not null)
{
yield return new FlatItem(level, false, isOrdered, mdp);
}
Expand Down Expand Up @@ -935,6 +942,8 @@ IEnumerable<OpenXmlElement> Literal2Elements(string literal, bool isNested, bool
"function_members" => TableHelpers.CreateFunctionMembersTable(block.code),
"format_strings_1" => new[] { new Paragraph(new Run(new Text("FIXME: Replace with first format strings table"))) },
"format_strings_2" => new[] { new Paragraph(new Run(new Text("FIXME: Replace with second format strings table"))) },
// This is for the sake of a simple unit test. It's a single-row table with two cells.
"test" => TableHelpers.CreateTestTable(),
_ => HandleInvalidCustomBlock(customBlockId)
};

Expand Down Expand Up @@ -1124,6 +1133,13 @@ internal static IEnumerable<OpenXmlCompositeElement> CreateSubtractionTable()
return CreateTableElements(table);
}

internal static IEnumerable<OpenXmlCompositeElement> CreateTestTable()
{
Table table = CreateTable(indentation: 900, width: 8000);
table.Append(CreateTableRow(CreateNormalTableCell("Normal cell"), CreateCodeTableCell("Code cell")));
return CreateTableElements(table);
}

private static TableRow CreateTableRow(params TableCell[] cells) => new TableRow(cells);

internal static Table CreateTable(int indentation = 360, int? width = null)
Expand Down

0 comments on commit d7644ed

Please sign in to comment.