Migrate to py3 style type annotation: sphinx.ext.mathjax

This commit is contained in:
Takeshi KOMIYA 2019-06-30 00:33:19 +09:00
parent c0b267b5a6
commit ad81d788f9

View File

@ -11,27 +11,23 @@
""" """
import json import json
from typing import Any, Dict
from typing import cast from typing import cast
from docutils import nodes from docutils import nodes
import sphinx import sphinx
from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.domains.math import MathDomain from sphinx.domains.math import MathDomain
from sphinx.environment import BuildEnvironment
from sphinx.errors import ExtensionError from sphinx.errors import ExtensionError
from sphinx.locale import _ from sphinx.locale import _
from sphinx.util.math import get_node_equation_number from sphinx.util.math import get_node_equation_number
from sphinx.writers.html import HTMLTranslator
if False:
# For type annotation
from typing import Any, Dict # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.environment import BuildEnvironment # NOQA
from sphinx.writers.html import HTMLTranslator # NOQA
def html_visit_math(self, node): def html_visit_math(self: HTMLTranslator, node: nodes.math) -> None:
# type: (HTMLTranslator, nodes.math) -> None
self.body.append(self.starttag(node, 'span', '', CLASS='math notranslate nohighlight')) self.body.append(self.starttag(node, 'span', '', CLASS='math notranslate nohighlight'))
self.body.append(self.builder.config.mathjax_inline[0] + self.body.append(self.builder.config.mathjax_inline[0] +
self.encode(node.astext()) + self.encode(node.astext()) +
@ -39,8 +35,7 @@ def html_visit_math(self, node):
raise nodes.SkipNode raise nodes.SkipNode
def html_visit_displaymath(self, node): def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None:
# type: (HTMLTranslator, nodes.math_block) -> None
self.body.append(self.starttag(node, 'div', CLASS='math notranslate nohighlight')) self.body.append(self.starttag(node, 'div', CLASS='math notranslate nohighlight'))
if node['nowrap']: if node['nowrap']:
self.body.append(self.encode(node.astext())) self.body.append(self.encode(node.astext()))
@ -72,8 +67,7 @@ def html_visit_displaymath(self, node):
raise nodes.SkipNode raise nodes.SkipNode
def install_mathjax(app, env): def install_mathjax(app: Sphinx, env: BuildEnvironment) -> None:
# type: (Sphinx, BuildEnvironment) -> None
if app.builder.format != 'html' or app.builder.math_renderer_name != 'mathjax': # type: ignore # NOQA if app.builder.format != 'html' or app.builder.math_renderer_name != 'mathjax': # type: ignore # NOQA
return return
if not app.config.mathjax_path: if not app.config.mathjax_path:
@ -94,8 +88,7 @@ def install_mathjax(app, env):
builder.add_js_file(None, type="text/x-mathjax-config", body=body) builder.add_js_file(None, type="text/x-mathjax-config", body=body)
def setup(app): def setup(app: Sphinx) -> Dict[str, Any]:
# type: (Sphinx) -> Dict[str, Any]
app.add_html_math_renderer('mathjax', app.add_html_math_renderer('mathjax',
(html_visit_math, None), (html_visit_math, None),
(html_visit_displaymath, None)) (html_visit_displaymath, None))