Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/syntax/organising_content.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 29 additions & 2 deletions myst_parser/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down
15 changes: 15 additions & 0 deletions tests/test_renderers/fixtures/mock_include.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ Include code:

c
.

Include with parser:
.
```{include} other.rst
:parser: rst
```
.
<document source="tmpdir/test.md">
<paragraph>
<emphasis>
a
<note>
<paragraph>
b
.
1 change: 1 addition & 0 deletions tests/test_renderers/test_include_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down