Skip to content

Commit

Permalink
Support zone section in learn docs
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 17, 2024
1 parent dcdf829 commit e8f5ae4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class MsLearnDocumentationParser : IMsLearnDocumentationParser
private readonly MarkdownTableParser _markdownTableParser;
private readonly MsLearnTableParser _msLearnTableParser;
private readonly IMarkdownTextExtractor _textExtractor;
private readonly MsLearnDocumentationPreprocessor _documentationPreprocessor;

public MsLearnDocumentationParser(IMarkdownTextExtractor textExtractor, ILogger logger)
{
Expand All @@ -28,12 +29,15 @@ public MsLearnDocumentationParser(IMarkdownTextExtractor textExtractor, ILogger

_markdownTableParser = new MarkdownTableParser(textExtractor);
_msLearnTableParser = new MsLearnTableParser();
_documentationPreprocessor = new MsLearnDocumentationPreprocessor();
}

public RoslynRules Parse(MsLearnDocumentationRawInfo rawInfo)
{
ArgumentNullException.ThrowIfNull(rawInfo);

rawInfo = _documentationPreprocessor.Process(rawInfo);

_logger.LogInformation("Parsing roslyn rules from MS Learn");

var roslynQualityRules = rawInfo.QualityRuleFileContents.SelectMany(ParseQualityRules).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.Configuin.MsLearn.Models;

namespace Kysect.Configuin.MsLearn;

public class MsLearnDocumentationPreprocessor
{
public MsLearnDocumentationRawInfo Process(MsLearnDocumentationRawInfo info)
{
return new MsLearnDocumentationRawInfo(
info.QualityRuleFileContents.Select(Process).ToList(),
info.StyleRuleFileContents.Select(Process).ToList(),
Process(info.SharpFormattingOptionsContent),
Process(info.DotnetFormattingOptionsContent));
}

public string Process(string input)
{
input.ThrowIfNull();

// TODO: remove this hack
input = input
.Replace("\r\n", "\n")
.Replace("\n", Environment.NewLine);

List<string> lines = input.Split(Environment.NewLine).ToList();

lines = RemoveZones(lines);

return string.Join(Environment.NewLine, lines);
}

private List<string> RemoveZones(List<string> lines)
{
// TODO: Performance is not so good
while (lines.Any(l => l.StartsWith(":::zone ")))
{
int startIndex = lines.FindIndex(l => l.StartsWith(":::zone "));
int endIndex = lines.FindIndex(l => l.StartsWith(":::zone-end"));

if (startIndex == -1 || endIndex == -1 || startIndex >= endIndex)
throw new ArgumentException("Cannot find zones for removing");

if (lines[startIndex].StartsWith(":::zone pivot=\"lang-csharp-vb\""))
{
lines.RemoveAt(endIndex);
lines.RemoveAt(startIndex);
continue;
}

if (lines[startIndex].StartsWith(":::zone pivot=\"lang-fsharp\""))
{
lines.RemoveRange(startIndex, endIndex - startIndex + 1);
continue;
}

throw new ArgumentException($"Unsupported zone {lines[startIndex]}");
}

return lines;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Kysect.Configuin.MsLearn;

namespace Kysect.Configuin.Tests.MsLearnDocumentation;

public class MsLearnDocumentationPreprocessorTests
{
private readonly MsLearnDocumentationPreprocessor _preprocessor = new MsLearnDocumentationPreprocessor();

[Fact]
public void Process_InputWithZones_RemoveOnlyFsharpBlock()
{
var input = """
first line
:::zone pivot="lang-csharp-vb"
second line
:::zone-end
third line
:::zone pivot="lang-fsharp"
fourth line
:::zone-end
fifth line
""";

var expected = """
first line
second line
third line
fifth line
""";

string actual = _preprocessor.Process(input);

actual.Should().Be(expected);
}
}

0 comments on commit e8f5ae4

Please sign in to comment.