diff --git a/sphinx/parsers.py b/sphinx/parsers.py index 8b83a388f31..f6a6517c069 100644 --- a/sphinx/parsers.py +++ b/sphinx/parsers.py @@ -24,18 +24,20 @@ def __init__(self, app: Sphinx, state_classes: list[State], initial_state: str, self.app = app super().__init__(state_classes=state_classes, initial_state=initial_state, debug=debug) - def insert_input(self, include_lines : StringList, path: str): - # first we need to combine the lines back into text so we can send it with the source-read - # event - text = "\n".join(include_lines) + def insert_input(self, include_lines : list[str], path: str): + # First we need to combine the lines back into text so we can send it with the source-read + # event. In newer releases of docutils there are two lines at the end, that act as markers. + # We must preserve them and leave them out of the source-read event: + text = "\n".join(include_lines[:-2]) # turn the path back to doc reference for source-read event doc = self.app.env.path2doc(path) # emit "source-read" event arg = [text] self.app.env.events.emit("source-read", doc, arg) text = arg[0] - # split back into lines again: - include_lines = text.splitlines() + # split back into lines and reattach the two marker lines + processed_lines = text.splitlines() + processed_lines += include_lines[-2:] # call the parent implementation return super().insert_input(include_lines, path) diff --git a/tests/roots/test-directive-include/baz/baz.rst b/tests/roots/test-directive-include/baz/baz.rst new file mode 100644 index 00000000000..84c3f2c2cc8 --- /dev/null +++ b/tests/roots/test-directive-include/baz/baz.rst @@ -0,0 +1,4 @@ +Baz +=== + +Baz was here. \ No newline at end of file diff --git a/tests/roots/test-directive-include/conf.py b/tests/roots/test-directive-include/conf.py new file mode 100644 index 00000000000..a4768582f36 --- /dev/null +++ b/tests/roots/test-directive-include/conf.py @@ -0,0 +1,2 @@ +project = 'test-directive-include' +exclude_patterns = ['_build'] diff --git a/tests/roots/test-directive-include/text.txt b/tests/roots/test-directive-include/text.txt new file mode 100644 index 00000000000..b7ea15d7b02 --- /dev/null +++ b/tests/roots/test-directive-include/text.txt @@ -0,0 +1 @@ +This is plain text. diff --git a/tests/test_directive_other.py b/tests/test_directive_other.py index 8d748c05ab2..466fd2f7980 100644 --- a/tests/test_directive_other.py +++ b/tests/test_directive_other.py @@ -150,14 +150,19 @@ def test_toctree_twice(app): includefiles=['foo', 'foo']) -@pytest.mark.sphinx(testroot='toctree-glob') +@pytest.mark.sphinx(testroot='directive-include') def test_include_source_read_event(app): sources_reported = {} def source_read_handler(app, doc, source): sources_reported[doc] = source[0] app.connect("source-read", source_read_handler) - text = ".. include:: baz.rst\n" + text = (".. include:: baz/baz.rst\n" + " :start-line: 2\n\n" + ".. include:: text.txt\n" + " :literal: \n") app.env.find_files(app.config, app.builder) doctree = restructuredtext.parse(app, text, 'index') assert("index" in sources_reported) - assert("baz" in sources_reported) + assert("text.txt" not in sources_reported) # text was included as literal, no rst parsing + assert("baz/baz" in sources_reported) + assert("\nBaz was here." == sources_reported["baz/baz"])