From 446febadcaec172144e31927e935ee34bfdca4e2 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Sun, 28 Apr 2024 21:38:48 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX=20empty=20value=20for=20fina?= =?UTF-8?q?l=20directive=20option=20(#924)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myst_parser/parsers/directives.py | 3 +- myst_parser/parsers/options.py | 4 +- .../fixtures/directive_parsing.txt | 44 +++++++++++++++++++ .../fixtures/option_parsing.yaml | 19 ++++++++ tests/test_renderers/test_parse_directives.py | 6 ++- 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/myst_parser/parsers/directives.py b/myst_parser/parsers/directives.py index 5a2a7f46..d6a000c1 100644 --- a/myst_parser/parsers/directives.py +++ b/myst_parser/parsers/directives.py @@ -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 diff --git a/myst_parser/parsers/options.py b/myst_parser/parsers/options.py index cf4d3532..366d4559 100644 --- a/myst_parser/parsers/options.py +++ b/myst_parser/parsers/options.py @@ -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. @@ -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 diff --git a/tests/test_renderers/fixtures/directive_parsing.txt b/tests/test_renderers/fixtures/directive_parsing.txt index 663d09e0..3b8cc86a 100644 --- a/tests/test_renderers/fixtures/directive_parsing.txt +++ b/tests/test_renderers/fixtures/directive_parsing.txt @@ -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: [] +. diff --git a/tests/test_renderers/fixtures/option_parsing.yaml b/tests/test_renderers/fixtures/option_parsing.yaml index 14fe0a5c..772aa7f9 100644 --- a/tests/test_renderers/fixtures/option_parsing.yaml +++ b/tests/test_renderers/fixtures/option_parsing.yaml @@ -182,3 +182,22 @@ folded values: ], "comments": false } + +empty_final_value: + content: |- + key1: val1 + key2: + expected: |- + { + "dict": [ + [ + "key1", + "val1" + ], + [ + "key2", + "" + ] + ], + "comments": false + } diff --git a/tests/test_renderers/test_parse_directives.py b/tests/test_renderers/test_parse_directives.py index e6b2c2b4..3813cc97 100644 --- a/tests/test_renderers/test_parse_directives.py +++ b/tests/test_renderers/test_parse_directives.py @@ -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") @@ -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: