|
11 | 11 | from typing import TYPE_CHECKING, final
|
12 | 12 |
|
13 | 13 | from docutils import nodes
|
14 |
| -from docutils.utils import DependencyList |
15 | 14 |
|
16 | 15 | from sphinx._cli.util.colour import bold
|
17 | 16 | from sphinx.deprecation import _deprecation_warning
|
|
23 | 22 | from sphinx.environment.adapters.asset import ImageAdapter
|
24 | 23 | from sphinx.errors import SphinxError
|
25 | 24 | from sphinx.locale import __
|
26 |
| -from sphinx.util import ( |
27 |
| - get_filetype, |
28 |
| - logging, |
29 |
| - rst, |
30 |
| -) |
| 25 | +from sphinx.util import get_filetype, logging |
31 | 26 | from sphinx.util._importer import import_object
|
32 | 27 | from sphinx.util._pathlib import _StrPathProperty
|
33 | 28 | from sphinx.util.build_phase import BuildPhase
|
34 | 29 | from sphinx.util.display import progress_message, status_iterator
|
35 |
| -from sphinx.util.docutils import sphinx_domains |
| 30 | +from sphinx.util.docutils import _parse_str_to_doctree |
36 | 31 | from sphinx.util.i18n import CatalogRepository, docname_to_domain
|
37 | 32 | from sphinx.util.osutil import ensuredir, relative_uri, relpath
|
38 | 33 | from sphinx.util.parallel import (
|
@@ -644,26 +639,34 @@ def read_doc(self, docname: str, *, _cache: bool = True) -> None:
|
644 | 639 | if docutils_conf.is_file():
|
645 | 640 | env.note_dependency(docutils_conf)
|
646 | 641 |
|
647 |
| - filename = str(env.doc2path(docname)) |
648 |
| - filetype = get_filetype(self._app.config.source_suffix, filename) |
649 |
| - publisher = self._registry._get_publisher( |
650 |
| - filetype, config=self.config, env=self.env |
| 642 | + filename = env.doc2path(docname) |
| 643 | + |
| 644 | + # set up error_handler for the target document |
| 645 | + # xref RemovedInSphinx90Warning |
| 646 | + error_handler = _UnicodeDecodeErrorHandler(docname) |
| 647 | + codecs.register_error('sphinx', error_handler) # type: ignore[arg-type] |
| 648 | + |
| 649 | + # read the source file |
| 650 | + content = filename.read_text( |
| 651 | + encoding=env.settings['input_encoding'], errors='sphinx' |
651 | 652 | )
|
652 |
| - self.env.current_document._parser = publisher.parser |
653 |
| - # record_dependencies is mutable even though it is in settings, |
654 |
| - # explicitly re-initialise for each document |
655 |
| - publisher.settings.record_dependencies = DependencyList() |
656 |
| - with ( |
657 |
| - sphinx_domains(env), |
658 |
| - rst.default_role(docname, self.config.default_role), |
659 |
| - ): |
660 |
| - # set up error_handler for the target document |
661 |
| - error_handler = _UnicodeDecodeErrorHandler(docname) |
662 |
| - codecs.register_error('sphinx', error_handler) # type: ignore[arg-type] |
663 | 653 |
|
664 |
| - publisher.set_source(source_path=filename) |
665 |
| - publisher.publish() |
666 |
| - doctree = publisher.document |
| 654 | + # TODO: move the "source-read" event to here. |
| 655 | + |
| 656 | + filetype = get_filetype(self.config.source_suffix, filename) |
| 657 | + parser = self._registry.create_source_parser( |
| 658 | + filetype, config=self.config, env=env |
| 659 | + ) |
| 660 | + doctree = _parse_str_to_doctree( |
| 661 | + content, |
| 662 | + filename=filename, |
| 663 | + default_role=self.config.default_role, |
| 664 | + default_settings=env.settings, |
| 665 | + env=env, |
| 666 | + events=self.events, |
| 667 | + parser=parser, |
| 668 | + transforms=self._registry.get_transforms(), |
| 669 | + ) |
667 | 670 |
|
668 | 671 | # store time of reading, for outdated files detection
|
669 | 672 | env.all_docs[docname] = time.time_ns() // 1_000
|
@@ -901,20 +904,21 @@ def __init__(self, docname: str, /) -> None:
|
901 | 904 | self.docname = docname
|
902 | 905 |
|
903 | 906 | def __call__(self, error: UnicodeDecodeError) -> tuple[str, int]:
|
904 |
| - line_start = error.object.rfind(b'\n', 0, error.start) |
905 |
| - line_end = error.object.find(b'\n', error.start) |
| 907 | + obj = error.object |
| 908 | + line_start = obj.rfind(b'\n', 0, error.start) |
| 909 | + line_end = obj.find(b'\n', error.start) |
906 | 910 | if line_end == -1:
|
907 |
| - line_end = len(error.object) |
908 |
| - line_num = error.object.count(b'\n', 0, error.start) + 1 |
| 911 | + line_end = len(obj) |
| 912 | + line_num = obj.count(b'\n', 0, error.start) + 1 |
909 | 913 | logger.warning(
|
910 |
| - __('undecodable source characters, replacing with "?": %r'), |
911 |
| - ( |
912 |
| - error.object[line_start + 1 : error.start] |
913 |
| - + b'>>>' |
914 |
| - + error.object[error.start : error.end] |
915 |
| - + b'<<<' |
916 |
| - + error.object[error.end : line_end] |
| 914 | + __( |
| 915 | + "undecodable source characters, replacing with '?': '%s>>>%s<<<%s'. " |
| 916 | + 'This will become an error in Sphinx 9.0.' |
| 917 | + # xref RemovedInSphinx90Warning |
917 | 918 | ),
|
| 919 | + obj[line_start + 1 : error.start].decode(errors='backslashreplace'), |
| 920 | + obj[error.start : error.end].decode(errors='backslashreplace'), |
| 921 | + obj[error.end : line_end].decode(errors='backslashreplace'), |
918 | 922 | location=(self.docname, line_num),
|
919 | 923 | )
|
920 | 924 | return '?', error.end
|
0 commit comments