-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Sources/Kysect.Configuin.MsLearn/MsLearnDocumentationPreprocessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Sources/Kysect.Configuin.Tests/MsLearnDocumentation/MsLearnDocumentationPreprocessorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |