Skip to content

Commit 790a926

Browse files
authored
🐛 FIX: allow indented option block (#925)
1 parent 446feba commit 790a926

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

myst_parser/parsers/directives.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,34 +175,34 @@ def _parse_directive_options(
175175
176176
:returns: (content, options, validation_errors)
177177
"""
178-
yaml_block: None | str = None
178+
options_block: None | str = None
179179
if content.startswith("---"):
180180
line = None if line is None else line + 1
181181
content = "\n".join(content.splitlines()[1:])
182182
match = re.search(r"^-{3,}", content, re.MULTILINE)
183183
if match:
184-
yaml_block = content[: match.start()]
184+
options_block = content[: match.start()]
185185
content = content[match.end() + 1 :] # TODO advance line number
186186
else:
187-
yaml_block = content
187+
options_block = content
188188
content = ""
189-
yaml_block = dedent(yaml_block)
190-
elif content.startswith(":"):
189+
options_block = dedent(options_block)
190+
elif content.lstrip().startswith(":"):
191191
content_lines = content.splitlines()
192192
yaml_lines = []
193193
while content_lines:
194-
if not content_lines[0].startswith(":"):
194+
if not content_lines[0].lstrip().startswith(":"):
195195
break
196-
yaml_lines.append(content_lines.pop(0)[1:])
197-
yaml_block = "\n".join(yaml_lines)
196+
yaml_lines.append(content_lines.pop(0).lstrip()[1:])
197+
options_block = "\n".join(yaml_lines)
198198
content = "\n".join(content_lines)
199199

200-
has_options_block = yaml_block is not None
200+
has_options_block = options_block is not None
201201

202202
if as_yaml:
203203
yaml_errors: list[ParseWarnings] = []
204204
try:
205-
yaml_options = yaml.safe_load(yaml_block or "") or {}
205+
yaml_options = yaml.safe_load(options_block or "") or {}
206206
except (yaml.parser.ParserError, yaml.scanner.ScannerError):
207207
yaml_options = {}
208208
yaml_errors.append(
@@ -226,9 +226,9 @@ def _parse_directive_options(
226226
validation_errors: list[ParseWarnings] = []
227227

228228
options: dict[str, str] = {}
229-
if yaml_block is not None:
229+
if options_block is not None:
230230
try:
231-
_options, state = options_to_items(yaml_block)
231+
_options, state = options_to_items(options_block)
232232
options = dict(_options)
233233
except TokenizeError as err:
234234
return _DirectiveOptions(

tests/test_renderers/fixtures/directive_parsing.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,24 @@ error: missing argument
258258
error: 1 argument(s) required, 0 supplied
259259
.
260260

261+
indented_options
262+
.
263+
```{note}
264+
:class: name
265+
266+
body
267+
```
268+
.
269+
arguments: []
270+
body:
271+
- body
272+
content_offset: 2
273+
options:
274+
class:
275+
- name
276+
warnings: []
277+
.
278+
261279
option_flags_std
262280
.
263281
```{code-block}

0 commit comments

Comments
 (0)