@@ -149,6 +149,13 @@ public CellRecord GetBodyCell(int row, int col)
149
149
// Processors
150
150
//
151
151
152
+ public class AdvancedMatch
153
+ {
154
+ public string Trigger ;
155
+ public string Pattern ;
156
+ public string Replace ;
157
+ }
158
+
152
159
public class ItemProcessor
153
160
{
154
161
//
@@ -177,8 +184,20 @@ public ItemProcessor(ImportExportTableRecord record, ExportTableAasEntitiesItem
177
184
private Regex regexReplacements = null ;
178
185
private Regex regexStop = null ;
179
186
private Regex regexCommands = null ;
187
+
180
188
private Dictionary < string , string > repDict = null ;
181
189
190
+ private List < AdvancedMatch > _advancedMatches = new List < AdvancedMatch > ( ) ;
191
+
192
+ private void advancedRep ( string trigger , string pattern , string replace )
193
+ {
194
+ _advancedMatches . Add ( new AdvancedMatch ( ) {
195
+ Trigger = trigger ? . Trim ( ) . ToLower ( ) ,
196
+ Pattern = pattern ,
197
+ Replace = replace
198
+ } ) ;
199
+ }
200
+
182
201
private void rep ( string tag , string value )
183
202
{
184
203
tag = tag ? . Trim ( ) . ToLower ( ) ;
@@ -283,12 +302,31 @@ private void repListOfLangStr(string head, List<Aas.ILangStringPreferredNameType
283
302
repLangStr ( head , ls ) ;
284
303
}
285
304
305
+ protected string escapeLiteralsToFormat ( string input )
306
+ {
307
+ // access
308
+ if ( Record == null )
309
+ return input ;
310
+
311
+ // switch
312
+ var fmt = ( ImportExportTableRecord . FormatEnum ) Record . Format ;
313
+ if ( fmt == ImportExportTableRecord . FormatEnum . MarkdownGH ||
314
+ fmt == ImportExportTableRecord . FormatEnum . AsciiDoc )
315
+ {
316
+ // escape __00__
317
+ input = input . Replace ( @"__00__" , @"\__00__" ) ;
318
+ input = input . Replace ( @"__000__" , @"\__000__" ) ;
319
+ }
320
+
321
+ return input ;
322
+ }
323
+
286
324
private void repReferable ( string head , Aas . IReferable rf )
287
325
{
288
326
//-9- {Referable}.{idShort, category, description, description[@en..], elementName,
289
327
// elementAbbreviation, parent}
290
328
if ( rf . IdShort != null )
291
- rep ( head + "idShort" , rf . IdShort ) ;
329
+ rep ( head + "idShort" , escapeLiteralsToFormat ( rf . IdShort ) ) ;
292
330
if ( rf . Category != null )
293
331
rep ( head + "category" , rf . Category ) ;
294
332
@@ -310,7 +348,8 @@ private void repReferable(string head, Aas.IReferable rf)
310
348
rf . GetSelfDescription ( ) ? . ElementAbbreviation ) ;
311
349
}
312
350
if ( rf is Aas . IReferable rfpar )
313
- rep ( head + "parent" , "" + ( rfpar . IdShort != null ? rfpar . IdShort : "-" ) ) ;
351
+ rep ( head + "parent" , escapeLiteralsToFormat (
352
+ "" + ( rfpar . IdShort != null ? rfpar . IdShort : "-" ) ) ) ;
314
353
315
354
// further details
316
355
List < string > details = new List < string > ( ) ;
@@ -413,11 +452,33 @@ private void repReference(string head, string refName, Aas.IReference rid)
413
452
}
414
453
}
415
454
455
+ private void repReferenceList ( string head , string refName , List < Aas . IReference > rids )
456
+ {
457
+ if ( refName == null )
458
+ return ;
459
+
460
+ rep ( head + refName + "" , "" + rids ? . ToStringExtended ( 2 , ", " ) ) ;
461
+ rep ( head + refName + "[br]" , "" + rids ? . ToStringExtended ( 2 , "\r \n %BRPOST%%BRPOST%" ) ) ;
462
+
463
+ advancedRep (
464
+ trigger : "" + head + refName ,
465
+ pattern : head + refName + @"\[label=([^]]*)\]" ,
466
+ replace : ( rids ? . IsValid ( ) != true ) ? ""
467
+ : "%GROUP1%: " + rids ? . ToStringExtended ( 2 , ", " ) + "" ) ;
468
+
469
+ advancedRep (
470
+ trigger : "" + head + refName ,
471
+ pattern : head + refName + @"\[br-label=([^]]*)\]" ,
472
+ replace : ( rids ? . IsValid ( ) != true ) ? ""
473
+ : "%GROUP1%: " + rids ? . ToStringExtended ( 2 , ", " ) + "%BRPOST%%BRPOST%" ) ;
474
+ }
475
+
416
476
public void Start ( )
417
477
{
418
478
// init Regex
419
479
// nice tester: http://regexstorm.net/tester
420
- regexReplacements = new Regex ( @"%([a-zA-Z0-9.@\[\]]+)%" , RegexOptions . IgnoreCase ) ;
480
+ // MIHO 2024-06-12: added "=-" to first regex!
481
+ regexReplacements = new Regex ( @"%([a-zA-Z0-9.@\[\]=-]+)%" , RegexOptions . IgnoreCase ) ;
421
482
regexStop = new Regex ( @"^(.*?)%stop%(.*)$" , RegexOptions . IgnoreCase ) ;
422
483
regexCommands = new Regex ( @"(%([A-Za-z0-9-_]+)=(.*?)%)" , RegexOptions . IgnoreCase ) ;
423
484
@@ -464,6 +525,7 @@ public void Start()
464
525
repMultiplicty ( head , parsme . Qualifiers ) ;
465
526
//-1- {Reference} = {semanticId, isCaseOf, unitId}
466
527
repReference ( head , "semanticId" , parsme . SemanticId ) ;
528
+ repReferenceList ( head , "supplSemIds" , parsme . SupplementalSemanticIds ) ;
467
529
}
468
530
469
531
//-1- {Referable} = {SM, SME, CD}
@@ -477,6 +539,7 @@ public void Start()
477
539
repIdentifiable ( head , sm ) ;
478
540
//-1- {Reference} = {semanticId, isCaseOf, unitId}
479
541
repReference ( head , "semanticId" , sm . SemanticId ) ;
542
+ repReferenceList ( head , "supplSemIds" , sm . SupplementalSemanticIds ) ;
480
543
}
481
544
482
545
if ( sme != null )
@@ -486,6 +549,7 @@ public void Start()
486
549
repQualifiable ( head , sme . Qualifiers ) ;
487
550
repMultiplicty ( head , sme . Qualifiers ) ;
488
551
repReference ( head , "semanticId" , sme . SemanticId ) ;
552
+ repReferenceList ( head , "supplSemIds" , sme . SupplementalSemanticIds ) ;
489
553
490
554
//-2- SME.value
491
555
@@ -631,6 +695,16 @@ public void Start()
631
695
}
632
696
}
633
697
698
+ private static bool IsRegexMatchOne ( string input , string pattern , out string matchOne )
699
+ {
700
+ var m = Regex . Match ( input , pattern ) ;
701
+ matchOne = "" ;
702
+ if ( ! m . Success || m . Groups . Count < 2 )
703
+ return false ;
704
+ matchOne = m . Groups [ 1 ] . ToString ( ) ;
705
+ return true ;
706
+ }
707
+
634
708
// see: https://codereview.stackexchange.com/questions/119519/
635
709
// regex-to-first-match-then-replace-found-matches
636
710
public static string Replace ( string s , int index , int length , string replacement )
@@ -668,15 +742,51 @@ public void ProcessCellRecord(CellRecord cr)
668
742
continue ;
669
743
670
744
// OK, found a placeholder-tag to replace
671
- var tag = "" + match . Groups [ 1 ] . Value . Trim ( ) . ToLower ( ) ;
745
+ var tag = "" + match . Groups [ 1 ] . Value . Trim ( ) ;
746
+ var tagLC = tag . ToLower ( ) ;
747
+
748
+ // try match advanced
749
+ string advancedReplace = null ;
750
+ foreach ( var am in _advancedMatches )
751
+ // fast condition
752
+ if ( tagLC . Contains ( am . Trigger ) )
753
+ {
754
+ // sufficient condition
755
+ // it is important to do the matching (for the group) on
756
+ // upper-/ lowercase
757
+ var m = Regex . Match ( tag , am . Pattern , RegexOptions . IgnoreCase ) ;
758
+ if ( m . Success && m . Groups . Count >= 2 )
759
+ {
760
+ // do the replace
761
+ advancedReplace = am . Replace ;
762
+
763
+ // debug
764
+ if ( am . Replace != "" )
765
+ ;
766
+
767
+ // now care for regognized groups
768
+ for ( int i = 1 ; i < m . Groups . Count ; i ++ )
769
+ advancedReplace = advancedReplace
770
+ . Replace ( $ "%GROUP{ i } %", m . Groups [ i ] . ToString ( ) ) ;
771
+ }
772
+ }
672
773
673
- if ( repDict . ContainsKey ( tag ) )
774
+ // further matching and actual replace of input
775
+ if ( advancedReplace != null )
674
776
{
675
- input = Replace ( input , match . Index , match . Length , repDict [ tag ] ) ;
777
+ // already prepared
778
+ input = Replace ( input , match . Index , match . Length , advancedReplace ) ;
676
779
NumberReplacements ++ ;
677
780
}
678
781
else
679
- if ( tag == "stop" )
782
+ if ( repDict . ContainsKey ( tagLC ) )
783
+ {
784
+ // use dictionary value
785
+ input = Replace ( input , match . Index , match . Length , repDict [ tagLC ] ) ;
786
+ NumberReplacements ++ ;
787
+ }
788
+ else
789
+ if ( tagLC == "stop" )
680
790
{
681
791
// do nothing! see below!
682
792
;
0 commit comments