@@ -33,8 +33,14 @@ protected override EnhancedCodeBlock CreateFencedBlock(BlockProcessor processor)
33
33
34
34
var lineSpan = processor . Line . AsSpan ( ) ;
35
35
var codeBlock = lineSpan . IndexOf ( "{applies_to}" ) > - 1
36
- ? new AppliesToDirective ( this , context ) { IndentCount = processor . Indent }
37
- : new EnhancedCodeBlock ( this , context ) { IndentCount = processor . Indent } ;
36
+ ? new AppliesToDirective ( this , context )
37
+ {
38
+ IndentCount = processor . Indent
39
+ }
40
+ : new EnhancedCodeBlock ( this , context )
41
+ {
42
+ IndentCount = processor . Indent
43
+ } ;
38
44
39
45
if ( processor . TrackTrivia )
40
46
{
@@ -78,7 +84,7 @@ public override bool Close(BlockProcessor processor, Block block)
78
84
79
85
codeBlock . Language = (
80
86
( codeBlock . Info ? . IndexOf ( '{' ) ?? - 1 ) != - 1
81
- ? codeBlock . Arguments
87
+ ? codeBlock . Arguments ? . Split ( ) [ 0 ]
82
88
: codeBlock . Info
83
89
) ?? "unknown" ;
84
90
@@ -101,7 +107,7 @@ public override bool Close(BlockProcessor processor, Block block)
101
107
return base . Close ( processor , block ) ;
102
108
103
109
if ( codeBlock is not AppliesToDirective appliesToDirective )
104
- ProcessCallOuts ( lines , language , codeBlock , context ) ;
110
+ ProcessCodeBlock ( lines , language , codeBlock , context ) ;
105
111
else
106
112
ProcessAppliesToDirective ( appliesToDirective , lines ) ;
107
113
@@ -128,9 +134,32 @@ private static void ProcessAppliesToDirective(AppliesToDirective appliesToDirect
128
134
}
129
135
}
130
136
131
- private static void ProcessCallOuts ( StringLineGroup lines , string language , EnhancedCodeBlock codeBlock ,
137
+ private static void ProcessCodeBlock (
138
+ StringLineGroup lines ,
139
+ string language ,
140
+ EnhancedCodeBlock codeBlock ,
132
141
ParserContext context )
133
142
{
143
+ string argsString ;
144
+ if ( codeBlock . Arguments == null )
145
+ argsString = "" ;
146
+ else if ( codeBlock . Info ? . IndexOf ( '{' ) == - 1 )
147
+ argsString = codeBlock . Arguments ?? "" ;
148
+ else
149
+ {
150
+ // if the code block starts with {code-block} and is followed by a language, we need to skip the language
151
+ var parts = codeBlock . Arguments . Split ( ) ;
152
+ argsString = parts . Length > 1 && CodeBlock . Languages . Contains ( parts [ 0 ] )
153
+ ? string . Join ( " " , parts [ 1 ..] )
154
+ : codeBlock . Arguments ;
155
+ }
156
+
157
+ var codeBlockArgs = CodeBlockArguments . Default ;
158
+ if ( ! CodeBlockArguments . TryParse ( argsString , out var codeArgs ) )
159
+ codeBlock . EmitError ( $ "Unable to parse code block arguments: { argsString } . Valid arguments are { CodeBlockArguments . KnownKeysString } .") ;
160
+ else
161
+ codeBlockArgs = codeArgs ;
162
+
134
163
var callOutIndex = 0 ;
135
164
var originatingLine = 0 ;
136
165
for ( var index = 0 ; index < lines . Lines . Length ; index ++ )
@@ -146,45 +175,49 @@ private static void ProcessCallOuts(StringLineGroup lines, string language, Enha
146
175
}
147
176
148
177
var span = line . Slice . AsSpan ( ) ;
149
-
150
- if ( span . ReplaceSubstitutions ( context . YamlFrontMatter ? . Properties , out var frontMatterReplacement ) )
178
+ if ( codeBlockArgs . UseSubstitutions )
151
179
{
152
- var s = new StringSlice ( frontMatterReplacement ) ;
153
- lines . Lines [ index ] = new StringLine ( ref s ) ;
154
- span = lines . Lines [ index ] . Slice . AsSpan ( ) ;
155
- }
180
+ if ( span . ReplaceSubstitutions ( context . YamlFrontMatter ? . Properties , out var frontMatterReplacement ) )
181
+ {
182
+ var s = new StringSlice ( frontMatterReplacement ) ;
183
+ lines . Lines [ index ] = new StringLine ( ref s ) ;
184
+ span = lines . Lines [ index ] . Slice . AsSpan ( ) ;
185
+ }
156
186
157
- if ( span . ReplaceSubstitutions ( context . Substitutions , out var globalReplacement ) )
158
- {
159
- var s = new StringSlice ( globalReplacement ) ;
160
- lines . Lines [ index ] = new StringLine ( ref s ) ;
161
- span = lines . Lines [ index ] . Slice . AsSpan ( ) ;
187
+ if ( span . ReplaceSubstitutions ( context . Substitutions , out var globalReplacement ) )
188
+ {
189
+ var s = new StringSlice ( globalReplacement ) ;
190
+ lines . Lines [ index ] = new StringLine ( ref s ) ;
191
+ span = lines . Lines [ index ] . Slice . AsSpan ( ) ;
192
+ }
162
193
}
163
194
164
-
165
195
if ( codeBlock . OpeningFencedCharCount > 3 )
166
196
continue ;
167
197
168
- List < CallOut > callOuts = [ ] ;
169
- var hasClassicCallout = span . IndexOf ( "<" ) > 0 && span . LastIndexOf ( ">" ) == span . Length - 1 ;
170
- if ( hasClassicCallout )
198
+ if ( codeBlockArgs . UseCallouts )
171
199
{
172
- var matchClassicCallout = CallOutParser . CallOutNumber ( ) . EnumerateMatches ( span ) ;
173
- callOuts . AddRange (
174
- EnumerateAnnotations ( matchClassicCallout , ref span , ref callOutIndex , originatingLine , false )
175
- ) ;
176
- }
200
+ List < CallOut > callOuts = [ ] ;
201
+ var hasClassicCallout = span . IndexOf ( "<" ) > 0 && span . LastIndexOf ( ">" ) == span . Length - 1 ;
202
+ if ( hasClassicCallout )
203
+ {
204
+ var matchClassicCallout = CallOutParser . CallOutNumber ( ) . EnumerateMatches ( span ) ;
205
+ callOuts . AddRange (
206
+ EnumerateAnnotations ( matchClassicCallout , ref span , ref callOutIndex , originatingLine , false )
207
+ ) ;
208
+ }
209
+
210
+ // only support magic callouts for smaller line lengths
211
+ if ( callOuts . Count == 0 && span . Length < 200 )
212
+ {
213
+ var matchInline = CallOutParser . MathInlineAnnotation ( ) . EnumerateMatches ( span ) ;
214
+ callOuts . AddRange (
215
+ EnumerateAnnotations ( matchInline , ref span , ref callOutIndex , originatingLine , true )
216
+ ) ;
217
+ }
177
218
178
- // only support magic callouts for smaller line lengths
179
- if ( callOuts . Count == 0 && span . Length < 200 )
180
- {
181
- var matchInline = CallOutParser . MathInlineAnnotation ( ) . EnumerateMatches ( span ) ;
182
- callOuts . AddRange (
183
- EnumerateAnnotations ( matchInline , ref span , ref callOutIndex , originatingLine , true )
184
- ) ;
219
+ codeBlock . CallOuts . AddRange ( callOuts ) ;
185
220
}
186
-
187
- codeBlock . CallOuts . AddRange ( callOuts ) ;
188
221
}
189
222
190
223
//update string slices to ignore call outs
0 commit comments