Skip to content

Commit

Permalink
Inline io.read_doc and explain why settings are copied
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed May 7, 2022
1 parent f346e0a commit 97afe17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
17 changes: 13 additions & 4 deletions sphinx/builders/__init__.py
Expand Up @@ -14,10 +14,9 @@
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
Expand Down Expand Up @@ -469,7 +468,17 @@ def read_doc(self, docname: str) -> None:
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;
Expand Down
17 changes: 1 addition & 16 deletions sphinx/io.py
Expand Up @@ -19,7 +19,7 @@
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

Expand Down Expand Up @@ -152,21 +152,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
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)
Expand Down

0 comments on commit 97afe17

Please sign in to comment.