Skip to content

Commit 1510656

Browse files
committed
refactor: Check builder support by checking translation_handlers
1 parent 4db8cd4 commit 1510656

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/sphinxnotes/strike/__init__.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
from sphinx.application import Sphinx
1919
from sphinx.builders import Builder
20-
from sphinx.builders.html import StandaloneHTMLBuilder
21-
from sphinx.builders.latex import LaTeXBuilder
20+
from sphinx.util import logging
21+
from sphinx.environment import BuildEnvironment
2222

2323
from . import meta
2424

25+
logger = logging.getLogger(__name__)
2526

26-
# List of all builders that support render :class:`strike_node`.
27-
SUPPORTED_BUILDERS: list[type[Builder]] = [StandaloneHTMLBuilder, LaTeXBuilder]
2827

2928
class strike_node(nodes.Inline, nodes.TextElement):
3029
pass
@@ -39,10 +38,16 @@ def strike_role(
3938
options: Dict = {},
4039
content: List[str] = [],
4140
) -> Tuple[List[Node], List[system_message]]:
42-
env = inliner.document.settings.env # type: ignore
43-
44-
if not isinstance(env.app.builder, tuple(SUPPORTED_BUILDERS)):
45-
# Builder is not supported, fallback to text.
41+
env: BuildEnvironment = inliner.document.settings.env # type: ignore
42+
builder = env.app.builder
43+
44+
if not _is_supported_builder(builder):
45+
logger.warning(
46+
f'role {typ} is not supported for builder {builder.name}, fallback to text',
47+
location=(env.docname, lineno),
48+
type='strike',
49+
subtype='unspported_builder',
50+
)
4651
return [Text(unescape(text))], []
4752

4853
node = strike_node(rawtext, unescape(text))
@@ -74,6 +79,15 @@ def latext_depart_strike_node(self, node: strike_node) -> None:
7479
self.body.append('}')
7580

7681

82+
def _is_supported_builder(builder: Builder) -> bool:
83+
app = builder.app
84+
# a dict of builder name -> dict of node name -> visitor and departure functions
85+
return (
86+
app.registry.translation_handlers[builder.name].get(strike_node.__name__)
87+
is not None
88+
)
89+
90+
7791
def setup(app: Sphinx):
7892
"""Sphinx extension entrypoint."""
7993
meta.pre_setup(app)

0 commit comments

Comments
 (0)