diff options
author | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2022-05-07 15:03:43 +0300 |
---|---|---|
committer | Adam Turner <9087854+aa-turner@users.noreply.github.com> | 2022-05-07 15:11:39 +0300 |
commit | 919eb1db912ae80c98b3edfbb303dcd04e2b3e9c (patch) | |
tree | d96c1a2e5877e087bb86a065d86cfa3b4c59cbef | |
parent | f346e0a11f358f0496524b1e3f5d0984722f7d85 (diff) |
Inline `io.read_doc` and explain why settings are copied
-rw-r--r-- | sphinx/builders/__init__.py | 18 | ||||
-rw-r--r-- | sphinx/io.py | 19 |
2 files changed, 16 insertions, 21 deletions
diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index f73c1a5fc..a67948acc 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -1,5 +1,6 @@ """Builder superclass for all builders.""" +import codecs import pickle import time from os import path @@ -14,10 +15,9 @@ from sphinx.environment import CONFIG_CHANGED_REASON, CONFIG_OK, BuildEnvironmen from sphinx.environment.adapters.asset import ImageAdapter from sphinx.errors import SphinxError from sphinx.events import EventManager -from sphinx.io import read_doc from sphinx.locale import __ -from sphinx.util import (get_filetype, import_object, logging, progress_message, rst, - status_iterator) +from sphinx.util import (UnicodeDecodeErrorHandler, get_filetype, import_object, logging, + progress_message, rst, status_iterator) from sphinx.util.build_phase import BuildPhase from sphinx.util.console import bold # type: ignore from sphinx.util.docutils import sphinx_domains @@ -469,7 +469,17 @@ class Builder: filetype = get_filetype(self.app.config.source_suffix, filename) publisher = self.app.registry.get_publisher(self.app, filetype) with sphinx_domains(self.env), rst.default_role(docname, self.config.default_role): - doctree = read_doc(publisher, docname, filename) + # set up error_handler for the target document + codecs.register_error('sphinx', UnicodeDecodeErrorHandler(docname)) # type: ignore + + publisher.set_source(source_path=filename) + publisher.publish() + doctree = publisher.document + + # The settings object is reused by the Publisher for each document. + # Becuase we modify the settings object in ``write_doctree``, we + # need to ensure that each doctree has an independent copy. + doctree.settings = doctree.settings.copy() # store time of reading, for outdated files detection # (Some filesystems have coarse timestamp resolution; diff --git a/sphinx/io.py b/sphinx/io.py index 4ffa8e54d..b4cec7d3e 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -1,5 +1,5 @@ """Input/Output files""" -import codecs + from typing import TYPE_CHECKING, Any, List, Type from docutils import nodes @@ -19,7 +19,7 @@ from sphinx.transforms import (AutoIndexUpgrader, DoctreeReadEvent, FigureAligne from sphinx.transforms.i18n import (Locale, PreserveTranslatableMessages, RemoveTranslatableInline) from sphinx.transforms.references import SphinxDomains -from sphinx.util import UnicodeDecodeErrorHandler, logging +from sphinx.util import logging from sphinx.util.docutils import LoggingReporter from sphinx.versioning import UIDTransform @@ -152,21 +152,6 @@ class SphinxFileInput(FileInput): super().__init__(*args, **kwargs) -def read_doc(publisher: Publisher, docname: str, filename: str) -> nodes.document: - """Parse a document and convert to doctree.""" - # set up error_handler for the target document - error_handler = UnicodeDecodeErrorHandler(docname) - codecs.register_error('sphinx', error_handler) # type: ignore - - publisher.set_source(source_path=filename) - publisher.publish() - - doctree = publisher.document - # settings get modified in ``write_doctree``; get a local copy - doctree.settings = doctree.settings.copy() - return doctree - - def create_publisher(app: "Sphinx", filetype: str) -> Publisher: reader = SphinxStandaloneReader() reader.setup(app) |