diff options
| author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-02-14 01:09:28 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-14 01:09:28 +0900 |
| commit | 549a763195580284bbb07b3bfbb8ae894dfaddda (patch) | |
| tree | ba4ca846753825716ba4226cd82105419dd2f7f1 /sphinx/util | |
| parent | e00ec8d7f94f90643171cacdacb0b2a5754b182d (diff) | |
| parent | ac70a4dd91fbfeaaf0a525a6802685273b67bf35 (diff) | |
| download | sphinx-git-549a763195580284bbb07b3bfbb8ae894dfaddda.tar.gz | |
Merge pull request #6025 from tk0miya/refactor_roles2
Introduce SphinxRole class as a base class of roles
Diffstat (limited to 'sphinx/util')
| -rw-r--r-- | sphinx/util/docutils.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py index e55cd016e..ba5a9d0ea 100644 --- a/sphinx/util/docutils.py +++ b/sphinx/util/docutils.py @@ -23,7 +23,7 @@ from docutils import nodes from docutils.io import FileOutput from docutils.parsers.rst import Directive, directives, roles, convert_directive_function from docutils.statemachine import StateMachine -from docutils.utils import Reporter +from docutils.utils import Reporter, unescape from sphinx.deprecation import RemovedInSphinx30Warning from sphinx.errors import ExtensionError @@ -36,7 +36,8 @@ report_re = re.compile('^(.+?:(?:\\d+)?): \\((DEBUG|INFO|WARNING|ERROR|SEVERE)/( if False: # For type annotation from types import ModuleType # NOQA - from typing import Any, Callable, Generator, List, Set, Tuple, Type # NOQA + from typing import Any, Callable, Dict, Generator, List, Set, Tuple, Type # NOQA + from docutils.parsers.rst.states import Inliner # NOQA from docutils.statemachine import State, StringList # NOQA from sphinx.builders import Builder # NOQA from sphinx.config import Config # NOQA @@ -383,6 +384,44 @@ class SphinxDirective(Directive): return self.env.config +class SphinxRole: + """A base class for Sphinx roles. + + This class provides helper methods for Sphinx roles. + + .. note:: The subclasses of this class might not work with docutils. + This class is strongly coupled with Sphinx. + """ + + def __call__(self, typ, rawtext, text, lineno, inliner, options={}, content=[]): + # type: (str, str, str, int, Inliner, Dict, List[str]) -> Tuple[List[nodes.Node], List[nodes.system_message]] # NOQA + self.type = typ + self.rawtext = rawtext + self.text = unescape(text) + self.lineno = lineno + self.inliner = inliner + self.options = options + self.content = content + + return self.run() + + def run(self): + # type: () -> Tuple[List[nodes.Node], List[nodes.system_message]] + raise NotImplementedError + + @property + def env(self): + # type: () -> BuildEnvironment + """Reference to the :class:`.BuildEnvironment` object.""" + return self.inliner.document.settings.env + + @property + def config(self): + # type: () -> Config + """Reference to the :class:`.Config` object.""" + return self.env.config + + class SphinxTranslator(nodes.NodeVisitor): """A base class for Sphinx translators. |
