From c17d855c48932dc37036740bfb1edf1b10ad4921 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Tue, 7 Jun 2022 14:00:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20`parse=5Fdirective=5Ftext?= =?UTF-8?q?`=20when=20body=20followed=20by=20options=20(#580)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myst_parser/parsers/directives.py | 9 +- test.rst | 1 + .../fixtures/directive_parsing.txt | 141 ++++++++++++++++++ tests/test_renderers/test_parse_directives.py | 34 +++-- .../test_parsing_Note___class__name_n_na_.yml | 7 - .../test_parsing_Note__a_.yml | 5 - .../test_parsing_Note_a__.yml | 5 - 7 files changed, 168 insertions(+), 34 deletions(-) create mode 100644 test.rst create mode 100644 tests/test_renderers/fixtures/directive_parsing.txt delete mode 100644 tests/test_renderers/test_parse_directives/test_parsing_Note___class__name_n_na_.yml delete mode 100644 tests/test_renderers/test_parse_directives/test_parsing_Note__a_.yml delete mode 100644 tests/test_renderers/test_parse_directives/test_parsing_Note_a__.yml diff --git a/myst_parser/parsers/directives.py b/myst_parser/parsers/directives.py index e275c843..56372545 100644 --- a/myst_parser/parsers/directives.py +++ b/myst_parser/parsers/directives.py @@ -78,13 +78,8 @@ def parse_directive_text( body_lines = content.splitlines() content_offset = 0 - if not ( - directive_class.required_arguments - or directive_class.optional_arguments - or options - ): - # If there are no possible arguments and no option block, - # then the body starts on the argument line + if not (directive_class.required_arguments or directive_class.optional_arguments): + # If there are no possible arguments, then the body starts on the argument line if first_line: body_lines.insert(0, first_line) arguments = [] diff --git a/test.rst b/test.rst new file mode 100644 index 00000000..25855295 --- /dev/null +++ b/test.rst @@ -0,0 +1 @@ +.. note:: hallo diff --git a/tests/test_renderers/fixtures/directive_parsing.txt b/tests/test_renderers/fixtures/directive_parsing.txt new file mode 100644 index 00000000..35878bc4 --- /dev/null +++ b/tests/test_renderers/fixtures/directive_parsing.txt @@ -0,0 +1,141 @@ +note: content in first line only +. +```{note} a +``` +. +arguments: [] +body: +- a +content_offset: 0 +options: {} +. + +note: content in body only +. +```{note} +a +``` +. +arguments: [] +body: +- a +content_offset: 0 +options: {} +. + +note: content after option +. +```{note} +:class: name +a +``` +. +arguments: [] +body: +- a +content_offset: 1 +options: + class: + - name +. + +note: content after option with new line +. +```{note} +:class: name + +a +``` +. +arguments: [] +body: +- a +content_offset: 2 +options: + class: + - name +. + +note: content after yaml option +. +```{note} +--- +class: name +--- +a +``` +. +arguments: [] +body: +- a +content_offset: 3 +options: + class: + - name +. + +note: content in first line and body +. +```{note} first line +:class: tip + +body line +``` +. +arguments: [] +body: +- first line +- '' +- body line +content_offset: 1 +options: + class: + - tip +. + +admonition: no options, no new line +. +```{admonition} first line +body line +``` +. +arguments: +- first line +body: +- body line +content_offset: 0 +options: {} +. + +admonition: no options, new line +. +```{admonition} first line + +body line +``` +. +arguments: +- first line +body: +- body line +content_offset: 1 +options: {} +. + +admonition: with options +. +```{admonition} first line +:class: tip + +body line +``` +. +arguments: +- first line +body: +- body line +content_offset: 2 +options: + class: + - tip +. diff --git a/tests/test_renderers/test_parse_directives.py b/tests/test_renderers/test_parse_directives.py index 068a3d77..ae6792ba 100644 --- a/tests/test_renderers/test_parse_directives.py +++ b/tests/test_renderers/test_parse_directives.py @@ -1,27 +1,41 @@ -# TODO add more tests +from pathlib import Path + import pytest -from docutils.parsers.rst.directives.admonitions import Note +import yaml +from docutils.parsers.rst.directives.admonitions import Admonition, Note from docutils.parsers.rst.directives.body import Rubric +from markdown_it import MarkdownIt from myst_parser.parsers.directives import DirectiveParsingError, parse_directive_text +FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures") -@pytest.mark.parametrize( - "klass,arguments,content", - [(Note, "", "a"), (Note, "a", ""), (Note, "", ":class: name\n\na")], -) -def test_parsing(klass, arguments, content, data_regression): + +@pytest.mark.param_file(FIXTURE_PATH / "directive_parsing.txt") +def test_parsing(file_params): + """Test parsing of directive text.""" + tokens = MarkdownIt("commonmark").parse(file_params.content) + assert len(tokens) == 1 and tokens[0].type == "fence" + name, *first_line = tokens[0].info.split(maxsplit=1) + if name == "{note}": + klass = Note + elif name == "{admonition}": + klass = Admonition + else: + raise AssertionError(f"Unknown directive: {name}") arguments, options, body_lines, content_offset = parse_directive_text( - klass, arguments, content + klass, first_line[0] if first_line else "", tokens[0].content ) - data_regression.check( + outcome = yaml.safe_dump( { "arguments": arguments, "options": options, "body": body_lines, "content_offset": content_offset, - } + }, + sort_keys=True, ) + file_params.assert_expected(outcome, rstrip_lines=True) @pytest.mark.parametrize( diff --git a/tests/test_renderers/test_parse_directives/test_parsing_Note___class__name_n_na_.yml b/tests/test_renderers/test_parse_directives/test_parsing_Note___class__name_n_na_.yml deleted file mode 100644 index 8b9bbdf3..00000000 --- a/tests/test_renderers/test_parse_directives/test_parsing_Note___class__name_n_na_.yml +++ /dev/null @@ -1,7 +0,0 @@ -arguments: [] -body: -- a -content_offset: 2 -options: - class: - - name diff --git a/tests/test_renderers/test_parse_directives/test_parsing_Note__a_.yml b/tests/test_renderers/test_parse_directives/test_parsing_Note__a_.yml deleted file mode 100644 index fd1e5585..00000000 --- a/tests/test_renderers/test_parse_directives/test_parsing_Note__a_.yml +++ /dev/null @@ -1,5 +0,0 @@ -arguments: [] -body: -- a -content_offset: 0 -options: {} diff --git a/tests/test_renderers/test_parse_directives/test_parsing_Note_a__.yml b/tests/test_renderers/test_parse_directives/test_parsing_Note_a__.yml deleted file mode 100644 index fd1e5585..00000000 --- a/tests/test_renderers/test_parse_directives/test_parsing_Note_a__.yml +++ /dev/null @@ -1,5 +0,0 @@ -arguments: [] -body: -- a -content_offset: 0 -options: {}