Skip to content

Commit

Permalink
Add support for MyST nested directives
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Feb 9, 2024
1 parent 0fc6f9c commit 301d445
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sybil/parsers/myst/lexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from sybil import Document, Region
from sybil.parsers.abstract.lexers import BlockLexer
from sybil.parsers.markdown.lexers import CODEBLOCK_END_TEMPLATE
from sybil.parsers.rest.lexers import parse_options_and_source

DIRECTIVE_START_TEMPLATE = (
r"^(?P<prefix>[ \t]*)```\{{(?P<directive>{directive})}} ?(?P<arguments>{arguments})$\n"
r"^(?P<prefix>[ \t]*)```+\{{(?P<directive>{directive})}} ?(?P<arguments>{arguments})$\n"
r'(?P<options>(?:\1[ \t]*:[\w-]*:[^\n]*\n)+)?'
r"(\1---\n(?P<yaml_options>(?:.+\n)*)\1---\n)?"
)
CODEBLOCK_END_TEMPLATE = r"(?<=\n){prefix}```+(:?\n|\Z)"


def parse_yaml_options(lexed: Region) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tests/samples/myst-directive-nested.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
````{note}
The warning block will be properly-parsed
```{warning}
Here's my warning
```
But the next block will be parsed as raw text
```{warning}
Here's my raw text warning that isn't parsed...
```
````
35 changes: 35 additions & 0 deletions tests/test_myst_lexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,38 @@ def test_directive_no_trailing_newline():
'source': 'flask\npyramid\ncustom\n',
}),
])


def test_directive_nested():
lexer = DirectiveLexer(directive='.+')
text = Path(sample_path('myst-directive-nested.md')).read_text().rstrip('\n')
compare(lex_text(text, lexer), expected=[
Region(0, 223, lexemes={
'directive': 'note',
'arguments': '',
'options': {},
'source': ('The warning block will be properly-parsed\n'
'\n'
' ```{warning}\n'
" Here's my warning\n"
' ```\n'
'\n'
'But the next block will be parsed as raw text\n'
'\n'
' ```{warning}\n'
" Here's my raw text warning that isn't parsed...\n"
' ```\n'),
}),
Region(54, 91, lexemes={
'directive': 'warning',
'arguments': '',
'options': {},
'source': "Here's my warning\n",
}),
Region(146, 215, lexemes={
'directive': 'warning',
'arguments': '',
'options': {},
'source': "Here's my raw text warning that isn't parsed...\n",
}),
])

0 comments on commit 301d445

Please sign in to comment.