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
11 changes: 7 additions & 4 deletions src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ private bool ReplaceWithNewTokens(UsfmParserState state, bool closed = true)
.Any(t => t.Type == UsfmTokenType.Text && t.Text.Length > 0);

bool useNewTokens =
(
(_textBehavior == UpdateUsfmTextBehavior.StripExisting && !IsInPreservedParagraph(marker))
!IsInPreservedParagraph(marker)
&& (
_textBehavior == UpdateUsfmTextBehavior.StripExisting
|| (HasNewText() && (!existingText || _textBehavior != UpdateUsfmTextBehavior.PreferExisting))
)
&& (!inEmbed || (InNoteText && !inNestedEmbed && _embedBehavior == UpdateUsfmMarkerBehavior.Preserve));
Expand All @@ -433,8 +434,10 @@ private bool ReplaceWithNewTokens(UsfmParserState state, bool closed = true)
else
AddNewTokens();
}

if (existingText && _textBehavior == UpdateUsfmTextBehavior.PreferExisting)
if (
existingText
&& (_textBehavior == UpdateUsfmTextBehavior.PreferExisting || IsInPreservedParagraph(marker))
)
{
if (inEmbed)
ClearNewEmbedTokens();
Expand Down
56 changes: 55 additions & 1 deletion tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,60 @@ public void GetUsfm_StripAllText()
Assess(target, result);
}

[Test]
public void GetUsfm_StripParagraphs_PreserveParagraphStyles()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)>
{
(ScrRef("MAT 1:0/1:rem"), "New remark"),
(ScrRef("MAT 1:0/3:ip"), "Another new remark"),
(ScrRef("MAT 1:1"), "Update 1"),
};
string usfm =
@"\id MAT
\c 1
\rem Update remark
\r reference
\ip This is another remark, but with a different marker
\v 1 This is a verse
";

string target = UpdateUsfm(
rows,
usfm,
textBehavior: UpdateUsfmTextBehavior.StripExisting,
paragraphBehavior: UpdateUsfmMarkerBehavior.Strip
);
string result =
@"\id MAT
\c 1
\rem Update remark
\r reference
\ip Another new remark
\v 1 Update 1
";

Assess(target, result);

var targetDiffParagraph = UpdateUsfm(
rows,
usfm,
textBehavior: UpdateUsfmTextBehavior.StripExisting,
paragraphBehavior: UpdateUsfmMarkerBehavior.Strip,
preserveParagraphStyles: ImmutableHashSet.Create("ip")
);
string resultDiffParagraph =
@"\id MAT
\c 1
\rem New remark
\r
\ip This is another remark, but with a different marker
\v 1 Update 1
";

Assess(targetDiffParagraph, resultDiffParagraph);
}

[Test]
public void GetUsfm_PreserveParagraphs()
{
Expand Down Expand Up @@ -167,7 +221,6 @@ public void GetUsfm_ParagraphInVerse()
\v 2 Verse 2
\p inner verse paragraph
";

Assess(target, result);

string targetStrip = UpdateUsfm(
Expand All @@ -184,6 +237,7 @@ public void GetUsfm_ParagraphInVerse()
\s1
\v 2
";
// Assert.Fail(targetStrip);

Assess(targetStrip, resultStrip);
}
Expand Down
Loading