From 0f51f4208dfddb05752da815fc0efc18fd163abe Mon Sep 17 00:00:00 2001 From: AA Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 20 Apr 2021 03:40:22 +0100 Subject: [PATCH] Add extension setup and maths & settings overrides --- pep_sphinx_extensions/__init__.py | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pep_sphinx_extensions/__init__.py diff --git a/pep_sphinx_extensions/__init__.py b/pep_sphinx_extensions/__init__.py new file mode 100644 index 00000000000..d623f034ad5 --- /dev/null +++ b/pep_sphinx_extensions/__init__.py @@ -0,0 +1,45 @@ +"""Sphinx extensions for performant PEP processing""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from sphinx.environment import default_settings +from docutils.writers.html5_polyglot import HTMLTranslator + +from pep_sphinx_extensions.pep_processor.html import pep_html_translator +from pep_sphinx_extensions.pep_processor.parsing import pep_parser +from pep_sphinx_extensions.pep_processor.parsing import pep_role + +if TYPE_CHECKING: + from sphinx.application import Sphinx + +# Monkeypatch sphinx.environment.default_settings as Sphinx doesn't allow custom settings or Readers +default_settings |= { + "pep_references": True, + "rfc_references": True, + "pep_base_url": "", + "pep_file_url_template": "pep-%04d.html", + "_disable_config": True, # disable using docutils.conf whilst running both PEP generators +} + + +def _depart_maths(): + pass # No-op callable for the type checker + + +def setup(app: Sphinx) -> dict[str, bool]: + """Initialize Sphinx extension.""" + + # Register plugin logic + app.add_source_parser(pep_parser.PEPParser) # Add PEP transforms + app.add_role("pep", pep_role.PEPRole(), override=True) # Transform PEP references to links + app.set_translator("html", pep_html_translator.PEPTranslator) # Docutils Node Visitor overrides + + # Mathematics rendering + inline_maths = HTMLTranslator.visit_math, _depart_maths + block_maths = HTMLTranslator.visit_math_block, _depart_maths + app.add_html_math_renderer("maths_to_html", inline_maths, block_maths) # Render maths to HTML + + # Parallel safety: https://www.sphinx-doc.org/en/master/extdev/index.html#extension-metadata + return {"parallel_read_safe": True, "parallel_write_safe": True}