Skip to content

Commit 56d019a

Browse files
committed
Fix directive arguments
1 parent eb29538 commit 56d019a

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Directive arguments were not being properly migrated in the AST.
13+
1014
## [v0.5.1] - 2020-07-15
1115

1216
### Added

snooty/n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class Directive(Parent[Node]):
257257
type = "directive"
258258
domain: str
259259
name: str
260-
argument: List["Text"]
260+
argument: MutableSequence["Text"]
261261
options: Dict[str, str]
262262

263263
def verify(self) -> None:

snooty/parser.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,26 @@ def handle_directive(
385385

386386
doc = n.Directive((line,), [], domain, name, [], options)
387387

388-
if (
389-
node.children
390-
and node.children[0].__class__.__name__ == "directive_argument"
391-
):
392-
visitor = self.__make_child_visitor()
393-
node.children[0].walkabout(visitor)
394-
top_of_visitor_state = visitor.state[-1]
395-
assert isinstance(top_of_visitor_state, n.Parent)
396-
argument = top_of_visitor_state.children
397-
doc.argument = argument # type: ignore
398-
node.children = node.children[1:]
399-
else:
400-
argument = []
401-
doc.argument = argument
388+
# Find and move the argument from the children to the "argument" field.
389+
argument: MutableSequence[Any] = []
390+
if node.children:
391+
index_of_argument = next(
392+
(
393+
idx
394+
for idx, value in enumerate(node.children)
395+
if isinstance(value, rstparser.directive_argument)
396+
),
397+
None,
398+
)
399+
if index_of_argument is not None:
400+
visitor = self.__make_child_visitor()
401+
node.children[index_of_argument].walkabout(visitor)
402+
top_of_visitor_state = visitor.state[-1]
403+
assert isinstance(top_of_visitor_state, n.Parent)
404+
argument = top_of_visitor_state.children
405+
del node.children[index_of_argument]
406+
407+
doc.argument = argument
402408

403409
argument_text = None
404410
try:

snooty/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ def test_deprecated() -> None:
15951595
assert len(diagnostics) == 1
15961596
check_ast_testing_string(
15971597
page.ast,
1598-
"""<root><directive name="raw"><directive_argument><text>html</text></directive_argument><FixedTextElement /></directive></root>""",
1598+
"""<root><directive name="raw"><text>html</text><FixedTextElement /></directive></root>""",
15991599
)
16001600

16011601

stubs/docutils/nodes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import docutils.nodes
22
import docutils.utils
3-
from typing import Any, Optional, Sequence, Iterable, Union
3+
from typing import Any, Optional, List, Iterable, Union
44
from typing_extensions import Protocol
55

66

@@ -17,7 +17,7 @@ class Node:
1717
line: Optional[int]
1818
parent: Optional[Node]
1919
document: Optional[Node]
20-
children: Sequence[Node]
20+
children: List[Node]
2121

2222
def walkabout(self, visitor: NodeVisitor) -> None: ...
2323
def astext(self) -> str: ...

0 commit comments

Comments
 (0)