Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 FIX: parse_directive_text when body followed by options #580

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions myst_parser/parsers/directives.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. note:: hallo
141 changes: 141 additions & 0 deletions tests/test_renderers/fixtures/directive_parsing.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.