| 376 | 376 | rel_filename, filename = self.env.relfn2path(self.arguments[0]) |
| 377 | 377 | self.arguments[0] = filename |
| 378 | 378 | self.env.note_included(filename) |
| 379 | return super().run() | |
| 379 | ||
| 380 | # Read the included file content and apply source-read event processing | |
| 381 | try: | |
| 382 | with open(filename, 'r', encoding=self.env.config.source_encoding) as f: | |
| 383 | content = f.read() | |
| 384 | ||
| 385 | # Get the included file docname | |
| 386 | included_docname = self.env.path2doc(filename) | |
| 387 | ||
| 388 | # Emit source-read event for the included file | |
| 389 | arg = [content] | |
| 390 | self.env.events.emit('source-read', included_docname, arg) | |
| 391 | processed_content = arg[0] | |
| 392 | ||
| 393 | # Create a temporary file with the processed content | |
| 394 | import tempfile | |
| 395 | import os | |
| 396 | ||
| 397 | # Preserve file extension for proper processing | |
| 398 | file_ext = os.path.splitext(filename)[1] | |
| 399 | with tempfile.NamedTemporaryFile(mode='w', encoding=self.env.config.source_encoding, | |
| 400 | delete=False, suffix=file_ext) as temp_file: | |
| 401 | temp_file.write(processed_content) | |
| 402 | temp_filename = temp_file.name | |
| 403 | ||
| 404 | # Use the temporary file for the include processing | |
| 405 | original_arg = self.arguments[0] | |
| 406 | self.arguments[0] = temp_filename | |
| 407 | ||
| 408 | try: | |
| 409 | result = super().run() | |
| 410 | finally: | |
| 411 | # Restore original argument and clean up | |
| 412 | self.arguments[0] = original_arg | |
| 413 | try: | |
| 414 | os.unlink(temp_filename) | |
| 415 | except OSError: | |
| 416 | pass # Ignore cleanup errors | |
| 417 | ||
| 418 | except (OSError, UnicodeDecodeError) as exc: | |
| 419 | from docutils.utils import SystemMessage | |
| 420 | raise SystemMessage( | |
| 421 | f'Problems with "{self.arguments[0]}" directive path:\n{exc}', | |
| 422 | level=2 | |
| 423 | ) | |
| 424 | ||
| 425 | return result | |
| 380 | 426 | |
| 381 | 427 | |
| 382 | 428 | def setup(app: Sphinx) -> dict[str, Any]: |
| Test Name | Status |
|---|---|
tests/test_directive_other.py::test_include_source_read_event | Fail |
tests/test_directive_other.py::test_include_source_read_event_nested_includes | Fail |
tests/test_directive_other.py::test_toctree | Pass |
tests/test_directive_other.py::test_relative_toctree | Pass |
tests/test_directive_other.py::test_toctree_urls_and_titles | Pass |
tests/test_directive_other.py::test_toctree_glob | Pass |
tests/test_directive_other.py::test_toctree_glob_and_url | Pass |
tests/test_directive_other.py::test_reversed_toctree | Pass |
tests/test_directive_other.py::test_toctree_twice | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.