-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathdocs_hooks.py
More file actions
48 lines (34 loc) · 1.81 KB
/
docs_hooks.py
File metadata and controls
48 lines (34 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""MkDocs hooks for documentation build adjustments."""
import logging
class SuppressDuplicatePrimaryURLFilter(logging.Filter):
"""Silences mkdocstrings-autorefs "Multiple primary URLs found" warnings.
These warnings are expected in bilingual (i18n) builds because the same
Python identifier is documented in both the default and translated API
reference pages. The duplicate-primary-URL warning is noise in this
context and does not indicate a real cross-reference error.
"""
def filter(self, record: logging.LogRecord) -> bool:
return "Multiple primary URLs found" not in record.getMessage()
def _suppress_autorefs_duplicates():
"""Apply logging filter to all mkdocs_autorefs loggers.
mkdocs_autorefs uses child loggers such as
``mkdocs.plugins.mkdocs_autorefs._internal.references``. We must attach
the filter to every concrete logger that emits the warning.
"""
target_prefix = "mkdocs.plugins.mkdocs_autorefs"
filter_obj = SuppressDuplicatePrimaryURLFilter()
for name, logger in logging.Logger.manager.loggerDict.items():
if name.startswith(target_prefix) and isinstance(logger, logging.Logger):
if not any(isinstance(f, SuppressDuplicatePrimaryURLFilter) for f in logger.filters):
logger.addFilter(filter_obj)
# Also cover the parent logger itself
parent = logging.getLogger(target_prefix)
if not any(isinstance(f, SuppressDuplicatePrimaryURLFilter) for f in parent.filters):
parent.addFilter(filter_obj)
def on_config(config, **kwargs):
"""Apply logging filters after configuration is loaded."""
_suppress_autorefs_duplicates()
return config
def on_pre_build(config, **kwargs):
"""Re-apply filter right before build starts (plugins may recreate loggers)."""
_suppress_autorefs_duplicates()