-
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
11 changed files
with
155 additions
and
6 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
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
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
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
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,85 @@ | ||
namespace Menees.Chords; | ||
|
||
#region Using Directives | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Menees.Chords.Parsers; | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// A line that parses with <see cref="Uri.TryCreate(string, UriKind, out Uri)"/> as an absolute URI. | ||
/// </summary> | ||
public sealed class UriLine : TextEntry | ||
{ | ||
#region Constructors | ||
|
||
private UriLine(string line, Uri uri, IEnumerable<Entry>? annotations = null) | ||
: base(line, annotations) | ||
{ | ||
this.Uri = uri; | ||
} | ||
|
||
#endregion | ||
|
||
#region Public Properties | ||
|
||
/// <summary> | ||
/// Gets the parsed <see cref="Uri"/> for the current line. | ||
/// </summary> | ||
public Uri Uri { get; } | ||
|
||
#endregion | ||
|
||
#region Public Methods | ||
|
||
/// <summary> | ||
/// Tries to parse a header line from the current context. | ||
/// </summary> | ||
/// <param name="context">The current parsing context.</param> | ||
/// <returns>A new instance if <paramref name="context"/>'s line was parsed. Null otherwise.</returns> | ||
public static UriLine? TryParse(LineContext context) | ||
{ | ||
Conditions.RequireNonNull(context); | ||
|
||
UriLine? result = null; | ||
|
||
Lexer lexer = context.CreateLexer(out IReadOnlyList<Entry> annotations); | ||
if (lexer.Read(skipLeadingWhiteSpace: true) && lexer.Token.Type == TokenType.Text) | ||
{ | ||
// We have to pull the token text out here because we're about to move the lexer forward. | ||
string uriText = lexer.Token.Text; | ||
|
||
// Since annotations were removed earlier, make sure there's nothing else on the line. | ||
// And make sure the header text isn't a chord. We won't look for specific header values | ||
// since Ultimate Guitar has many header variations: | ||
// Intro, Outro, Verse, Verse #, Chorus, Interlude, Bridge, Pre-Chorus, Solo, Solo #, Break, Post-Chorus, Pre-Verse | ||
if ((!lexer.Read() || string.IsNullOrEmpty(lexer.ReadToEnd(skipTrailingWhiteSpace: true))) | ||
&& Uri.TryCreate(uriText, UriKind.Absolute, out Uri? uri)) | ||
{ | ||
result = new(uriText, uri, annotations); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
#endregion | ||
|
||
#region Protected Methods | ||
|
||
/// <summary> | ||
/// Writes <see cref="Uri"/>. | ||
/// </summary> | ||
/// <param name="writer">Used to write output.</param> | ||
protected override void WriteWithoutAnnotations(TextWriter writer) | ||
{ | ||
Conditions.RequireNonNull(writer); | ||
writer.Write(this.Uri); | ||
} | ||
|
||
#endregion | ||
} |
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
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
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
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
2 changes: 1 addition & 1 deletion
2
tests/Menees.Chords.Tests/Transformers/ChordOverLyricTransformerTests.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
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,54 @@ | ||
namespace Menees.Chords; | ||
|
||
using Menees.Chords.Parsers; | ||
|
||
[TestClass] | ||
public class UriLineTests | ||
{ | ||
[TestMethod] | ||
public void TryParseValidTest() | ||
{ | ||
Test("https://github.com/menees/Chords/commits/master"); | ||
Test(" https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=net70 "); | ||
Test("https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=net70#install-with-windows-installer"); | ||
Test(@"C:\Windows\System32\aadtb.dll", "file:///C:/Windows/System32/aadtb.dll"); | ||
Test( | ||
"https://learn.microsoft.com/en-us/dotnet/core/install/windows (install-with-windows-installer)", | ||
"https://learn.microsoft.com/en-us/dotnet/core/install/windows", | ||
"(install-with-windows-installer)"); | ||
|
||
static void Test(string text, string? uri = null, string? comment = null) | ||
{ | ||
uri ??= text; | ||
LineContext context = LineContextTests.Create(text); | ||
UriLine line = UriLine.TryParse(context).ShouldNotBeNull(text); | ||
line.Uri.ShouldBe(new Uri(uri)); | ||
if (comment is not null) | ||
{ | ||
line.Annotations.Count.ShouldBe(1); | ||
line.Annotations[0].ShouldBeOfType<Comment>().ToString().ShouldBe(comment); | ||
if (text.EndsWith(comment)) | ||
{ | ||
text = text.Substring(0, text.Length - comment.Length); | ||
} | ||
} | ||
|
||
line.Text.ShouldBe(text.Trim()); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void TryParseInvalidTest() | ||
{ | ||
Test("Not a Uri"); | ||
Test("[A#]"); | ||
Test("Mesa/Boogie"); | ||
|
||
static void Test(string text) | ||
{ | ||
LineContext context = LineContextTests.Create(text); | ||
UriLine? line = UriLine.TryParse(context); | ||
line.ShouldBeNull(); | ||
} | ||
} | ||
} |