Skip to content

Commit 961a638

Browse files
✨ NEW: Emits sphinx include-read event (#887)
Adds emission of Sphinx `include-read` events while processing include directives See: https://www.sphinx-doc.org/en/master/extdev/appapi.html#event-include-read Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
1 parent da1a85a commit 961a638

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

myst_parser/mocking.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ def run(self) -> list[nodes.Element]:
381381
4, f'Directive "{self.name}": error reading file: {path}\n{error}.'
382382
) from error
383383

384+
if self.renderer.sphinx_env is not None:
385+
# Emit the "include-read" event
386+
# see: https://github.com/sphinx-doc/sphinx/commit/ff18318613db56d0000db47e5c8f0140556cef0c
387+
arg = [file_content]
388+
relative_path = Path(
389+
os.path.relpath(path, start=self.renderer.sphinx_env.srcdir)
390+
)
391+
parent_docname = Path(self.renderer.document["source"]).stem
392+
self.renderer.sphinx_env.app.events.emit(
393+
"include-read",
394+
relative_path,
395+
parent_docname,
396+
arg,
397+
)
398+
file_content = arg[0]
399+
384400
# get required section of text
385401
startline = self.options.get("start-line", None)
386402
endline = self.options.get("end-line", None)

tests/test_sphinx/sourcedirs/includes/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Main Title
22

3+
```{include} ../include_from_rst/include.md
4+
```
5+
36
```{include} include1.inc.md
47
```
58

tests/test_sphinx/test_sphinx_builds.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
(e.g. converting `<div class="section">` to `<section>`)
99
"""
1010

11+
from __future__ import annotations
12+
1113
import os
1214
import re
15+
from pathlib import Path
1316

1417
import pytest
18+
import sphinx
1519
from docutils import VersionInfo, __version_info__
1620

1721
SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs"))
@@ -583,3 +587,45 @@ def test_texinfo(app, status, warning):
583587
assert "build succeeded" in status.getvalue() # Build succeeded
584588
warnings = warning.getvalue().strip()
585589
assert warnings == ""
590+
591+
592+
@pytest.mark.skipif(
593+
sphinx.version_info < (7, 2, 5), reason="include-read event added in sphinx 7.2.5"
594+
)
595+
@pytest.mark.sphinx(
596+
buildername="html",
597+
srcdir=os.path.join(SOURCE_DIR, "includes"),
598+
freshenv=True,
599+
)
600+
def test_include_read_event(app, status, warning):
601+
"""Test that include-read event is emitted correctly."""
602+
603+
include_read_events = []
604+
605+
def handle_include_read(
606+
app, relative_path: Path, parent_docname: str, content: list[str]
607+
) -> None:
608+
include_read_events.append((relative_path, parent_docname, content))
609+
610+
app.connect("include-read", handle_include_read)
611+
app.build()
612+
assert "build succeeded" in status.getvalue() # Build succeeded
613+
warnings = warning.getvalue().strip()
614+
assert warnings == ""
615+
expected = [
616+
("../include_from_rst/include.md", "index"),
617+
("include1.inc.md", "index"),
618+
(os.path.join("subfolder", "include2.inc.md"), "include1.inc"),
619+
("include_code.py", "index"),
620+
("include_code.py", "index"),
621+
("include_literal.txt", "index"),
622+
("include_literal.txt", "index"),
623+
]
624+
expected_events = []
625+
for include_file_name, parent_docname in expected:
626+
with open(os.path.join(SOURCE_DIR, "includes", include_file_name)) as file:
627+
content = file.read()
628+
expected_events.append((Path(include_file_name), parent_docname, [content]))
629+
assert len(include_read_events) == len(expected_events), "Wrong number of events"
630+
for evt in expected_events:
631+
assert evt in include_read_events

tests/test_sphinx/test_sphinx_builds/test_includes.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ <h1>
88
99
</a>
1010
</h1>
11+
</section>
12+
<section id="markdown">
13+
<h1>
14+
Markdown
15+
<a class="headerlink" href="#markdown" title="Permalink to this heading">
16+
17+
</a>
18+
</h1>
19+
<p>
20+
<a class="reference external" href="http://example.com/">
21+
target
22+
</a>
23+
</p>
1124
<section id="a-sub-heading-in-include">
1225
<span id="inc-header">
1326
</span>

tests/test_sphinx/test_sphinx_builds/test_includes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<section ids="main-title" names="main\ title">
33
<title>
44
Main Title
5+
<section ids="markdown" names="markdown">
6+
<title>
7+
Markdown
8+
<paragraph>
9+
<reference refuri="http://example.com/">
10+
target
511
<target refid="inc-header">
612
<section ids="a-sub-heading-in-include inc-header" names="a\ sub-heading\ in\ include inc_header">
713
<title>

0 commit comments

Comments
 (0)