diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-10-06 12:41:32 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-10-06 12:41:32 +0300 |
commit | e8925e78ca3be64a94c061e71a1da7d161640fa1 (patch) | |
tree | 9a62de9ce0baab55f07a7d72e4964653b2ad15bf /sphinx/io.py | |
parent | 00efa53decbdf7a49f0883deb61ea5be9e66fb21 (diff) | |
parent | 73a93f897e645d065ab67d384d561bb5a0a8ba7c (diff) |
Merge branch '2.0'
Diffstat (limited to 'sphinx/io.py')
-rw-r--r-- | sphinx/io.py | 61 |
1 files changed, 44 insertions, 17 deletions
diff --git a/sphinx/io.py b/sphinx/io.py index 38cd27cad..8f6c0ae15 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -17,6 +17,7 @@ from docutils.readers import standalone from docutils.transforms.references import DanglingReferences from docutils.writers import UnfilteredWriter +from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.transforms import ( AutoIndexUpgrader, DoctreeReadEvent, FigureAligner, SphinxTransformer ) @@ -54,12 +55,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 @@ -81,7 +105,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 @@ -95,10 +119,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 @@ -106,18 +130,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] @@ -130,8 +154,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, @@ -188,7 +214,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 == (): @@ -204,10 +231,10 @@ def read_doc(app, env, filename): # Sphinx-1.8 style source = input_class(app, env, source=None, source_path=filename, # type: ignore encoding=env.config.source_encoding) - pub = Publisher(reader=reader, # type: ignore + pub = Publisher(reader=reader, parser=parser, writer=SphinxDummyWriter(), - source_class=SphinxDummySourceClass, + source_class=SphinxDummySourceClass, # type: ignore destination=NullOutput()) pub.process_programmatic_settings(None, env.settings, None) pub.set_source(source, filename) |