@@ -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
0 commit comments