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: allow indented option block #925

Merged
merged 1 commit into from
Apr 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions myst_parser/parsers/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,34 +175,34 @@ def _parse_directive_options(

:returns: (content, options, validation_errors)
"""
yaml_block: None | str = None
options_block: None | str = None
if content.startswith("---"):
line = None if line is None else line + 1
content = "\n".join(content.splitlines()[1:])
match = re.search(r"^-{3,}", content, re.MULTILINE)
if match:
yaml_block = content[: match.start()]
options_block = content[: match.start()]
content = content[match.end() + 1 :] # TODO advance line number
else:
yaml_block = content
options_block = content
content = ""
yaml_block = dedent(yaml_block)
elif content.startswith(":"):
options_block = dedent(options_block)
elif content.lstrip().startswith(":"):
content_lines = content.splitlines()
yaml_lines = []
while content_lines:
if not content_lines[0].startswith(":"):
if not content_lines[0].lstrip().startswith(":"):
break
yaml_lines.append(content_lines.pop(0)[1:])
yaml_block = "\n".join(yaml_lines)
yaml_lines.append(content_lines.pop(0).lstrip()[1:])
options_block = "\n".join(yaml_lines)
content = "\n".join(content_lines)

has_options_block = yaml_block is not None
has_options_block = options_block is not None

if as_yaml:
yaml_errors: list[ParseWarnings] = []
try:
yaml_options = yaml.safe_load(yaml_block or "") or {}
yaml_options = yaml.safe_load(options_block or "") or {}
except (yaml.parser.ParserError, yaml.scanner.ScannerError):
yaml_options = {}
yaml_errors.append(
Expand All @@ -226,9 +226,9 @@ def _parse_directive_options(
validation_errors: list[ParseWarnings] = []

options: dict[str, str] = {}
if yaml_block is not None:
if options_block is not None:
try:
_options, state = options_to_items(yaml_block)
_options, state = options_to_items(options_block)
options = dict(_options)
except TokenizeError as err:
return _DirectiveOptions(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_renderers/fixtures/directive_parsing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ error: missing argument
error: 1 argument(s) required, 0 supplied
.

indented_options
.
```{note}
:class: name

body
```
.
arguments: []
body:
- body
content_offset: 2
options:
class:
- name
warnings: []
.

option_flags_std
.
```{code-block}
Expand Down