feat: added the abilty to add or remove the preformat block around tables

This commit is contained in:
Dylan R. E. Moonfire 2022-02-16 09:02:11 -06:00
parent e94bbbed41
commit 0649c62685
4 changed files with 63 additions and 5 deletions

View file

@ -19,6 +19,7 @@ namespace MfGames.Markdown.Gemini.Tests
new GemtextPipeTableExtension(
new GemtextPipeTableOptions()
{
OmitPreformatLines = true,
ConfigureTableBuilder = (x) =>
x.WithFormat(
ConsoleTableBuilderFormat.MarkDown),
@ -42,6 +43,39 @@ namespace MfGames.Markdown.Gemini.Tests
Assert.Equal(expected, actual);
}
[Fact]
public void FencedTable()
{
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
.Use(
new GemtextPipeTableExtension(
new GemtextPipeTableOptions()
{
ConfigureTableBuilder = (x) =>
x.WithFormat(
ConsoleTableBuilderFormat.MarkDown),
}))
.Build();
string input = string.Join(
"\n",
"aaa|bbb|ccc",
"--:|---|:-:",
"1|2|3",
"4|5|6");
string expected = string.Join(
"\n",
"```",
"| aaa | bbb | ccc |",
"|-----|-----|-----|",
"| 1 | 2 | 3 |",
"| 4 | 5 | 6 |",
"```",
"");
string actual = MarkdownGemtext.ToGemtext(input, pipeline);
Assert.Equal(expected, actual);
}
[Fact]
public void SimpleTable()
{
@ -50,6 +84,7 @@ namespace MfGames.Markdown.Gemini.Tests
new GemtextPipeTableExtension(
new GemtextPipeTableOptions()
{
OmitPreformatLines = true,
ConfigureTableBuilder = (x) =>
x.WithFormat(
ConsoleTableBuilderFormat.MarkDown),

View file

@ -59,7 +59,9 @@ namespace MfGames.Markdown.Gemtext.Extensions
}
gemtext.ObjectRenderers.Add(
new TableRenderer(this.Options.ConfigureTableBuilder));
new TableRenderer(
this.Options.OmitPreformatLines,
this.Options.ConfigureTableBuilder));
}
}
}

View file

@ -12,5 +12,11 @@ namespace MfGames.Markdown.Gemtext.Extensions
/// Gets or sets the table builder to control formatting.
/// </summary>
public Action<ConsoleTableBuilder>? ConfigureTableBuilder { get; set; }
/// <summary>
/// Gets or sets a value whether the preformat (backticks) fence should
/// not be emitted.
/// </summary>
public bool OmitPreformatLines { get; set; }
}
}

View file

@ -13,16 +13,18 @@ namespace MfGames.Markdown.Gemtext.Renderers.Gemtext.Blocks
{
private readonly Action<ConsoleTableBuilder>? configureTableBuilder;
public TableRenderer(Action<ConsoleTableBuilder>? configureTableBuilder)
private readonly bool omitPreformat;
public TableRenderer(
bool omitPreformat,
Action<ConsoleTableBuilder>? configureTableBuilder)
{
this.omitPreformat = omitPreformat;
this.configureTableBuilder = configureTableBuilder;
}
protected override void Write(GemtextRenderer renderer, Table table)
{
// Make sure we have plenty of space above us.
renderer.EnsureTwoLines();
// Since Gemtext doesn't have a table format per-se, we are going
// to use ConsoleTableEx to make a nicely-formatted table and emit
// the lines directly. That should produce the desired result.
@ -62,7 +64,20 @@ namespace MfGames.Markdown.Gemtext.Renderers.Gemtext.Blocks
// Format the final table.
string formatted = builder.Export().ToString().TrimEnd();
// Write out the table including making sure two lines are above it.
renderer.EnsureTwoLines();
if (!this.omitPreformat)
{
renderer.WriteLine("```");
}
renderer.WriteLine(formatted);
if (!this.omitPreformat)
{
renderer.WriteLine("```");
}
}
private static List<object> GetCellValues(TableRow row)