Skip to content

Commit 446feba

Browse files
authored
🐛 FIX empty value for final directive option (#924)
1 parent c9579c4 commit 446feba

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

myst_parser/parsers/directives.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949

5050
from myst_parser.warnings_ import MystWarnings
5151

52-
from .options import TokenizeError
53-
from .options import to_items as options_to_items
52+
from .options import TokenizeError, options_to_items
5453

5554

5655
@dataclass

myst_parser/parsers/options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class State:
168168
has_comments: bool = False
169169

170170

171-
def to_items(
171+
def options_to_items(
172172
text: str, line_offset: int = 0, column_offset: int = 0
173173
) -> tuple[list[tuple[str, str]], State]:
174174
"""Parse a directive option block into (key, value) tuples.
@@ -211,6 +211,8 @@ def _to_tokens(
211211
raise TokenizeError("expected key before value", token.start)
212212
yield key_token, token
213213
key_token = None
214+
if key_token is not None:
215+
yield key_token, None
214216
except TokenizeError as exc:
215217
if line_offset or column_offset:
216218
raise exc.clone(line_offset, column_offset) from exc

tests/test_renderers/fixtures/directive_parsing.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,47 @@ error: missing argument
257257
.
258258
error: 1 argument(s) required, 0 supplied
259259
.
260+
261+
option_flags_std
262+
.
263+
```{code-block}
264+
:linenos:
265+
:lineno-start: 2
266+
:force:
267+
268+
body
269+
```
270+
.
271+
arguments: []
272+
body:
273+
- body
274+
content_offset: 4
275+
options:
276+
force: null
277+
lineno-start: 2
278+
linenos: null
279+
warnings: []
280+
.
281+
282+
option_flags_delimited
283+
.
284+
```{code-block}
285+
---
286+
linenos:
287+
lineno-start: 2
288+
force:
289+
---
290+
291+
body
292+
```
293+
.
294+
arguments: []
295+
body:
296+
- body
297+
content_offset: 6
298+
options:
299+
force: null
300+
lineno-start: 2
301+
linenos: null
302+
warnings: []
303+
.

tests/test_renderers/fixtures/option_parsing.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,22 @@ folded values:
182182
],
183183
"comments": false
184184
}
185+
186+
empty_final_value:
187+
content: |-
188+
key1: val1
189+
key2:
190+
expected: |-
191+
{
192+
"dict": [
193+
[
194+
"key1",
195+
"val1"
196+
],
197+
[
198+
"key2",
199+
""
200+
]
201+
],
202+
"comments": false
203+
}

tests/test_renderers/test_parse_directives.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from docutils.parsers.rst.directives.admonitions import Admonition, Note
77
from docutils.parsers.rst.directives.body import Rubric
88
from markdown_it import MarkdownIt
9+
from sphinx.directives.code import CodeBlock
910

1011
from myst_parser.parsers.directives import MarkupError, parse_directive_text
11-
from myst_parser.parsers.options import TokenizeError
12-
from myst_parser.parsers.options import to_items as options_to_items
12+
from myst_parser.parsers.options import TokenizeError, options_to_items
1313

1414
FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")
1515

@@ -50,6 +50,8 @@ def test_parsing(file_params):
5050
klass = Note
5151
elif name == "{admonition}":
5252
klass = Admonition
53+
elif name == "{code-block}":
54+
klass = CodeBlock
5355
else:
5456
raise AssertionError(f"Unknown directive: {name}")
5557
try:

0 commit comments

Comments
 (0)