diff --git a/Sources/Kysect.Configuin.Core/MarkdownParsing/Documents/MarkdownHeadedBlock.cs b/Sources/Kysect.Configuin.Core/MarkdownParsing/Documents/MarkdownHeadedBlock.cs index 25b9d9e..30b427a 100644 --- a/Sources/Kysect.Configuin.Core/MarkdownParsing/Documents/MarkdownHeadedBlock.cs +++ b/Sources/Kysect.Configuin.Core/MarkdownParsing/Documents/MarkdownHeadedBlock.cs @@ -17,7 +17,7 @@ public MarkdownHeadedBlock(HeadingBlock header, IReadOnlyCollection conte // TODO: remove this method and pass header text instead of header block public string GetHeaderText() { - var roundtripRendererTextExtractor = new RoundtripRendererPlainTextExtractor(MarkdownPipelineProvider.GetDefault()); - return roundtripRendererTextExtractor.ExtractText(Header); + var plainTextExtractor = new PlainTextExtractor(MarkdownPipelineProvider.GetDefault()); + return plainTextExtractor.ExtractText(Header); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/PlainTextExtractor.cs b/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/PlainTextExtractor.cs new file mode 100644 index 0000000..2260ca1 --- /dev/null +++ b/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/PlainTextExtractor.cs @@ -0,0 +1,36 @@ +using Markdig; +using Markdig.Renderers; +using Markdig.Syntax; +using System.Text; +using System.Web; + +namespace Kysect.Configuin.Core.MarkdownParsing.TextExtractor; + +public class PlainTextExtractor : IMarkdownTextExtractor +{ + private readonly MarkdownPipeline _markdownPipeline; + + public PlainTextExtractor(MarkdownPipeline markdownPipeline) + { + _markdownPipeline = markdownPipeline; + } + + public string ExtractText(Block block) + { + var memoryStream = new MemoryStream(); + using var streamWriter = new StreamWriter(memoryStream); + var renderer = new HtmlRenderer(streamWriter) + { + EnableHtmlForBlock = false, + EnableHtmlForInline = false, + }; + _markdownPipeline.Setup(renderer); + + renderer.Render(block); + + streamWriter.Flush(); + string result = Encoding.ASCII.GetString(memoryStream.ToArray()); + // KB: HtmlRenderer change '"' to """. Decode will change in back + return HttpUtility.HtmlDecode(result.Trim()); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/RoundtripRendererPlainTextExtractor.cs b/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/RoundtripRendererPlainTextExtractor.cs deleted file mode 100644 index bd4b377..0000000 --- a/Sources/Kysect.Configuin.Core/MarkdownParsing/TextExtractor/RoundtripRendererPlainTextExtractor.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Markdig; -using Markdig.Syntax; - -namespace Kysect.Configuin.Core.MarkdownParsing.TextExtractor; - -public class RoundtripRendererPlainTextExtractor : IMarkdownTextExtractor -{ - private readonly RoundtripRendererTextExtractor _roundtripRendererTextExtractor; - - public RoundtripRendererPlainTextExtractor(MarkdownPipeline markdownPipeline) - { - _roundtripRendererTextExtractor = new RoundtripRendererTextExtractor(markdownPipeline); - } - - public string ExtractText(Block block) - { - string result = _roundtripRendererTextExtractor.ExtractText(block); - return Markdown.ToPlainText(result).Trim(); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs index af15ddc..ef6a1d0 100644 --- a/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs +++ b/Sources/Kysect.Configuin.Core/MsLearnDocumentation/MsLearnDocumentationParser.cs @@ -141,7 +141,13 @@ private RoslynStyleRuleOption ParseOption(MarkdownHeadedBlock optionBlock) MsLearnPropertyValueDescriptionTable table = _msLearnTableParser.Parse(markdownTableContent); var codeBlocks = optionBlock.Content.OfType().ToList(); - // TODO: implement code block filtering (C# / VB) and parsing + CodeBlock? csharpCodeBlock = codeBlocks + .OfType() + .FirstOrDefault(cb => cb.Info == "csharp"); + // TODO: use null instead of empty line + string csharpCodeSample = csharpCodeBlock is null + ? "" + : _textExtractor.ExtractText(csharpCodeBlock); MsLearnPropertyValueDescriptionTableRow optionName = table.GetSingleValue("Option name"); IReadOnlyList optionValues = table.GetValues("Option values"); @@ -151,7 +157,6 @@ private RoslynStyleRuleOption ParseOption(MarkdownHeadedBlock optionBlock) optionName.Value, optionValues.Select(v => new RoslynStyleRuleOptionValue(v.Value, v.Description)).ToList(), defaultValue.Value, - // TODO: replace with real value - CsharpCodeSample: string.Empty); + csharpCodeSample); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MarkdownTableParserTests.cs b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MarkdownTableParserTests.cs index 9866ee4..d9a1f8f 100644 --- a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MarkdownTableParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MarkdownTableParserTests.cs @@ -20,7 +20,7 @@ public class MarkdownTableParserTests public void Setup() { MarkdownPipeline markdownPipeline = MarkdownPipelineProvider.GetDefault(); - _parser = new MarkdownTableParser(new RoundtripRendererPlainTextExtractor(markdownPipeline)); + _parser = new MarkdownTableParser(new PlainTextExtractor(markdownPipeline)); } [Test] diff --git a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs index 1fc6801..9a3ba3c 100644 --- a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationParserTests.cs @@ -14,7 +14,7 @@ public class MsLearnDocumentationParserTests [SetUp] public void Setup() { - _parser = new MsLearnDocumentationParser(new RoundtripRendererPlainTextExtractor(MarkdownPipelineProvider.GetDefault())); + _parser = new MsLearnDocumentationParser(new PlainTextExtractor(MarkdownPipelineProvider.GetDefault())); } [Test] @@ -48,9 +48,24 @@ public void ParseStyleRule_IDE0040_ReturnExpectedResult() roslynStyleRule.Options.Single().DefaultValue .Should().Be("for_non_interface_members"); - // TODO: should add validation for code samples - //roslynStyleRule.Options.Single().CsharpCodeSample - // .Should().Be(""); + // TODO: add method Be() with ignoreEndOfLine or smth like this + var codeSample = """ + // dotnet_style_require_accessibility_modifiers = always + // dotnet_style_require_accessibility_modifiers = for_non_interface_members + class MyClass + { + private const string thisFieldIsConst = "constant"; + } + + // dotnet_style_require_accessibility_modifiers = never + class MyClass + { + const string thisFieldIsConst = "constant"; + } + """.Replace("\r\n", "\n", StringComparison.InvariantCultureIgnoreCase); + + roslynStyleRule.Options.Single().CsharpCodeSample + .Should().Be(codeSample); } [Test] diff --git a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnTableParserTests.cs b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnTableParserTests.cs index 9915907..37b76a8 100644 --- a/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnTableParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnTableParserTests.cs @@ -126,7 +126,7 @@ public void Parse_PropertyValueWithDescriptionTable_ReturnExpectedResult() private MarkdownTableContent ConvertToMarkdownTable(string content) { - var parser = new MarkdownTableParser(new RoundtripRendererPlainTextExtractor(MarkdownPipelineProvider.GetDefault())); + var parser = new MarkdownTableParser(new PlainTextExtractor(MarkdownPipelineProvider.GetDefault())); MarkdownDocument markdownDocument = MarkdownDocumentExtensions.CreateFromString(content); Table table = markdownDocument.Single().To(); return parser.ParseToSimpleContent(table);