|
9 | 9 | from docutils.parsers.rst import Directive |
10 | 10 | from docutils.parsers.rst import Parser as RstParser |
11 | 11 | from docutils.statemachine import StringList |
12 | | -from docutils.utils import new_document |
13 | 12 | from jinja2 import Environment, PackageLoader |
14 | 13 | from sphinx import addnodes |
15 | 14 | from sphinx import version_info as sphinx_version_info |
|
52 | 51 | logger = logging.getLogger(__name__) |
53 | 52 |
|
54 | 53 |
|
| 54 | +def new_document_from_parent( |
| 55 | + source_path: str, parent_doc: nodes.document, line: int | None = None |
| 56 | +) -> nodes.document: |
| 57 | + """Create a new document that inherits the parent's settings and reporter.""" |
| 58 | + settings = parent_doc.settings |
| 59 | + reporter = parent_doc.reporter |
| 60 | + doc = nodes.document(settings, reporter, source=source_path) |
| 61 | + doc.note_source(source_path, -1) |
| 62 | + # Store line number for sphinx_js_type_role to use |
| 63 | + doc.sphinx_js_source_line = line # type: ignore[attr-defined] |
| 64 | + return doc |
| 65 | + |
| 66 | + |
55 | 67 | def sort_attributes_first_then_by_path(obj: TopLevel) -> Any: |
56 | 68 | """Return a sort key for IR objects.""" |
57 | 69 | match obj: |
@@ -325,13 +337,12 @@ def rst_nodes(self) -> list[Node]: |
325 | 337 | ) |
326 | 338 |
|
327 | 339 | # Parse the RST into docutils nodes with a fresh doc, and return |
328 | | - # them. |
329 | | - # |
330 | | - # Not sure if passing the settings from the "real" doc is the right |
331 | | - # thing to do here: |
332 | | - doc = new_document( |
333 | | - f"{obj.filename}:{obj.path}({obj.line})", |
334 | | - settings=self._directive.state.document.settings, |
| 340 | + # them. Use the directive's source location for error messages. |
| 341 | + source, line = self._directive.state_machine.get_source_and_line( |
| 342 | + self._directive.lineno |
| 343 | + ) |
| 344 | + doc = new_document_from_parent( |
| 345 | + source or "", self._directive.state.document, line |
335 | 346 | ) |
336 | 347 | RstParser().parse(rst, doc) |
337 | 348 | return doc.children |
|
0 commit comments