Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Aug 13, 2024
1 parent e97df8c commit e49bd29
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/SIL.Machine/Corpora/ScriptureRefUsfmParserHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ IReadOnlyList<UsfmAttribute> attributes
CheckConvertVerseParaToNonVerse(state);
}

public override void OptBreak(UsfmParserState state)
{
if (_curTextType.Count == 0)
_curTextType.Push(ScriptureTextType.NonVerse);
}

protected virtual void StartVerseText(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs) { }

protected virtual void EndVerseText(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs) { }
Expand Down
12 changes: 8 additions & 4 deletions src/SIL.Machine/Corpora/UsfmTextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,20 @@ public override void EndNote(UsfmParserState state, string marker, bool closed)

public override void OptBreak(UsfmParserState state)
{
base.OptBreak(state);

if (_text._includeMarkers)
{
_rowTexts.Peek().Append("//");
if (_rowTexts.Count > 0)
_rowTexts.Peek().Append("//");
else
_rowTexts.Push(new StringBuilder("//"));
}
else if (CurrentTextType != ScriptureTextType.Verse || state.IsVerseText)
else if (CurrentTextType != ScriptureTextType.Verse || state.IsVerseText) //When is this not true?
{
if (_rowTexts.Count == 0)
return;
_rowTexts.Peek().TrimEnd();
}
base.OptBreak(state);
}

public override void Text(UsfmParserState state, string text)
Expand Down
54 changes: 54 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/UsfmMemoryTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ public void GetRows_TriplicateVerse()
});
}

[Test]
public void GetRows_OptBreak()
{
// a verse paragraph that begins with a non-verse segment followed by a verse segment
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
\v 1 First verse in line // More text
\c 2
\v 1
",
includeAllText: true,
includeMarkers: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(2), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo(@"First verse in line // More text"));
});
}

[Test]
public void GetRows_VersePara_BeginningNonVerseSegment()
{
Expand All @@ -132,6 +153,39 @@ public void GetRows_VersePara_BeginningNonVerseSegment()
Assert.That(rows, Has.Length.EqualTo(4), string.Join(",", rows.ToList().Select(tr => tr.Text)));
}

[Test]
public void GetRows_OptBreakAtBeginning()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\li //
",
includeAllText: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo(""));
});
}

[Test]
public void GetRows_OptBreakAtBeginningIncludeMarkers()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\li //
",
includeAllText: true,
includeMarkers: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo("//"));
});
}

private static TextRow[] GetRows(string usfm, bool includeMarkers = false, bool includeAllText = false)
{
UsfmMemoryText text =
Expand Down

0 comments on commit e49bd29

Please sign in to comment.