|
20 | 20 | logger = logging.getLogger(__name__) |
21 | 21 |
|
22 | 22 |
|
23 | | -def log_override_warning(app: Sphinx, version: int, current: str, new: str) -> None: |
| 23 | +def log_override_warning(app: Sphinx, config_name: str, current: str, new: str) -> None: |
24 | 24 | """Log a warning if MathJax configuration being overridden.""" |
25 | 25 | if logging.is_suppressed_warning("myst", "mathjax", app.config.suppress_warnings): |
26 | 26 | return |
27 | | - config_name = ( |
28 | | - "mathjax3_config['options']['processHtmlClass']" |
29 | | - if version == 3 |
30 | | - else "mathjax_config['tex2jax']['processClass']" |
31 | | - ) |
32 | 27 | logger.warning( |
33 | | - f"`{config_name}` is being overridden by myst-parser: '{current}' -> '{new}'. " |
| 28 | + f"`{config_name}['options']['processHtmlClass']` " |
| 29 | + f"is being overridden by myst-parser: '{current}' -> '{new}'. " |
34 | 30 | "Set `suppress_warnings=['myst.mathjax']` to ignore this warning, or " |
35 | 31 | "`myst_update_mathjax=False` if this is undesirable." |
36 | 32 | ) |
@@ -59,43 +55,33 @@ def override_mathjax(app: Sphinx): |
59 | 55 |
|
60 | 56 | mjax_classes = app.env.myst_config.mathjax_classes # type: ignore[attr-defined] |
61 | 57 |
|
62 | | - mathjax_opt = None |
63 | | - for opt in ("mathjax4_config", "mathjax3_config"): |
64 | | - if getattr(app.config, opt, None): |
65 | | - mathjax_opt = opt |
66 | | - break |
67 | | - if mathjax_opt is not None: |
68 | | - # sphinx 4 + mathjax 3 |
69 | | - # sphinx 9 + mathjax 4 |
70 | | - config = getattr(app.config, opt, {}) or {} |
71 | | - config.setdefault("options", {}) |
72 | | - if ( |
73 | | - "processHtmlClass" in config["options"] |
74 | | - and config["options"]["processHtmlClass"] != mjax_classes |
75 | | - ): |
76 | | - log_override_warning( |
77 | | - app, |
78 | | - 3, |
79 | | - config["options"]["processHtmlClass"], |
80 | | - mjax_classes, |
81 | | - ) |
82 | | - config["options"]["processHtmlClass"] = mjax_classes |
83 | | - setattr(app.config, opt, config) |
84 | | - elif "mathjax_config" in app.config: |
85 | | - # sphinx 3 + mathjax 2 |
86 | | - app.config.mathjax_config = app.config.mathjax_config or {} |
87 | | - app.config.mathjax_config.setdefault("tex2jax", {}) |
88 | | - if ( |
89 | | - "processClass" in app.config.mathjax_config["tex2jax"] |
90 | | - and app.config.mathjax_config["tex2jax"]["processClass"] != mjax_classes |
91 | | - ): |
92 | | - log_override_warning( |
93 | | - app, |
94 | | - 2, |
95 | | - app.config.mathjax_config["tex2jax"]["processClass"], |
96 | | - mjax_classes, |
97 | | - ) |
98 | | - app.config.mathjax_config["tex2jax"]["processClass"] = mjax_classes |
| 58 | + # Determine which mathjax config to modify: |
| 59 | + # prefer whichever the user has explicitly set, falling back to |
| 60 | + # the Sphinx version's default (mathjax4_config for Sphinx 9+, mathjax3_config for 8) |
| 61 | + has_mjax4 = hasattr(app.config, "mathjax4_config") |
| 62 | + if has_mjax4 and app.config.mathjax4_config: |
| 63 | + config_attr = "mathjax4_config" |
| 64 | + elif app.config.mathjax3_config: |
| 65 | + config_attr = "mathjax3_config" |
| 66 | + elif has_mjax4: |
| 67 | + config_attr = "mathjax4_config" |
| 68 | + else: |
| 69 | + config_attr = "mathjax3_config" |
| 70 | + |
| 71 | + config: dict = getattr(app.config, config_attr) or {} # type: ignore[type-arg] |
| 72 | + config.setdefault("options", {}) |
| 73 | + if ( |
| 74 | + "processHtmlClass" in config["options"] |
| 75 | + and config["options"]["processHtmlClass"] != mjax_classes |
| 76 | + ): |
| 77 | + log_override_warning( |
| 78 | + app, |
| 79 | + config_attr, |
| 80 | + config["options"]["processHtmlClass"], |
| 81 | + mjax_classes, |
| 82 | + ) |
| 83 | + config["options"]["processHtmlClass"] = mjax_classes |
| 84 | + setattr(app.config, config_attr, config) |
99 | 85 |
|
100 | 86 |
|
101 | 87 | def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None: |
|
0 commit comments