Skip to content

Commit 34f07ed

Browse files
authored
Allow to find all literal blocks without language (#15)
* Forgot changelog fragment. * Allow to find all literal blocks without language.
1 parent b5551e7 commit 34f07ed

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
minor_changes:
2+
- "Allow to find all literal blocks without language
3+
(https://github.com/ansible-community/antsibull-docutils/pull/15)."

src/antsibull_docutils/rst_code_finder.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ def __init__(
191191
callback: t.Callable[
192192
[str, int, int, bool, bool, str, nodes.literal_block], None
193193
],
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+
],
195197
):
196198
super().__init__(document)
197199
self.__content_lines = content.splitlines()
@@ -216,7 +218,9 @@ def visit_literal_block(self, node: nodes.literal_block) -> None:
216218
"""
217219
if "antsibull-code-block" not in node.attributes:
218220
# 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+
)
220224
raise nodes.SkipNode
221225

222226
language = node.attributes["antsibull-code-language"]
@@ -316,13 +320,22 @@ def find_code_blocks_in_document(
316320
document: nodes.document,
317321
content: str,
318322
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,
319326
) -> t.Generator[CodeBlockInfo]:
320327
"""
321328
Given a parsed RST document, finds all code blocks.
322329
323330
All code blocks must be parsed with special directives
324331
(see ``get_code_block_directives()``) that have appropriate metadata
325332
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.
326339
"""
327340
# If someone can figure out how to yield from a sub-function, we can avoid
328341
# using this ugly list
@@ -357,9 +370,14 @@ def warn_unknown_block_cb(
357370
line: int | str,
358371
col: int,
359372
node: nodes.literal_block,
373+
unknown_directive: bool,
360374
) -> None:
361-
if warn_unknown_block:
375+
if warn_unknown_block and unknown_directive:
362376
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+
)
363381

364382
# Process the document
365383
try:
@@ -371,20 +389,29 @@ def warn_unknown_block_cb(
371389
yield from results
372390

373391

374-
def find_code_blocks(
392+
def find_code_blocks( # pylint: disable=too-many-arguments
375393
content: str,
376394
*,
377395
path: str | os.PathLike[str] | None = None,
378396
root_prefix: str | os.PathLike[str] | None = None,
379397
extra_directives: Mapping[str, t.Type[Directive]] | None = None,
380398
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,
381402
) -> t.Generator[CodeBlockInfo]:
382403
"""
383404
Given a RST document, finds all code blocks.
384405
385406
To add support for own types of code blocks, you can pass these
386407
as ``extra_directives``. Use ``mark_antsibull_code_block()`` to
387408
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.
388415
"""
389416
directives = get_code_block_directives(extra_directives=extra_directives)
390417

@@ -400,6 +427,7 @@ def find_code_blocks(
400427
document=doc,
401428
content=content,
402429
warn_unknown_block=warn_unknown_block,
430+
warn_unknown_block_w_unknown_info=warn_unknown_block_w_unknown_info,
403431
)
404432

405433

tests/test_rst_code_finder.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,37 @@ def test_find_code_blocks_ext():
284284
foobar
285285
286286
Some invalid `markup <foo>
287+
288+
.. highlight:: python
289+
290+
::
291+
292+
def foo():
293+
pass
294+
295+
.. literalinclude:: nox.py
296+
297+
| a
298+
| b
299+
c
287300
"""
288301
found_warnings = []
302+
found_warnings_2 = []
289303

290304
def add_warning(line: int | str, col: int, message: str) -> None:
291305
found_warnings.append((line, col, message))
292306

307+
def add_warning_2(
308+
line: int | str, col: int, message: str, unknown_origin: bool
309+
) -> None:
310+
found_warnings_2.append((line, col, message, unknown_origin))
311+
293312
found_code_block_infos = list(
294313
find_code_blocks(
295314
source,
296315
extra_directives={"foo": CodeBlockTest},
297316
warn_unknown_block=add_warning,
317+
warn_unknown_block_w_unknown_info=add_warning_2,
298318
)
299319
)
300320
assert found_code_block_infos == [
@@ -317,9 +337,11 @@ def add_warning(line: int | str, col: int, message: str) -> None:
317337
attributes={},
318338
),
319339
]
320-
assert found_warnings == [
321-
(6, 0, "bar"),
322-
(12, 0, "bazbam"),
340+
assert found_warnings == []
341+
assert found_warnings_2 == [
342+
(6, 0, "bar", False),
343+
(12, 0, "bazbam", False),
344+
(24, 0, "def foo():\n pass", False),
323345
]
324346

325347

0 commit comments

Comments
 (0)