From 0649c62685f16258daeba9450e0a09bcbe1352c0 Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Wed, 16 Feb 2022 09:02:11 -0600 Subject: [PATCH] feat: added the abilty to add or remove the preformat block around tables --- .../TableTests.cs | 35 +++++++++++++++++++ .../Extensions/GemtextPipeTableExtension.cs | 4 ++- .../Extensions/GemtextPipeTableOptions.cs | 6 ++++ .../Renderers/Gemtext/Blocks/TableRenderer.cs | 23 +++++++++--- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/MfGames.Markdown.Gemtext.Tests/TableTests.cs b/src/MfGames.Markdown.Gemtext.Tests/TableTests.cs index 0c82a21..dd3456d 100644 --- a/src/MfGames.Markdown.Gemtext.Tests/TableTests.cs +++ b/src/MfGames.Markdown.Gemtext.Tests/TableTests.cs @@ -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), diff --git a/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableExtension.cs b/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableExtension.cs index 7b34954..4d5dc27 100644 --- a/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableExtension.cs +++ b/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableExtension.cs @@ -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)); } } } diff --git a/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableOptions.cs b/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableOptions.cs index 3de1d18..9da5a52 100644 --- a/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableOptions.cs +++ b/src/MfGames.Markdown.Gemtext/Extensions/GemtextPipeTableOptions.cs @@ -12,5 +12,11 @@ namespace MfGames.Markdown.Gemtext.Extensions /// Gets or sets the table builder to control formatting. /// public Action? ConfigureTableBuilder { get; set; } + + /// + /// Gets or sets a value whether the preformat (backticks) fence should + /// not be emitted. + /// + public bool OmitPreformatLines { get; set; } } } diff --git a/src/MfGames.Markdown.Gemtext/Renderers/Gemtext/Blocks/TableRenderer.cs b/src/MfGames.Markdown.Gemtext/Renderers/Gemtext/Blocks/TableRenderer.cs index 8e63c6a..37a67d9 100644 --- a/src/MfGames.Markdown.Gemtext/Renderers/Gemtext/Blocks/TableRenderer.cs +++ b/src/MfGames.Markdown.Gemtext/Renderers/Gemtext/Blocks/TableRenderer.cs @@ -13,16 +13,18 @@ namespace MfGames.Markdown.Gemtext.Renderers.Gemtext.Blocks { private readonly Action? configureTableBuilder; - public TableRenderer(Action? configureTableBuilder) + private readonly bool omitPreformat; + + public TableRenderer( + bool omitPreformat, + Action? 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 GetCellValues(TableRow row)