Skip to content

Commit 5acb2fd

Browse files
committed
Add support for per-chapter remarks
1 parent a272952 commit 5acb2fd

3 files changed

Lines changed: 129 additions & 20 deletions

File tree

src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public string UpdateUsfm(
2929
UpdateUsfmMarkerBehavior styleBehavior = UpdateUsfmMarkerBehavior.Strip,
3030
IEnumerable<string> preserveParagraphStyles = null,
3131
IEnumerable<IUsfmUpdateBlockHandler> updateBlockHandlers = null,
32-
IEnumerable<string> remarks = null,
32+
IEnumerable<(int, string)> remarks = null,
3333
Func<UsfmUpdateBlockHandlerException, bool> errorHandler = null,
3434
bool compareSegments = false
3535
)

src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class UpdateUsfmParserHandler : ScriptureRefUsfmParserHandlerBase
6060
private readonly HashSet<string> _preserveParagraphStyles;
6161
private readonly Stack<UsfmUpdateBlock> _updateBlocks;
6262
private readonly Stack<IUsfmUpdateBlockHandler> _updateBlockHandlers;
63-
private readonly List<string> _remarks;
63+
private readonly List<(int, string)> _remarks;
6464
private readonly Stack<bool> _replace;
6565
private int _tokenIndex;
6666
private readonly Func<UsfmUpdateBlockHandlerException, bool> _errorHandler;
@@ -76,7 +76,7 @@ public UpdateUsfmParserHandler(
7676
UpdateUsfmMarkerBehavior styleBehavior = UpdateUsfmMarkerBehavior.Strip,
7777
IEnumerable<string> preserveParagraphStyles = null,
7878
IEnumerable<IUsfmUpdateBlockHandler> updateBlockHandlers = null,
79-
IEnumerable<string> remarks = null,
79+
IEnumerable<(int, string)> remarks = null,
8080
Func<UsfmUpdateBlockHandlerException, bool> errorHandler = null,
8181
bool compareSegments = false
8282
)
@@ -107,7 +107,7 @@ public UpdateUsfmParserHandler(
107107
preserveParagraphStyles == null
108108
? new HashSet<string> { "r", "rem" }
109109
: new HashSet<string>(preserveParagraphStyles);
110-
_remarks = remarks?.ToList() ?? new List<string>();
110+
_remarks = remarks?.ToList() ?? new List<(int, string)>();
111111
_errorHandler = errorHandler;
112112
if (_errorHandler == null)
113113
_errorHandler = (error) => false;
@@ -433,26 +433,66 @@ public string GetUsfm(string stylesheetFileName = "usfm.sty")
433433
public string GetUsfm(UsfmStylesheet stylesheet)
434434
{
435435
var tokenizer = new UsfmTokenizer(stylesheet);
436-
List<UsfmToken> tokens = new List<UsfmToken>(_tokens);
437-
if (_remarks.Count() > 0)
436+
var tokens = new List<UsfmToken>(_tokens);
437+
if (_remarks.Count > 0)
438438
{
439-
var remarkTokens = new List<UsfmToken>();
440-
foreach (string remark in _remarks)
439+
var remarkTokensByChapter = new Dictionary<int, List<UsfmToken>>();
440+
foreach ((int chapterNum, string remark) in _remarks)
441441
{
442-
remarkTokens.Add(new UsfmToken(UsfmTokenType.Paragraph, "rem", null, null));
443-
remarkTokens.Add(new UsfmToken(remark));
442+
// Add the remark tokens for each chapter that is to have remarks
443+
if (!remarkTokensByChapter.TryGetValue(chapterNum, out List<UsfmToken> chapterTokens))
444+
{
445+
chapterTokens = new List<UsfmToken>();
446+
remarkTokensByChapter.Add(chapterNum, chapterTokens);
447+
}
448+
449+
chapterTokens.Add(new UsfmToken(UsfmTokenType.Paragraph, "rem", null, null));
450+
chapterTokens.Add(new UsfmToken(remark));
444451
}
445452
if (tokens.Count > 0)
446453
{
447-
int index = 0;
448-
HashSet<string> markersToSkip = new HashSet<string>() { "id", "ide", "rem" };
449-
while (markersToSkip.Contains(tokens[index].Marker))
454+
foreach (KeyValuePair<int, List<UsfmToken>> remarkTokens in remarkTokensByChapter)
450455
{
451-
index++;
452-
if (tokens.Count > index && tokens[index].Type == UsfmTokenType.Text)
456+
int index;
457+
HashSet<string> markersToSkip;
458+
if (remarkTokens.Key == 0)
459+
{
460+
// Add the remarks at the top level of the USFM,
461+
// after the book id, encode, and any initial comments
462+
index = 0;
463+
markersToSkip = new HashSet<string> { "id", "ide", "rem" };
464+
}
465+
else
466+
{
467+
// Add the remarks just after the specified chapter
468+
index = tokens.FindIndex(t =>
469+
t.Type == UsfmTokenType.Chapter
470+
&& int.TryParse(t.Data, out int chapterNumber)
471+
&& chapterNumber == remarkTokens.Key
472+
);
473+
if (index == -1)
474+
continue;
453475
index++;
476+
markersToSkip = new HashSet<string>();
477+
}
478+
479+
if (index >= tokens.Count)
480+
{
481+
// The remark insertion point is at the very end
482+
tokens.AddRange(remarkTokens.Value);
483+
}
484+
else
485+
{
486+
while (markersToSkip.Contains(tokens[index].Marker))
487+
{
488+
index++;
489+
if (tokens.Count > index && tokens[index].Type == UsfmTokenType.Text)
490+
index++;
491+
}
492+
493+
tokens.InsertRange(index, remarkTokens.Value);
494+
}
454495
}
455-
tokens.InsertRange(index, remarkTokens);
456496
}
457497
}
458498

tests/SIL.Machine.Tests/Corpora/UpdateUsfmParserHandlerTests.cs

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ public void GetUsfm_IdTags()
13801380
}
13811381

13821382
[Test]
1383-
public void GetUsfm_PreferExisting_AddRemark()
1383+
public void GetUsfm_PreferExisting_AddRemarkToStart()
13841384
{
13851385
var rows = new List<UpdateUsfmRow>
13861386
{
@@ -1400,7 +1400,7 @@ public void GetUsfm_PreferExisting_AddRemark()
14001400
rows,
14011401
usfm,
14021402
textBehavior: UpdateUsfmTextBehavior.PreferExisting,
1403-
remarks: ["New remark"]
1403+
remarks: [(0, "New remark")]
14041404
);
14051405
string result =
14061406
@"\id MAT - Test
@@ -1419,7 +1419,7 @@ public void GetUsfm_PreferExisting_AddRemark()
14191419
rows,
14201420
target,
14211421
textBehavior: UpdateUsfmTextBehavior.PreferExisting,
1422-
remarks: ["New remark 2"]
1422+
remarks: [(0, "New remark 2")]
14231423
);
14241424
result =
14251425
@"\id MAT - Test
@@ -1436,6 +1436,75 @@ public void GetUsfm_PreferExisting_AddRemark()
14361436
AssertUsfmEquals(target, result);
14371437
}
14381438

1439+
[Test]
1440+
public void GetUsfm_PreferExisting_AddRemarkToChapter()
1441+
{
1442+
var rows = new List<UpdateUsfmRow>
1443+
{
1444+
new UpdateUsfmRow(ScrRef("MAT 2:1"), "Update 1"),
1445+
new UpdateUsfmRow(ScrRef("MAT 2:2"), "Update 2"),
1446+
};
1447+
string usfm =
1448+
@"\id MAT - Test
1449+
\ide UTF-8
1450+
\c 1
1451+
\v 1 Chapter 1, Verse 1
1452+
\c 2
1453+
\rem Existing remark
1454+
\v 1 Some text
1455+
\v 2
1456+
\v 3 Other text
1457+
\c 3
1458+
";
1459+
string target = UpdateUsfm(
1460+
rows,
1461+
usfm,
1462+
textBehavior: UpdateUsfmTextBehavior.PreferExisting,
1463+
remarks: [(2, "New remark"), (3, "Last remark"), (4, "Remark for missing chapter")]
1464+
);
1465+
string result =
1466+
@"\id MAT - Test
1467+
\ide UTF-8
1468+
\c 1
1469+
\v 1 Chapter 1, Verse 1
1470+
\c 2
1471+
\rem New remark
1472+
\rem Existing remark
1473+
\v 1 Some text
1474+
\v 2 Update 2
1475+
\v 3 Other text
1476+
\c 3
1477+
\rem Last remark
1478+
";
1479+
1480+
AssertUsfmEquals(target, result);
1481+
1482+
target = UpdateUsfm(
1483+
rows,
1484+
target,
1485+
textBehavior: UpdateUsfmTextBehavior.PreferExisting,
1486+
remarks: [(1, "New remark 2"), (2, "New remark 3")]
1487+
);
1488+
result =
1489+
@"\id MAT - Test
1490+
\ide UTF-8
1491+
\c 1
1492+
\rem New remark 2
1493+
\v 1 Chapter 1, Verse 1
1494+
\c 2
1495+
\rem New remark 3
1496+
\rem New remark
1497+
\rem Existing remark
1498+
\v 1 Some text
1499+
\v 2 Update 2
1500+
\v 3 Other text
1501+
\c 3
1502+
\rem Last remark
1503+
";
1504+
1505+
AssertUsfmEquals(target, result);
1506+
}
1507+
14391508
[Test]
14401509
public void UpdateBlock_FootnoteInPublishedChapterNumber()
14411510
{
@@ -1536,7 +1605,7 @@ private static string UpdateUsfm(
15361605
UpdateUsfmMarkerBehavior styleBehavior = UpdateUsfmMarkerBehavior.Strip,
15371606
IEnumerable<string>? preserveParagraphStyles = null,
15381607
IEnumerable<IUsfmUpdateBlockHandler>? usfmUpdateBlockHandlers = null,
1539-
IEnumerable<string>? remarks = null,
1608+
IEnumerable<(int, string)>? remarks = null,
15401609
bool compareSegments = false
15411610
)
15421611
{

0 commit comments

Comments
 (0)