|
9 | 9 | import sys |
10 | 10 | import warnings |
11 | 11 | from functools import lru_cache, partial |
| 12 | +from pathlib import Path |
12 | 13 | from typing import TYPE_CHECKING, Any, Callable, Match, Pattern, Sequence |
13 | 14 |
|
| 15 | +from griffe.dataclasses import Alias, Object |
14 | 16 | from griffe.docstrings.dataclasses import ( |
15 | 17 | DocstringSectionAttributes, |
16 | 18 | DocstringSectionClasses, |
17 | 19 | DocstringSectionFunctions, |
18 | 20 | DocstringSectionModules, |
19 | 21 | ) |
20 | | -from jinja2 import pass_context |
| 22 | +from jinja2 import TemplateNotFound, pass_context, pass_environment |
21 | 23 | from markupsafe import Markup |
22 | 24 | from mkdocstrings.loggers import get_logger |
23 | 25 |
|
24 | 26 | if TYPE_CHECKING: |
25 | | - from griffe.dataclasses import Alias, Attribute, Class, Function, Module, Object |
| 27 | + from griffe.dataclasses import Attribute, Class, Function, Module |
| 28 | + from jinja2 import Environment, Template |
26 | 29 | from jinja2.runtime import Context |
27 | 30 | from mkdocstrings.handlers.base import CollectorItem |
28 | 31 |
|
@@ -137,7 +140,8 @@ def do_format_signature( |
137 | 140 | The same code, formatted. |
138 | 141 | """ |
139 | 142 | env = context.environment |
140 | | - template = env.get_template("signature.html") |
| 143 | + # TODO: Stop using `do_get_template` when `*.html` templates are removed. |
| 144 | + template = env.get_template(do_get_template(env, "signature")) |
141 | 145 | config_annotations = context.parent["config"]["show_signature_annotations"] |
142 | 146 | old_stash_ref_filter = env.filters["stash_crossref"] |
143 | 147 |
|
@@ -204,7 +208,8 @@ def do_format_attribute( |
204 | 208 | The same code, formatted. |
205 | 209 | """ |
206 | 210 | env = context.environment |
207 | | - template = env.get_template("expression.html") |
| 211 | + # TODO: Stop using `do_get_template` when `*.html` templates are removed. |
| 212 | + template = env.get_template(do_get_template(env, "expression")) |
208 | 213 | annotations = context.parent["config"]["show_signature_annotations"] |
209 | 214 | separate_signature = context.parent["config"]["separate_signature"] |
210 | 215 | old_stash_ref_filter = env.filters["stash_crossref"] |
@@ -448,17 +453,46 @@ def formatter(code: str, line_length: int) -> str: |
448 | 453 | return formatter |
449 | 454 |
|
450 | 455 |
|
451 | | -def do_get_template(obj: Object) -> str: |
| 456 | +@pass_environment |
| 457 | +def do_get_template(env: Environment, obj: str | Object) -> str | Template: |
452 | 458 | """Get the template name used to render an object. |
453 | 459 |
|
454 | 460 | Parameters: |
455 | | - obj: A Griffe object. |
| 461 | + env: The Jinja environment, passed automatically. |
| 462 | + obj: A Griffe object, or a template name. |
456 | 463 |
|
457 | 464 | Returns: |
458 | 465 | A template name. |
459 | 466 | """ |
460 | | - extra_data = getattr(obj, "extra", {}).get("mkdocstrings", {}) |
461 | | - return extra_data.get("template", "") or f"{obj.kind.value}.html" |
| 467 | + name = obj |
| 468 | + if isinstance(obj, (Alias, Object)): |
| 469 | + extra_data = getattr(obj, "extra", {}).get("mkdocstrings", {}) |
| 470 | + if name := extra_data.get("template", ""): |
| 471 | + return name |
| 472 | + name = obj.kind.value |
| 473 | + try: |
| 474 | + template = env.get_template(f"{name}.html") |
| 475 | + except TemplateNotFound: |
| 476 | + return f"{name}.html.jinja" |
| 477 | + else: |
| 478 | + # TODO: Remove once support for Python 3.8 is dropped. |
| 479 | + if sys.version_info < (3, 9): |
| 480 | + try: |
| 481 | + Path(template.filename).relative_to(Path(__file__).parent) # type: ignore[arg-type] |
| 482 | + except ValueError: |
| 483 | + our_template = False |
| 484 | + else: |
| 485 | + our_template = True |
| 486 | + else: |
| 487 | + our_template = Path(template.filename).is_relative_to(Path(__file__).parent) # type: ignore[arg-type] |
| 488 | + if not our_template: |
| 489 | + # TODO: Switch to a warning log after some time. |
| 490 | + logger.info( |
| 491 | + f"DeprecationWarning: Overriding '{name}.html' is deprecated, override '{name}.html.jinja' instead. " |
| 492 | + "After some time, this message will be logged as a warning, causing strict builds to fail.", |
| 493 | + once=True, |
| 494 | + ) |
| 495 | + return f"{name}.html" |
462 | 496 |
|
463 | 497 |
|
464 | 498 | @pass_context |
|
0 commit comments