diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-03-09 10:35:47 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-08-20 15:45:50 +0300 |
commit | 21cd1c2ef52699194a1ced02714681198c04b89d (patch) | |
tree | 66e86b236a6e76fbf67b46e3dc2162b1ae81b583 /sphinx/io.py | |
parent | 63757596b14991801ce09d1ac9bff425715e0c40 (diff) |
refactor: SphinxStandaloneReader should not hold an application object
Diffstat (limited to 'sphinx/io.py')
-rw-r--r-- | sphinx/io.py | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/sphinx/io.py b/sphinx/io.py index 5bf2d60da..419657e02 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -19,7 +19,7 @@ from docutils.statemachine import StringList, string2lines from docutils.transforms.references import DanglingReferences from docutils.writers import UnfilteredWriter -from sphinx.deprecation import RemovedInSphinx30Warning +from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning from sphinx.transforms import ( AutoIndexUpgrader, DoctreeReadEvent, FigureAligner, SphinxTransformer ) @@ -58,12 +58,35 @@ class SphinxBaseReader(standalone.Reader): transforms = [] # type: List[Type[Transform]] - def __init__(self, app, *args, **kwargs): - # type: (Sphinx, Any, Any) -> None - self.app = app - self.env = app.env + def __init__(self, *args, **kwargs): + # type: (Any, Any) -> None + from sphinx.application import Sphinx + if len(args) > 0 and isinstance(args[0], Sphinx): + self._app = args[0] + self._env = self._app.env + args = args[1:] + super().__init__(*args, **kwargs) + @property + def app(self): + # type: () -> Sphinx + warnings.warn('SphinxBaseReader.app is deprecated.', + RemovedInSphinx40Warning, stacklevel=2) + return self._app + + @property + def env(self): + # type: () -> BuildEnvironment + warnings.warn('SphinxBaseReader.env is deprecated.', + RemovedInSphinx40Warning, stacklevel=2) + return self._env + + def setup(self, app): + # type: (Sphinx) -> None + self._app = app # hold application object only for compatibility + self._env = app.env + def get_transforms(self): # type: () -> List[Type[Transform]] transforms = super().get_transforms() + self.transforms @@ -85,7 +108,7 @@ class SphinxBaseReader(standalone.Reader): # substitute transformer document.transformer = SphinxTransformer(document) - document.transformer.set_environment(self.env) + document.transformer.set_environment(self.settings.env) # substitute reporter reporter = document.reporter @@ -99,10 +122,10 @@ class SphinxStandaloneReader(SphinxBaseReader): A basic document reader for Sphinx. """ - def __init__(self, app, *args, **kwargs): - # type: (Sphinx, Any, Any) -> None + def setup(self, app): + # type: (Sphinx) -> None self.transforms = self.transforms + app.registry.get_transforms() - super().__init__(app, *args, **kwargs) + super().setup(app) def read(self, source, parser, settings): # type: (Input, Parser, Values) -> nodes.document @@ -110,18 +133,18 @@ class SphinxStandaloneReader(SphinxBaseReader): if not self.parser: self.parser = parser self.settings = settings - self.input = self.read_source() + self.input = self.read_source(settings.env) self.parse() return self.document - def read_source(self): - # type: () -> str + def read_source(self, env): + # type: (BuildEnvironment) -> str """Read content from source and do post-process.""" content = self.source.read() # emit "source-read" event arg = [content] - self.app.emit('source-read', self.env.docname, arg) + env.events.emit('source-read', env.docname, arg) return arg[0] @@ -134,8 +157,10 @@ class SphinxI18nReader(SphinxBaseReader): Because the translated texts are partial and they don't have correct line numbers. """ - def __init__(self, app, *args, **kwargs): - # type: (Sphinx, Any, Any) -> None + def setup(self, app): + # type: (Sphinx) -> None + super().setup(app) + self.transforms = self.transforms + app.registry.get_transforms() unused = [PreserveTranslatableMessages, Locale, RemoveTranslatableInline, AutoIndexUpgrader, FigureAligner, SphinxDomains, DoctreeReadEvent, @@ -144,8 +169,6 @@ class SphinxI18nReader(SphinxBaseReader): if transform in self.transforms: self.transforms.remove(transform) - super().__init__(app, *args, **kwargs) - def set_lineno_for_reporter(self, lineno): # type: (int) -> None """Stores the source line number of original text.""" @@ -290,7 +313,8 @@ def read_doc(app, env, filename): error_handler = UnicodeDecodeErrorHandler(env.docname) codecs.register_error('sphinx', error_handler) # type: ignore - reader = SphinxStandaloneReader(app) + reader = SphinxStandaloneReader() + reader.setup(app) filetype = get_filetype(app.config.source_suffix, filename) parser = app.registry.create_source_parser(app, filetype) if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == (): |