diff --git a/docs/syntax/organising_content.md b/docs/syntax/organising_content.md index 2ae3027d..7d99ef21 100644 --- a/docs/syntax/organising_content.md +++ b/docs/syntax/organising_content.md @@ -138,6 +138,7 @@ to make it relative to the location that it is being inserted: Additional options: :encoding: The text encoding of the external file +:parser: Parse the included file with this parser (e.g. `rst`), instead of as MyST Markdown ::::{admonition} Including to/from reStructuredText files :class: tip dropdown diff --git a/myst_parser/mocking.py b/myst_parser/mocking.py index 97e6dcaf..ee11d12d 100644 --- a/myst_parser/mocking.py +++ b/myst_parser/mocking.py @@ -10,13 +10,13 @@ from pathlib import Path from typing import TYPE_CHECKING, Any -from docutils import nodes +from docutils import frontend, nodes from docutils.parsers.rst import Directive, DirectiveError from docutils.parsers.rst import Parser as RSTParser from docutils.parsers.rst.directives.misc import Include from docutils.parsers.rst.states import Body, Inliner, RSTStateMachine from docutils.statemachine import StringList -from docutils.utils import unescape +from docutils.utils import new_document, unescape from markdown_it.rules_inline import StateInline from .parsers.directives import MarkupError, is_option_line, parse_directive_text @@ -641,6 +641,8 @@ def run(self) -> list[nodes.Element]: state_machine=state_machine, ) return codeblock.run() + if "parser" in self.options: + return self._custom_parse(file_content, path) # Here we perform a nested render, but temporarily setup the document/reporter # with the correct document path and lineno for the included file. @@ -680,6 +682,31 @@ def run(self) -> list[nodes.Element]: del self.renderer.reporter.get_source_and_line return [] + def _custom_parse(self, text: str, path: Path) -> list[nodes.Element]: + """Parse the included text with the parser given by the ``parser`` option. + + As for the docutils ``include`` directive, the text is parsed into a new + (dummy) document, the parser's default transforms are applied, + and the resulting child nodes are returned. + """ + parser = self.options["parser"]() + settings = frontend.get_default_settings(parser) + # update with the current document settings + for key, value in self.document.settings.__dict__.items(): + setattr(settings, key, value) + document = new_document(str(path), settings) + document.include_log = getattr(self.document, "include_log", []) + document.ids = self.document.ids + document.nameids = self.document.nameids + document.nametypes = self.document.nametypes + parser.parse(text, document) + self.document.parse_messages.extend(document.parse_messages) + # clean up doctree and complete parsing + document.transformer.populate_from_components((parser,)) + document.transformer.apply_transforms() + self.document.transform_messages.extend(document.transform_messages) + return document.children + def add_name(self, node: nodes.Element): """Append self.options['name'] to node['names'] if it exists. diff --git a/tests/test_renderers/fixtures/mock_include.md b/tests/test_renderers/fixtures/mock_include.md index 18d0583d..e82ff1cc 100644 --- a/tests/test_renderers/fixtures/mock_include.md +++ b/tests/test_renderers/fixtures/mock_include.md @@ -64,3 +64,18 @@ Include code: c . + +Include with parser: +. +```{include} other.rst +:parser: rst +``` +. + + + + a + + + b +. diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index 96ede2ba..54aa073e 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -16,6 +16,7 @@ def test_render(file_params, tmp_path, monkeypatch, normalize_doctree_xml): tmp_path.joinpath("other.md").write_text("a\nb\nc") tmp_path.joinpath("fmatter.md").write_text("---\na: 1\n---\nb") + tmp_path.joinpath("other.rst").write_text("*a*\n\n.. note:: b") doctree = publish_doctree( file_params.content,