Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/MarkdownSnippets/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ public static string TrimBackCommentChars(this string input, int startIndex)
return string.Empty;
}

public static string[] SplitBySpace(this string value) =>
value.Split(' ', StringSplitOptions.RemoveEmptyEntries);

public static string[] Lines(this string value) =>
value.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);

Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownSnippets/Reading/EndFunc.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
delegate bool EndFunc(string line);
delegate bool EndFunc(string line);
14 changes: 0 additions & 14 deletions src/MarkdownSnippets/Reading/ExpressiveCode.cs

This file was deleted.

49 changes: 28 additions & 21 deletions src/MarkdownSnippets/Reading/StartEndTester.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
static class StartEndTester
static class StartEndTester
{
internal static bool IsStartOrEnd(string trimmedLine) =>
IsBeginSnippet(trimmedLine) ||
Expand Down Expand Up @@ -96,37 +96,44 @@ internal static bool IsBeginSnippet(
var substring = line
.TrimBackCommentChars(startIndex);

var match = ExpressiveCode.Pattern.Match(substring);

if (match.Length == 0)
var startArgs = substring.IndexOf('(');
if (startArgs == -1)
{
throw new SnippetReadingException(
$"""
No Key could be derived.
Path: {path}
Line: '{line}'
""");
key = substring.Trim();
}
else
{
substring = substring.Trim();
key = substring[..startArgs].Trim();

if (!substring.EndsWith(')'))
{
throw new SnippetReadingException(
$"""
ExpressiveCode must end with ')`.
Key: {key}
Path: {path}
Line: {line}
""");
}

expressiveCode = substring[(startArgs +1)..^1].Trim();
if (expressiveCode.Length == 0)
{
expressiveCode = null;
}
}

var partOne = match.Groups[1].Value;
var split = partOne.SplitBySpace();
if (split.Length != 1)
if (key.Length == 0)
{
throw new SnippetReadingException(
$"""
Too many parts.
No Key could be derived.
Path: {path}
Line: '{line}'
""");
}

key = split[0];
expressiveCode = match.Groups[2].Value;
if (expressiveCode.Length == 0)
{
expressiveCode = null;
}

if (KeyValidator.IsValidKey(key.AsSpan()))
{
return true;
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/StartEndTester_IsBeginSnippetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,21 @@ public void CanExtractWithExpressiveCodeWithCsharpComment()
Assert.Equal("CodeKey", key);
Assert.Equal("""title="Program.cs" {1-3}""", expressive);
}
[Fact]
public void CanExtractWithExpressiveCodeWithHtmlSnippetTrailingWhitespace()
{
var isBeginSnippet = StartEndTester.IsBeginSnippet("""<!--begin-snippet: CodeKey(title="Program.cs" {1-3}) -->""", "file", out var key, out var block);
Assert.True(isBeginSnippet);
Assert.Equal("CodeKey", key);
Assert.Equal("""title="Program.cs" {1-3}""", block);
}

[Fact]
public void CanExtractWithExpressiveCodeWithCsharpCommentTrailingWhitespace()
{
var isBeginSnippet = StartEndTester.IsBeginSnippet("""/*begin-snippet: CodeKey(title="Program.cs" {1-3}) */""", "file", out var key, out var expressive);
Assert.True(isBeginSnippet);
Assert.Equal("CodeKey", key);
Assert.Equal("""title="Program.cs" {1-3}""", expressive);
}
}