diff --git a/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs b/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs index c63fcd80..4ce564a0 100644 --- a/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs +++ b/src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs @@ -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)); @@ -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(); diff --git a/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs b/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs index 0145f4db..4a2eb548 100644 --- a/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs +++ b/tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs @@ -98,6 +98,60 @@ public void GetUsfm_StripAllText() Assess(target, result); } + [Test] + public void GetUsfm_StripParagraphs_PreserveParagraphStyles() + { + var rows = new List<(IReadOnlyList, 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() { @@ -167,7 +221,6 @@ public void GetUsfm_ParagraphInVerse() \v 2 Verse 2 \p inner verse paragraph "; - Assess(target, result); string targetStrip = UpdateUsfm( @@ -184,6 +237,7 @@ public void GetUsfm_ParagraphInVerse() \s1 \v 2 "; + // Assert.Fail(targetStrip); Assess(targetStrip, resultStrip); }