Skip to content

Commit

Permalink
Clean up section headers and bad trailing lines
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Feb 5, 2024
1 parent 21a3c75 commit 86c12c5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
57 changes: 54 additions & 3 deletions src/Menees.Chords/Parsers/Cleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
/// <item>Trims trailing whitespace from each line</item>
/// <item>Removes alternating blank lines</item>
/// <item>Removes consecutive blank lines</item>
/// <item>Ensures blank line before [Section] header</item>
/// <item>Removes blank line after [Section] header</item>
/// <item>Removes leading and trailing blank lines</item>
/// <item>Removes known useless trailing lines (e.g., X, Set8)</item>
/// </list>
/// This , , and
/// removes consecutive blank lines.
/// </remarks>
public sealed class Cleaner
{
Expand Down Expand Up @@ -109,10 +110,53 @@ private static void RemoveConsecutiveBlankLines(List<string> lines)
}
}

private static void NormalizeSections(List<string> lines)
{
DocumentParser parser = new(
[HeaderLine.TryParse, LyricLine.Parse],
[GroupEntries.ByHeaderLine]);
Document document = Document.Parse(string.Join(Environment.NewLine, lines), parser);
lines.Clear();
foreach (Entry entry in document.Entries)
{
AddEntryLines(entry);
}

void AddEntryLines(Entry entry)
{
if (entry is IEntryContainer container)
{
bool checkForPostHeaderBlankLine = false;
foreach (Entry subentry in container.Entries)
{
if (!checkForPostHeaderBlankLine || subentry is not BlankLine)
{
AddEntryLines(subentry);
checkForPostHeaderBlankLine = false;
}

if (subentry is HeaderLine)
{
checkForPostHeaderBlankLine = true;
}
}
}
else
{
if (entry is HeaderLine && lines.Count > 0 && !string.IsNullOrEmpty(lines[^1]))
{
lines.Add(string.Empty);
}

lines.Add(entry.ToString());
}
}
}

private static void RemoveLeadingAndTrailingBlankLines(List<string> lines)
{
// Trailing blank lines are more common than leading blank lines, so remove them first.
while (lines.Count > 0 && string.IsNullOrEmpty(lines[lines.Count - 1]))
while (lines.Count > 0 && string.IsNullOrEmpty(lines[^1]))
{
lines.RemoveAt(lines.Count - 1);
}
Expand Down Expand Up @@ -145,8 +189,15 @@ private string Clean()
}

RemoveConsecutiveBlankLines(lines);
NormalizeSections(lines);
RemoveLeadingAndTrailingBlankLines(lines);

HashSet<string> badTrailingLines = new(ChordParser.Comparer) { "X", "Set8" };
while (lines.Count > 0 && badTrailingLines.Contains(lines[^1]))
{
lines.RemoveAt(lines.Count - 1);
}

string result = string.Join(Environment.NewLine, lines);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Chords/Parsers/LineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static Comment CreateComment(string text, string start, string end)
return result;
}

private IReadOnlyList<Entry> SplitAnnotations(out int annotationStartIndex)
private List<Entry> SplitAnnotations(out int annotationStartIndex)
{
List<Entry> result = [];
annotationStartIndex = this.LineText.Length;
Expand Down
18 changes: 17 additions & 1 deletion tests/Menees.Chords.Tests/Parsers/CleanerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,23 @@ Line 7
[TestMethod]
public void NoProblemsTest()
{
Test("Line 1\nLine 2\n\nLine 4", "Line 1\nLine 2\n\nLine 4");
Test("Line 1\nLine 2\n\nLine 4\n\n[Outro]\nLine 7", "Line 1\nLine 2\n\nLine 4\n\n[Outro]\nLine 7");
}

[TestMethod]
public void BadTrailingLines()
{
Test("Line 1\nLine 2\nX", "Line 1\nLine 2");
Test("Line 1\nLine 2\nSet8", "Line 1\nLine 2");
Test("Line 1\nLine 2\nSet8\nX", "Line 1\nLine 2");
}

[TestMethod]
public void NormalizeSections()
{
Test("L1\n\n[Intro]\n\nL4", "L1\n\n[Intro]\nL4");
Test("L1\n[Intro]\nL4", "L1\n\n[Intro]\nL4");
Test("L1\n[Intro]\n\nL4", "L1\n\n[Intro]\nL4");
}

#endregion
Expand Down

0 comments on commit 86c12c5

Please sign in to comment.