Skip to content

Commit

Permalink
Ready to review - almost done testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Jan 17, 2025
1 parent ecead42 commit 034e8bc
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 75 deletions.
7 changes: 6 additions & 1 deletion src/SIL.Machine/Corpora/IUsfmParserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ IReadOnlyList<UsfmAttribute> attributes
/// <summary>
/// End of a sub component
/// </summary>
void EndSubComponent(UsfmParserState state, string marker, bool closed);
void EndSubComponent(
UsfmParserState state,
string marker,
IReadOnlyList<UsfmAttribute> attributes,
bool closed
);

/// <summary>
/// Start of a sub component text
Expand Down
8 changes: 6 additions & 2 deletions src/SIL.Machine/Corpora/ScriptureRefUsfmParserHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public override void EndSidebar(UsfmParserState state, string marker, bool close

public override void StartSubComponent(UsfmParserState state, string marker, string caller, string category)
{
if (_curVerseRef.IsDefault)
UpdateVerseRef(state.VerseRef, marker, startAsChildElement: true);

if (CurrentTextType != ScriptureTextType.None && !_duplicateVerse)
{
// if we hit a note in a verse paragraph and we aren't in a verse, then start a non-verse segment
Expand Down Expand Up @@ -237,12 +240,13 @@ private void EndNonVerseText(UsfmParserState state)
_curTextType.Pop();
}

private void UpdateVerseRef(VerseRef verseRef, string marker)
private void UpdateVerseRef(VerseRef verseRef, string marker, bool startAsChildElement = false)
{
if (!VerseRef.AreOverlappingVersesRanges(verseRef, _curVerseRef))
{
_curElements.Clear();
_curElements.Push(new ScriptureElement(0, marker));
int position = startAsChildElement ? 1 : 0;
_curElements.Push(new ScriptureElement(position, marker));
}
_curVerseRef = verseRef;
}
Expand Down
9 changes: 7 additions & 2 deletions src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,20 @@ public override void StartSubComponent(UsfmParserState state, string marker, str
base.StartSubComponent(state, marker, caller, category);
}

public override void EndSubComponent(UsfmParserState state, string marker, bool closed)
public override void EndSubComponent(
UsfmParserState state,
string marker,
IReadOnlyList<UsfmAttribute> attributes,
bool closed
)
{
// strip out notes in verses that are being replaced
if (ReplaceWithNewTokens(state, closed: closed, endSubComponent: true))
SkipTokens(state);
else
CollectTokens(state);

base.EndSubComponent(state, marker, closed);
base.EndSubComponent(state, marker, attributes, closed);
}

public override void Ref(UsfmParserState state, string marker, string display, string target)
Expand Down
4 changes: 2 additions & 2 deletions src/SIL.Machine/Corpora/UsfmParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public bool ProcessToken()
State.SpecialTokenCount += 3;
}

State.Push(new UsfmParserElement(UsfmElementType.SubComponent, token.Marker));
State.Push(new UsfmParserElement(UsfmElementType.SubComponent, token.Marker, token.Attributes));

Handler?.StartSubComponent(State, token.Marker, token.Data, noteCategory);
break;
Expand Down Expand Up @@ -657,7 +657,7 @@ private void CloseElement(bool closed = false)
Handler?.EndChar(State, element.Marker, element.Attributes, closed);
break;
case UsfmElementType.SubComponent:
Handler?.EndSubComponent(State, element.Marker, closed);
Handler?.EndSubComponent(State, element.Marker, element.Attributes, closed);
break;
case UsfmElementType.SubComponentText:
Handler?.EndSubComponentText(State);
Expand Down
7 changes: 6 additions & 1 deletion src/SIL.Machine/Corpora/UsfmParserHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ bool closed

public virtual void StartSubComponent(UsfmParserState state, string marker, string caller, string category) { }

public virtual void EndSubComponent(UsfmParserState state, string marker, bool closed) { }
public virtual void EndSubComponent(
UsfmParserState state,
string marker,
IReadOnlyList<UsfmAttribute> attributes,
bool closed
) { }

public virtual void StartSubComponentText(UsfmParserState state) { }

Expand Down
12 changes: 10 additions & 2 deletions src/SIL.Machine/Corpora/UsfmTextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,17 @@ public override void StartSubComponent(UsfmParserState state, string marker, str
OutputMarker(state);
}

public override void EndSubComponent(UsfmParserState state, string marker, bool closed)
public override void EndSubComponent(
UsfmParserState state,
string marker,
IReadOnlyList<UsfmAttribute> attributes,
bool closed
)
{
base.EndSubComponent(state, marker, closed);
base.EndSubComponent(state, marker, attributes, closed);

if (_text._includeMarkers && attributes != null && state.PrevToken?.Type == UsfmTokenType.Attribute)
_rowTexts.Peek().Append(state.PrevToken);

if (closed)
OutputMarker(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
\s1 Chapter \it Two \it*
\p
\p
\v 1 Chapter \add two\add*, verse \f + \fr 2:1: \ft This is a\bd footnote.\bd*\f*one.
\v 1 Chapter \add two\add*, verse \f + \fr 2:1: \ft This is a \bd footnote.\bd*\f*one.
\v 2-3 Chapter two, // verse \fm ∆\fm*two.
\esb
\ms This is a sidebar
Expand All @@ -41,7 +41,7 @@
\p
\v 6 Bad verse. \x - \xo 2:3-4 \xt Cool Book 3:24 \xta The annotation \x* and more content.
\p
\v 5 Chapter two, verse five \rq (MAT 3:1)\rq*.
\v 5 Chapter two, verse five\rq (MAT 3:1)\rq*.
\v 7a Chapter two, verse seven A,
\s Section header \ts-s\*
\p
Expand Down
12 changes: 6 additions & 6 deletions tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public void GetUsfm_NonVerse_CharStyle()
[Test]
public void GetUsfm_NonVerse_Paragraph()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)> { (ScrRef("MAT 1:0/8:s"), "The first chapter.") };
var rows = new List<(IReadOnlyList<ScriptureRef>, string)> { (ScrRef("MAT 1:0/9:s"), "The first chapter.") };

string target = UpdateUsfm(rows);
Assert.That(target, Contains.Substring("\\s The first chapter.\r\n"));
Expand Down Expand Up @@ -422,7 +422,7 @@ public void GetUsfm_NonVerse_KeepNote()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)>
{
(ScrRef("MAT 1:0/3:ip"), "The introductory paragraph.")
(ScrRef("MAT 1:0/4:ip"), "The introductory paragraph.")
};

string target = UpdateUsfm(rows, subComponentBehavior: UpdateUsfmIntraVerseMarkerBehavior.Preserve);
Expand All @@ -437,7 +437,7 @@ public void GetUsfm_NonVerse_SkipNote()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)>
{
(ScrRef("MAT 1:0/3:ip"), "The introductory paragraph.")
(ScrRef("MAT 1:0/4:ip"), "The introductory paragraph.")
};

string target = UpdateUsfm(rows, subComponentBehavior: UpdateUsfmIntraVerseMarkerBehavior.Strip);
Expand All @@ -449,8 +449,8 @@ public void GetUsfm_NonVerse_ReplaceNote()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)>
{
(ScrRef("MAT 1:0/3:ip"), "The introductory paragraph."),
(ScrRef("MAT 1:0/3:ip/1:fe"), "This is a new endnote.")
(ScrRef("MAT 1:0/4:ip"), "The introductory paragraph."),
(ScrRef("MAT 1:0/4:ip/1:fe"), "This is a new endnote.")
};

string target = UpdateUsfm(rows);
Expand Down Expand Up @@ -509,7 +509,7 @@ public void GetUsfm_Verse_PretranslationsBeforeText()
(ScrRef("GEN 1:3"), "Pretranslations before the start"),
(ScrRef("GEN 1:4"), "Pretranslations before the start"),
(ScrRef("GEN 1:5"), "Pretranslations before the start"),
(ScrRef("MAT 1:0/3:ip"), "The introductory paragraph.")
(ScrRef("MAT 1:0/4:ip"), "The introductory paragraph.")
};

string target = UpdateUsfm(rows);
Expand Down
Loading

0 comments on commit 034e8bc

Please sign in to comment.