Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH/MAINT: avoid overwriting the HtmlTranslator #1105

Merged
merged 4 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import lru_cache
import json
from urllib.parse import urlparse, urlunparse
import types

import jinja2
from bs4 import BeautifulSoup as bs
Expand All @@ -22,7 +23,7 @@
import requests
from requests.exceptions import ConnectionError, HTTPError, RetryError

from .bootstrap_html_translator import BootstrapHTML5Translator
from .translator import BootstrapHTML5TranslatorMixin

__version__ = "0.12.1rc1.dev0"

Expand Down Expand Up @@ -1030,6 +1031,39 @@ def parse_url(self, uri):
return text


def setup_translators(app):
"""
Override translators respecting the one defined (if any).

We create a new class by inheriting the Sphinx Translator already defined and our own ``BootstrapHTML5TranslatorMixin`` that includes custom logic
12rambau marked this conversation as resolved.
Show resolved Hide resolved
"""
if not app.registry.translators.items():
translator = types.new_class(
"BootstrapHTML5Translator",
(
BootstrapHTML5TranslatorMixin,
app.builder.default_translator_class,
),
{},
)
app.set_translator(app.builder.name, translator, override=True)
else:
for name, klass in app.registry.translators.items():
if app.builder.format != "html":
# Skip translators that are not HTML
continue

translator = types.new_class(
"BootstrapHTML5Translator",
(
BootstrapHTML5TranslatorMixin,
klass,
),
{},
)
app.set_translator(name, translator, override=True)


# -----------------------------------------------------------------------------


Expand All @@ -1041,13 +1075,7 @@ def setup(app):

app.add_post_transform(ShortenLinkTransform)

app.set_translator("html", BootstrapHTML5Translator)
# Read the Docs uses ``readthedocs`` as the name of the build, and also
# uses a special "dirhtml" builder so we need to replace these both with
# our custom HTML builder
app.set_translator("readthedocs", BootstrapHTML5Translator, override=True)
app.set_translator("readthedocsdirhtml", BootstrapHTML5Translator, override=True)

app.connect("builder-inited", setup_translators)
app.connect("builder-inited", update_config)
app.connect("html-page-context", setup_edit_url)
app.connect("html-page-context", add_toctree_functions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""A custom Sphinx HTML Translator for Bootstrap layout
"""
A custom Sphinx HTML Translator for Bootstrap layout
"""
from packaging.version import Version

import sphinx
from sphinx.writers.html5 import HTML5Translator
from sphinx.util import logging
from sphinx.ext.autosummary import autosummary_table

logger = logging.getLogger(__name__)


class BootstrapHTML5Translator(HTML5Translator):
"""Custom HTML Translator for a Bootstrap-ified Sphinx layout
This is a specialization of the HTML5 Translator of sphinx.
class BootstrapHTML5TranslatorMixin:
"""
Mixin HTML Translator for a Bootstrap-ified Sphinx layout.
Only a couple of functions have been overridden to produce valid HTML to be
directly styled with Bootstrap, and fulfill acessibility best practices.
"""
Expand Down