diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-12-15 17:30:45 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-12-16 15:59:26 +0300 |
commit | 608246b0fa56a92f27b04a5b248ba59a204aaf89 (patch) | |
tree | 81e656eda9f84f7e97ed7a668bdac146aad463a7 /sphinx/io.py | |
parent | 42140fae390a13f9c57aae6837d6d7e9d9c0fe97 (diff) |
Move `source-read` event to SphinxStandaloneReader
Diffstat (limited to 'sphinx/io.py')
-rw-r--r-- | sphinx/io.py | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/sphinx/io.py b/sphinx/io.py index 1b326b804..0ddbc75e6 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -42,6 +42,7 @@ if False: # For type annotation from typing import Any, Dict, List, Tuple, Type, Union # NOQA from docutils import nodes # NOQA + from docutils.frontend import Values # NOQA from docutils.io import Input # NOQA from docutils.parsers import Parser # NOQA from docutils.transforms import Transform # NOQA @@ -64,6 +65,7 @@ class SphinxBaseReader(standalone.Reader): def __init__(self, app, *args, **kwargs): # type: (Sphinx, Any, Any) -> None + self.app = app self.env = app.env super().__init__(*args, **kwargs) @@ -106,6 +108,26 @@ class SphinxStandaloneReader(SphinxBaseReader): self.transforms = self.transforms + app.registry.get_transforms() super().__init__(app, *args, **kwargs) + def read(self, source, parser, settings): + # type: (Input, Parser, Values) -> nodes.document + self.source = source + if not self.parser: + self.parser = parser + self.settings = settings + self.input = self.read_source() + self.parse() + return self.document + + def read_source(self): + # type: () -> 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) + return arg[0] + class SphinxI18nReader(SphinxBaseReader): """ @@ -156,8 +178,7 @@ def SphinxDummySourceClass(source, *args, **kwargs): class SphinxBaseFileInput(FileInput): """A base class of SphinxFileInput. - It supports to replace unknown Unicode characters to '?'. And it also emits - Sphinx events :event:`source-read` on reading. + It supports to replace unknown Unicode characters to '?'. """ def __init__(self, app, env, *args, **kwds): @@ -168,19 +189,6 @@ class SphinxBaseFileInput(FileInput): kwds['error_handler'] = 'sphinx' # py3: handle error on open. super().__init__(*args, **kwds) - def read(self): - # type: () -> str - """Reads the contents from file. - - After reading, it emits Sphinx event ``source-read``. - """ - data = super().read() - - # emit source-read event - arg = [data] - self.app.emit('source-read', self.env.docname, arg) - return arg[0] - def warn_and_replace(self, error): # type: (Any) -> Tuple warnings.warn('SphinxBaseFileInput.warn_and_replace() is deprecated. ' |