2
2
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
3
// See the LICENSE file in the project root for more information
4
4
5
+ using System . Diagnostics . CodeAnalysis ;
5
6
using System . Text . RegularExpressions ;
7
+ using Elastic . Markdown . Myst ;
6
8
7
9
namespace Elastic . Markdown . Helpers ;
8
10
@@ -14,22 +16,60 @@ internal static partial class InterpolationRegex
14
16
15
17
public static class Interpolation
16
18
{
17
- public static bool ReplaceSubstitutions ( this ReadOnlySpan < char > span , IReadOnlyDictionary < string , string > ? properties , out string ? replacement )
19
+ public static string ReplaceSubstitutions (
20
+ this string input ,
21
+ ParserContext context
22
+ )
23
+ {
24
+ var span = input . AsSpan ( ) ;
25
+ if ( span . ReplaceSubstitutions ( [ context . Substitutions , context . ContextSubstitutions ] , out var replacement ) )
26
+ return replacement ;
27
+ return input ;
28
+ }
29
+
30
+
31
+ public static bool ReplaceSubstitutions (
32
+ this ReadOnlySpan < char > span ,
33
+ ParserContext context ,
34
+ [ NotNullWhen ( true ) ] out string ? replacement
35
+ ) =>
36
+ span . ReplaceSubstitutions ( [ context . Substitutions , context . ContextSubstitutions ] , out replacement ) ;
37
+
38
+ public static bool ReplaceSubstitutions (
39
+ this ReadOnlySpan < char > span ,
40
+ IReadOnlyDictionary < string , string > ? properties ,
41
+ [ NotNullWhen ( true ) ] out string ? replacement
42
+ )
18
43
{
19
44
replacement = null ;
45
+ if ( properties is null || properties . Count == 0 )
46
+ return false ;
47
+
20
48
if ( span . IndexOf ( "}}" ) < 0 )
21
49
return false ;
22
50
23
- if ( properties is null || properties . Count == 0 )
51
+ return span . ReplaceSubstitutions ( [ properties ] , out replacement ) ;
52
+ }
53
+
54
+ public static bool ReplaceSubstitutions (
55
+ this ReadOnlySpan < char > span ,
56
+ IReadOnlyDictionary < string , string > [ ] properties ,
57
+ [ NotNullWhen ( true ) ] out string ? replacement
58
+ )
59
+ {
60
+ replacement = null ;
61
+ if ( span . IndexOf ( "}}" ) < 0 )
24
62
return false ;
25
63
26
- var substitutions = properties as Dictionary < string , string >
27
- ?? new Dictionary < string , string > ( properties , StringComparer . OrdinalIgnoreCase ) ;
28
- if ( substitutions . Count == 0 )
64
+ if ( properties . Length == 0 || properties . Sum ( p => p . Count ) == 0 )
29
65
return false ;
30
66
67
+ var lookups = properties
68
+ . Select ( p => p as Dictionary < string , string > ?? new Dictionary < string , string > ( p , StringComparer . OrdinalIgnoreCase ) )
69
+ . Select ( d => d . GetAlternateLookup < ReadOnlySpan < char > > ( ) )
70
+ . ToArray ( ) ;
71
+
31
72
var matchSubs = InterpolationRegex . MatchSubstitutions ( ) . EnumerateMatches ( span ) ;
32
- var lookup = substitutions . GetAlternateLookup < ReadOnlySpan < char > > ( ) ;
33
73
34
74
var replaced = false ;
35
75
foreach ( var match in matchSubs )
@@ -39,14 +79,15 @@ public static bool ReplaceSubstitutions(this ReadOnlySpan<char> span, IReadOnlyD
39
79
40
80
var spanMatch = span . Slice ( match . Index , match . Length ) ;
41
81
var key = spanMatch . Trim ( [ '{' , '}' ] ) ;
82
+ foreach ( var lookup in lookups )
83
+ {
84
+ if ( ! lookup . TryGetValue ( key , out var value ) )
85
+ continue ;
42
86
43
- if ( ! lookup . TryGetValue ( key , out var value ) )
44
- continue ;
45
-
46
- replacement ??= span . ToString ( ) ;
47
- replacement = replacement . Replace ( spanMatch . ToString ( ) , value ) ;
48
- replaced = true ;
49
-
87
+ replacement ??= span . ToString ( ) ;
88
+ replacement = replacement . Replace ( spanMatch . ToString ( ) , value ) ;
89
+ replaced = true ;
90
+ }
50
91
}
51
92
52
93
return replaced ;
0 commit comments