Skip to content

Commit

Permalink
🐛 FIX: parse_directive_text when body followed by options (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Jun 7, 2022
1 parent cc44a35 commit c17d855
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 34 deletions.
9 changes: 2 additions & 7 deletions myst_parser/parsers/directives.py
Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions test.rst
@@ -0,0 +1 @@
.. note:: hallo
141 changes: 141 additions & 0 deletions 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
.
34 changes: 24 additions & 10 deletions 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(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit c17d855

Please sign in to comment.