@@ -191,7 +191,9 @@ def __init__(
191
191
callback : t .Callable [
192
192
[str , int , int , bool , bool , str , nodes .literal_block ], None
193
193
],
194
- warn_unknown_block : t .Callable [[int | str , int , nodes .literal_block ], None ],
194
+ warn_unknown_block : t .Callable [
195
+ [int | str , int , nodes .literal_block , bool ], None
196
+ ],
195
197
):
196
198
super ().__init__ (document )
197
199
self .__content_lines = content .splitlines ()
@@ -216,7 +218,9 @@ def visit_literal_block(self, node: nodes.literal_block) -> None:
216
218
"""
217
219
if "antsibull-code-block" not in node .attributes :
218
220
# This could be a `::` block, or something else (unknown)
219
- self .__warn_unknown_block (node .line or "unknown" , 0 , node )
221
+ self .__warn_unknown_block (
222
+ node .line or "unknown" , 0 , node , bool (node .attributes ["classes" ])
223
+ )
220
224
raise nodes .SkipNode
221
225
222
226
language = node .attributes ["antsibull-code-language" ]
@@ -316,13 +320,22 @@ def find_code_blocks_in_document(
316
320
document : nodes .document ,
317
321
content : str ,
318
322
warn_unknown_block : t .Callable [[int | str , int , str ], None ] | None = None ,
323
+ warn_unknown_block_w_unknown_info : (
324
+ t .Callable [[int | str , int , str , bool ], None ] | None
325
+ ) = None ,
319
326
) -> t .Generator [CodeBlockInfo ]:
320
327
"""
321
328
Given a parsed RST document, finds all code blocks.
322
329
323
330
All code blocks must be parsed with special directives
324
331
(see ``get_code_block_directives()``) that have appropriate metadata
325
332
registered with ``mark_antsibull_code_block()``.
333
+
334
+ You can provide callbacks:
335
+ * ``warn_unknown_block()`` will be called for every literal block
336
+ that's of unknown origin.
337
+ * ``warn_unknown_block_w_unknown_info()`` will be called for every
338
+ literal block that's of known or unknown origin.
326
339
"""
327
340
# If someone can figure out how to yield from a sub-function, we can avoid
328
341
# using this ugly list
@@ -357,9 +370,14 @@ def warn_unknown_block_cb(
357
370
line : int | str ,
358
371
col : int ,
359
372
node : nodes .literal_block ,
373
+ unknown_directive : bool ,
360
374
) -> None :
361
- if warn_unknown_block :
375
+ if warn_unknown_block and unknown_directive :
362
376
warn_unknown_block (line , col , node .rawsource )
377
+ if warn_unknown_block_w_unknown_info :
378
+ warn_unknown_block_w_unknown_info (
379
+ line , col , node .rawsource , unknown_directive
380
+ )
363
381
364
382
# Process the document
365
383
try :
@@ -371,20 +389,29 @@ def warn_unknown_block_cb(
371
389
yield from results
372
390
373
391
374
- def find_code_blocks (
392
+ def find_code_blocks ( # pylint: disable=too-many-arguments
375
393
content : str ,
376
394
* ,
377
395
path : str | os .PathLike [str ] | None = None ,
378
396
root_prefix : str | os .PathLike [str ] | None = None ,
379
397
extra_directives : Mapping [str , t .Type [Directive ]] | None = None ,
380
398
warn_unknown_block : t .Callable [[int | str , int , str ], None ] | None = None ,
399
+ warn_unknown_block_w_unknown_info : (
400
+ t .Callable [[int | str , int , str , bool ], None ] | None
401
+ ) = None ,
381
402
) -> t .Generator [CodeBlockInfo ]:
382
403
"""
383
404
Given a RST document, finds all code blocks.
384
405
385
406
To add support for own types of code blocks, you can pass these
386
407
as ``extra_directives``. Use ``mark_antsibull_code_block()`` to
387
408
mark them to be found by ``find_code_blocks()``.
409
+
410
+ You can provide callbacks:
411
+ * ``warn_unknown_block()`` will be called for every literal block
412
+ that's of unknown origin.
413
+ * ``warn_unknown_block_w_unknown_info()`` will be called for every
414
+ literal block that's of known or unknown origin.
388
415
"""
389
416
directives = get_code_block_directives (extra_directives = extra_directives )
390
417
@@ -400,6 +427,7 @@ def find_code_blocks(
400
427
document = doc ,
401
428
content = content ,
402
429
warn_unknown_block = warn_unknown_block ,
430
+ warn_unknown_block_w_unknown_info = warn_unknown_block_w_unknown_info ,
403
431
)
404
432
405
433
0 commit comments