Skip to content

Commit

Permalink
🐛 FIX empty value for final directive option (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Apr 28, 2024
1 parent c9579c4 commit 446feba
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
3 changes: 1 addition & 2 deletions myst_parser/parsers/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@

from myst_parser.warnings_ import MystWarnings

from .options import TokenizeError
from .options import to_items as options_to_items
from .options import TokenizeError, options_to_items


@dataclass
Expand Down
4 changes: 3 additions & 1 deletion myst_parser/parsers/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class State:
has_comments: bool = False


def to_items(
def options_to_items(
text: str, line_offset: int = 0, column_offset: int = 0
) -> tuple[list[tuple[str, str]], State]:
"""Parse a directive option block into (key, value) tuples.
Expand Down Expand Up @@ -211,6 +211,8 @@ def _to_tokens(
raise TokenizeError("expected key before value", token.start)
yield key_token, token
key_token = None
if key_token is not None:
yield key_token, None
except TokenizeError as exc:
if line_offset or column_offset:
raise exc.clone(line_offset, column_offset) from exc
Expand Down
44 changes: 44 additions & 0 deletions tests/test_renderers/fixtures/directive_parsing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,47 @@ error: missing argument
.
error: 1 argument(s) required, 0 supplied
.

option_flags_std
.
```{code-block}
:linenos:
:lineno-start: 2
:force:

body
```
.
arguments: []
body:
- body
content_offset: 4
options:
force: null
lineno-start: 2
linenos: null
warnings: []
.

option_flags_delimited
.
```{code-block}
---
linenos:
lineno-start: 2
force:
---

body
```
.
arguments: []
body:
- body
content_offset: 6
options:
force: null
lineno-start: 2
linenos: null
warnings: []
.
19 changes: 19 additions & 0 deletions tests/test_renderers/fixtures/option_parsing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,22 @@ folded values:
],
"comments": false
}
empty_final_value:
content: |-
key1: val1
key2:
expected: |-
{
"dict": [
[
"key1",
"val1"
],
[
"key2",
""
]
],
"comments": false
}
6 changes: 4 additions & 2 deletions tests/test_renderers/test_parse_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from docutils.parsers.rst.directives.admonitions import Admonition, Note
from docutils.parsers.rst.directives.body import Rubric
from markdown_it import MarkdownIt
from sphinx.directives.code import CodeBlock

from myst_parser.parsers.directives import MarkupError, parse_directive_text
from myst_parser.parsers.options import TokenizeError
from myst_parser.parsers.options import to_items as options_to_items
from myst_parser.parsers.options import TokenizeError, options_to_items

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

Expand Down Expand Up @@ -50,6 +50,8 @@ def test_parsing(file_params):
klass = Note
elif name == "{admonition}":
klass = Admonition
elif name == "{code-block}":
klass = CodeBlock
else:
raise AssertionError(f"Unknown directive: {name}")
try:
Expand Down

0 comments on commit 446feba

Please sign in to comment.