From fbc97be8886c3ce0d8f505674a2c1b25eb717381 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 12 Nov 2024 09:08:29 +0100 Subject: [PATCH] Remove implemented directives that are not needed (#58) --- .../Myst/Directives/CardBlock.cs | 25 ----- .../Myst/Directives/DirectiveBlockParser.cs | 35 +++---- .../Myst/Directives/DirectiveHtmlRenderer.cs | 42 --------- .../Myst/Directives/SideBarBlock.cs | 12 --- .../Myst/Directives/TabSetBlock.cs | 92 ------------------- .../Slices/Directives/Card.cshtml | 11 --- .../Slices/Directives/Grid.cshtml | 10 -- .../Slices/Directives/GridItemCard.cshtml | 13 --- .../Slices/Directives/SideBar.cshtml | 4 - .../Slices/Directives/_ViewModels.cs | 18 ---- .../Directives/CardTests.cs | 39 -------- .../Directives/GridTests.cs | 40 -------- .../Directives/SideBarTests.cs | 25 ----- 13 files changed, 18 insertions(+), 348 deletions(-) delete mode 100644 src/Elastic.Markdown/Myst/Directives/CardBlock.cs delete mode 100644 src/Elastic.Markdown/Myst/Directives/SideBarBlock.cs delete mode 100644 src/Elastic.Markdown/Slices/Directives/Card.cshtml delete mode 100644 src/Elastic.Markdown/Slices/Directives/Grid.cshtml delete mode 100644 src/Elastic.Markdown/Slices/Directives/GridItemCard.cshtml delete mode 100644 src/Elastic.Markdown/Slices/Directives/SideBar.cshtml delete mode 100644 tests/Elastic.Markdown.Tests/Directives/CardTests.cs delete mode 100644 tests/Elastic.Markdown.Tests/Directives/GridTests.cs delete mode 100644 tests/Elastic.Markdown.Tests/Directives/SideBarTests.cs diff --git a/src/Elastic.Markdown/Myst/Directives/CardBlock.cs b/src/Elastic.Markdown/Myst/Directives/CardBlock.cs deleted file mode 100644 index ec84b91..0000000 --- a/src/Elastic.Markdown/Myst/Directives/CardBlock.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information -namespace Elastic.Markdown.Myst.Directives; - -public class CardBlock(DirectiveBlockParser parser, Dictionary properties) - : DirectiveBlock(parser, properties) -{ - public string? Link { get; set; } - - public string? Title { get; set; } - - public string? Header { get; set; } - - public string? Footer { get; set; } - - public override void FinalizeAndValidate(ParserContext context) - { - Title = Arguments; - Link = Properties.GetValueOrDefault("link"); - //TODO Render - Header = Properties.GetValueOrDefault("header"); - Footer = Properties.GetValueOrDefault("footer"); - } -} diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs index e68a069..ccb0b39 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs @@ -50,6 +50,16 @@ public DirectiveBlockParser() { "exercise", 30 }, { "solution", 31 }, { "toctree", 32 }, + { "grid", 26 }, + { "grid-item-card", 26 }, + { "card", 25 }, + { "mermaid", 20 }, + { "aside", 4 }, + { "margin", 4 }, + { "sidebar", 4 }, + { "code-cell", 8 }, + + }.ToFrozenDictionary(); protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor) @@ -67,24 +77,17 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor) if (info.IndexOf("{") == -1) return new CodeBlock(this, "raw", _admonitionData); + // TODO alternate lookup .NET 9 + var directive = info.ToString().Trim(['{', '}', '`']); + if (_unsupportedBlocks.TryGetValue(directive, out var issueId)) + return new UnsupportedDirectiveBlock(this, directive, _admonitionData, issueId); + if (info.IndexOf("{tab-set}") > 0) return new TabSetBlock(this, _admonitionData); if (info.IndexOf("{tab-item}") > 0) return new TabItemBlock(this, _admonitionData); - if (info.IndexOf("{sidebar}") > 0) - return new SideBarBlock(this, _admonitionData); - - if (info.IndexOf("{card}") > 0) - return new CardBlock(this, _admonitionData); - - if (info.IndexOf("{grid}") > 0) - return new GridBlock(this, _admonitionData); - - if (info.IndexOf("{grid-item-card}") > 0) - return new GridItemCardBlock(this, _admonitionData); - if (info.IndexOf("{dropdown}") > 0) return new DropdownBlock(this, _admonitionData); @@ -97,6 +100,9 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor) if (info.IndexOf("{figure-md}") > 0) return new FigureBlock(this, _admonitionData, context); + // this is currently listed as unsupported + // leaving the parsing in untill we are confident we don't want this + // for dev-docs if (info.IndexOf("{mermaid}") > 0) return new MermaidBlock(this, _admonitionData); @@ -123,11 +129,6 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor) if (info.IndexOf($"{{{code}}}") > 0) return new CodeBlock(this, code, _admonitionData); } - // TODO alternate lookup .NET 9 - var directive = info.ToString().Trim(['{', '}', '`']); - if (_unsupportedBlocks.TryGetValue(directive, out var issueId)) - return new UnsupportedDirectiveBlock(this, directive, _admonitionData, issueId); - return new UnknownDirectiveBlock(this, info.ToString(), _admonitionData); } diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index fb1171b..7570290 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -54,24 +54,12 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo case CodeBlock codeBlock: WriteCode(renderer, codeBlock); return; - case SideBarBlock sideBar: - WriteSideBar(renderer, sideBar); - return; case TabSetBlock tabSet: WriteTabSet(renderer, tabSet); return; case TabItemBlock tabItem: WriteTabItem(renderer, tabItem); return; - case CardBlock card: - WriteCard(renderer, card); - return; - case GridBlock grid: - WriteGrid(renderer, grid); - return; - case GridItemCardBlock gridItemCard: - WriteGridItemCard(renderer, gridItemCard); - return; case LiteralIncludeBlock literalIncludeBlock: WriteLiteralIncludeBlock(renderer, literalIncludeBlock); return; @@ -135,30 +123,6 @@ private void WriteFigure(HtmlRenderer renderer, ImageBlock block) private void WriteChildren(HtmlRenderer renderer, DirectiveBlock directiveBlock) => renderer.WriteChildren(directiveBlock); - private void WriteCard(HtmlRenderer renderer, CardBlock block) - { - var slice = Card.Create(new CardViewModel { Title = block.Title, Link = block.Link }); - RenderRazorSlice(slice, renderer, block); - } - - private void WriteGrid(HtmlRenderer renderer, GridBlock block) - { - var slice = Grid.Create(new GridViewModel - { - BreakPoint = block.BreakPoint - }); - RenderRazorSlice(slice, renderer, block); - } - - private void WriteGridItemCard(HtmlRenderer renderer, GridItemCardBlock directiveBlock) - { - var title = directiveBlock.Arguments; - var link = directiveBlock.Properties.GetValueOrDefault("link"); - var slice = GridItemCard.Create(new GridItemCardViewModel { Title = title, Link = link }); - RenderRazorSlice(slice, renderer, directiveBlock); - } - - private void WriteVersion(HtmlRenderer renderer, VersionBlock block) { var slice = Slices.Directives.Version.Create(new VersionViewModel @@ -204,12 +168,6 @@ private void WriteCode(HtmlRenderer renderer, CodeBlock block) } - private void WriteSideBar(HtmlRenderer renderer, SideBarBlock directiveBlock) - { - var slice = SideBar.Create(new SideBarViewModel()); - RenderRazorSlice(slice, renderer, directiveBlock); - } - private void WriteTabSet(HtmlRenderer renderer, TabSetBlock block) { var slice = TabSet.Create(new TabSetViewModel()); diff --git a/src/Elastic.Markdown/Myst/Directives/SideBarBlock.cs b/src/Elastic.Markdown/Myst/Directives/SideBarBlock.cs deleted file mode 100644 index 0c1bd8f..0000000 --- a/src/Elastic.Markdown/Myst/Directives/SideBarBlock.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information -namespace Elastic.Markdown.Myst.Directives; - -public class SideBarBlock(DirectiveBlockParser parser, Dictionary properties) - : DirectiveBlock(parser, properties) -{ - public override void FinalizeAndValidate(ParserContext context) - { - } -} diff --git a/src/Elastic.Markdown/Myst/Directives/TabSetBlock.cs b/src/Elastic.Markdown/Myst/Directives/TabSetBlock.cs index 0853273..465bf23 100644 --- a/src/Elastic.Markdown/Myst/Directives/TabSetBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/TabSetBlock.cs @@ -36,95 +36,3 @@ public override void FinalizeAndValidate(ParserContext context) } } - -public class GridResponsive -{ - public required int Xs { get; init; } - public required int Sm { get; init; } - public required int Md { get; init; } - public required int Lg { get; init; } -} -public class GridCorners -{ - public required int Top { get; init; } - public required int Bottom { get; init; } - public required int Left { get; init; } - public required int Right { get; init; } -} - -public class GridBlock(DirectiveBlockParser parser, Dictionary properties) - : DirectiveBlock(parser, properties) -{ - - public GridResponsive BreakPoint { get; set; } = new() { Xs = 1, Sm = 1, Md = 2, Lg = 3 }; - - /// Spacing between items. One or four integers (for “xs sm md lg”) between 0 and 5. - public GridResponsive? Gutter { get; set; } - - /// Outer margin of grid. One (all) or four (top bottom left right) values from: 0, 1, 2, 3, 4, 5, auto. - public GridCorners? Margin { get; set; } - - /// Inner padding of grid. One (all) or four (top bottom left right) values from: 0, 1, 2, 3, 4, 5. - public GridCorners? Padding { get; set; } - - /// Create a border around the grid. - public bool? Outline { get; set; } - - /// Reverse the order of the grid items. - public bool? Reverse { get; set; } - - /// Additional CSS classes for the grid container element. - public string? ClassContainer { get; set; } - - /// Additional CSS classes for the grid row element - public string? ClassRow { get; set; } - - - public override void FinalizeAndValidate(ParserContext context) - { - //todo we always assume 4 integers - if (!string.IsNullOrEmpty(Arguments)) - ParseData(Arguments, (xs, sm, md, lg) => BreakPoint = new() { Xs = xs, Sm = sm, Md = md, Lg = lg }); - else - { - //todo invalidate - } - if (Properties.GetValueOrDefault("gutter") is { } gutter) - ParseData(gutter, (xs, sm, md, lg) => Gutter = new() { Xs = xs, Sm = sm, Md = md, Lg = lg }); - if (Properties.GetValueOrDefault("margin") is { } margin) - ParseData(margin, (top, bottom, left, right) => Margin = new() { Top = top, Bottom = bottom, Left = left, Right = right }); - if (Properties.GetValueOrDefault("padding") is { } padding) - ParseData(padding, (top, bottom, left, right) => Padding = new() { Top = top, Bottom = bottom, Left = left, Right = right }); - ParseBool("outline", b => Outline = b); - ParseBool("reverse", b => Reverse = b); - - ClassContainer = Properties.GetValueOrDefault("class-container"); - ClassRow = Properties.GetValueOrDefault("class-row"); - - } - - private void ParseData(string data, Action setter, bool allowAuto = true) - { - var columns = data.Split(' ') - .Select(t => int.TryParse(t, out var c) ? c : t == "auto" ? -1 : -2) - .Where(t => t is > -2 and <= 5) - .ToArray(); - if (columns.Length == 1) - setter(columns[0], columns[0], columns[0], columns[0]); - else if (columns.Length == 4) - setter(columns[0], columns[1], columns[2], columns[3]); - else - { - //todo invalidate - } - } - - -} -public class GridItemCardBlock(DirectiveBlockParser parser, Dictionary properties) - : DirectiveBlock(parser, properties) -{ - public override void FinalizeAndValidate(ParserContext context) - { - } -} diff --git a/src/Elastic.Markdown/Slices/Directives/Card.cshtml b/src/Elastic.Markdown/Slices/Directives/Card.cshtml deleted file mode 100644 index ac5457b..0000000 --- a/src/Elastic.Markdown/Slices/Directives/Card.cshtml +++ /dev/null @@ -1,11 +0,0 @@ -@inherits RazorSlice -
-
-
@(new HtmlString(Model.Title))
- [CONTENT] -
- @if (!string.IsNullOrEmpty(Model.Link)) - { - @Model.Link - } -
diff --git a/src/Elastic.Markdown/Slices/Directives/Grid.cshtml b/src/Elastic.Markdown/Slices/Directives/Grid.cshtml deleted file mode 100644 index 742ad2b..0000000 --- a/src/Elastic.Markdown/Slices/Directives/Grid.cshtml +++ /dev/null @@ -1,10 +0,0 @@ -@inherits RazorSlice -
-
- [CONTENT] -
-
diff --git a/src/Elastic.Markdown/Slices/Directives/GridItemCard.cshtml b/src/Elastic.Markdown/Slices/Directives/GridItemCard.cshtml deleted file mode 100644 index 00a4af3..0000000 --- a/src/Elastic.Markdown/Slices/Directives/GridItemCard.cshtml +++ /dev/null @@ -1,13 +0,0 @@ -@inherits RazorSlice -
-
-
-
@(new HtmlString(Model.Title))
- [CONTENT] -
- @if (!string.IsNullOrEmpty(Model.Link)) - { - @Model.Link - } -
-
\ No newline at end of file diff --git a/src/Elastic.Markdown/Slices/Directives/SideBar.cshtml b/src/Elastic.Markdown/Slices/Directives/SideBar.cshtml deleted file mode 100644 index 04c320b..0000000 --- a/src/Elastic.Markdown/Slices/Directives/SideBar.cshtml +++ /dev/null @@ -1,4 +0,0 @@ -@inherits RazorSlice - \ No newline at end of file diff --git a/src/Elastic.Markdown/Slices/Directives/_ViewModels.cs b/src/Elastic.Markdown/Slices/Directives/_ViewModels.cs index 0e9fc6f..70377c8 100644 --- a/src/Elastic.Markdown/Slices/Directives/_ViewModels.cs +++ b/src/Elastic.Markdown/Slices/Directives/_ViewModels.cs @@ -29,7 +29,6 @@ public class VersionViewModel public required string Title { get; init; } } -public class SideBarViewModel; public class TabSetViewModel; public class TabItemViewModel @@ -39,23 +38,6 @@ public class TabItemViewModel public required string Title { get; init; } } -public class CardViewModel -{ - public required string? Title { get; init; } - public required string? Link { get; init; } -} - -public class GridViewModel -{ - public required GridResponsive BreakPoint { get; init; } -} - -public class GridItemCardViewModel -{ - public required string? Title { get; init; } - public required string? Link { get; init; } -} - public class IncludeViewModel { public required string Html { get; init; } diff --git a/tests/Elastic.Markdown.Tests/Directives/CardTests.cs b/tests/Elastic.Markdown.Tests/Directives/CardTests.cs deleted file mode 100644 index 50421de..0000000 --- a/tests/Elastic.Markdown.Tests/Directives/CardTests.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information -using Elastic.Markdown.Myst.Directives; -using FluentAssertions; -using Xunit.Abstractions; - -namespace Elastic.Markdown.Tests.Directives; - -public class CardTests(ITestOutputHelper output) : DirectiveTest(output, -""" -```{card} Card title -Card content -``` -""" -) -{ - [Fact] - public void ParsesBlock () => Block.Should().NotBeNull(); - -} - -public class LinkCardTests(ITestOutputHelper output) : DirectiveTest(output, -""" -```{card} Clickable Card -:link: https://elastic.co/docs - -The entire card can be clicked to navigate to `Elastic Docs`. -``` -""" -) -{ - [Fact] - public void ParsesBlock() => Block.Should().NotBeNull(); - - [Fact] - public void ExposesLink() => Block!.Link.Should().Be("https://elastic.co/docs"); - -} diff --git a/tests/Elastic.Markdown.Tests/Directives/GridTests.cs b/tests/Elastic.Markdown.Tests/Directives/GridTests.cs deleted file mode 100644 index 5473542..0000000 --- a/tests/Elastic.Markdown.Tests/Directives/GridTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information -using Elastic.Markdown.Myst.Directives; -using FluentAssertions; -using Xunit.Abstractions; - -namespace Elastic.Markdown.Tests.Directives; - -public class GridTests(ITestOutputHelper output) : DirectiveTest(output, -""" -````{grid} 2 2 3 4 -```{grid-item-card} Admonitions -:link: admonitions.html - Click this card to learn about admonitions. -``` -```{grid-item-card} Code Blocks -:link: code.html - Click this card to learn about code blocks. -``` -```{grid-item-card} Tabs and Dropdowns -:link: tabs_dropdowns.html - Click this card to learn about Tabs and Dropdowns. -``` -""" -) -{ - [Fact] - public void ParsesBlock() => Block.Should().NotBeNull(); - - [Fact] - public void ParsesBreakPoint() - { - Block!.BreakPoint.Should().NotBeNull(); - Block!.BreakPoint.Xs.Should().Be(2); - Block!.BreakPoint.Sm.Should().Be(2); - Block!.BreakPoint.Md.Should().Be(3); - Block!.BreakPoint.Lg.Should().Be(4); - } -} diff --git a/tests/Elastic.Markdown.Tests/Directives/SideBarTests.cs b/tests/Elastic.Markdown.Tests/Directives/SideBarTests.cs deleted file mode 100644 index 1e1b873..0000000 --- a/tests/Elastic.Markdown.Tests/Directives/SideBarTests.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to Elasticsearch B.V under one or more agreements. -// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information -using Elastic.Markdown.Myst.Directives; -using FluentAssertions; -using Xunit.Abstractions; - -namespace Elastic.Markdown.Tests.Directives; - -public class SideBarTests(ITestOutputHelper output) : DirectiveTest(output, -""" -```{sidebar} -This code is very helpful. - -It does lots of things. - -But it does not sing. -``` -""" -) -{ - [Fact] - public void ParsesBlock () => Block.Should().NotBeNull(); - -}