diff --git a/sybil/parsers/myst/lexers.py b/sybil/parsers/myst/lexers.py index 5d41e83..7224c27 100644 --- a/sybil/parsers/myst/lexers.py +++ b/sybil/parsers/myst/lexers.py @@ -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[ \t]*)```\{{(?P{directive})}} ?(?P{arguments})$\n" + r"^(?P[ \t]*)```+\{{(?P{directive})}} ?(?P{arguments})$\n" r'(?P(?:\1[ \t]*:[\w-]*:[^\n]*\n)+)?' r"(\1---\n(?P(?:.+\n)*)\1---\n)?" ) +CODEBLOCK_END_TEMPLATE = r"(?<=\n){prefix}```+(:?\n|\Z)" def parse_yaml_options(lexed: Region) -> None: diff --git a/tests/samples/myst-directive-nested.md b/tests/samples/myst-directive-nested.md new file mode 100644 index 0000000..d2b0a73 --- /dev/null +++ b/tests/samples/myst-directive-nested.md @@ -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... + ``` +```` diff --git a/tests/test_myst_lexers.py b/tests/test_myst_lexers.py index 6cf4ab4..a67d4b8 100644 --- a/tests/test_myst_lexers.py +++ b/tests/test_myst_lexers.py @@ -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", + }), + ])