Skip to content

Commit

Permalink
Improve test and fix issues uncovered.
Browse files Browse the repository at this point in the history
  • Loading branch information
halldorfannar committed Jul 24, 2023
1 parent b61e2bf commit 552ecd5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
14 changes: 8 additions & 6 deletions sphinx/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions tests/roots/test-directive-include/baz/baz.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Baz
===

Baz was here.
2 changes: 2 additions & 0 deletions tests/roots/test-directive-include/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project = 'test-directive-include'
exclude_patterns = ['_build']
1 change: 1 addition & 0 deletions tests/roots/test-directive-include/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is plain text.
11 changes: 8 additions & 3 deletions tests/test_directive_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit 552ecd5

Please sign in to comment.