-
Notifications
You must be signed in to change notification settings - Fork 196
/
warnings_.py
119 lines (98 loc) · 3.81 KB
/
warnings_.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Central handling of warnings for the myst extension."""
from __future__ import annotations
from enum import Enum
from typing import Sequence
from docutils import nodes
class MystWarnings(Enum):
"""MyST warning types."""
DEPRECATED = "deprecated"
"""Deprecated usage."""
NOT_SUPPORTED = "not_supported"
"""Functionality that is not yet supported in docutils."""
RENDER_METHOD = "render"
"""The render method is not implemented."""
MD_TOPMATTER = "topmatter"
"""Issue reading front-matter."""
MD_DEF_DUPE = "duplicate_def"
"""Duplicate Markdown reference definition."""
MD_FOOTNOTE_DUPE = "footnote"
"""Duplicate Markdown footnote definition."""
MD_FOOTNOTE_MISSING = "footnote"
"""Missing Markdown footnote definition."""
MD_HEADING_NON_CONSECUTIVE = "header"
"""Non-consecutive heading levels."""
DIRECTIVE_PARSING = "directive_parse"
"""Issue parsing directive."""
UNKNOWN_DIRECTIVE = "directive_unknown"
"""Unknown directive."""
UNKNOWN_ROLE = "role_unknown"
"""Unknown role."""
# cross-reference resolution
XREF_AMBIGUOUS = "xref_ambiguous"
"""Multiple targets were found for a cross-reference."""
XREF_MISSING = "xref_missing"
"""A target was not found for a cross-reference."""
INV_LOAD = "inv_retrieval"
"""Failure to retrieve or load an inventory."""
IREF_MISSING = "iref_missing"
"""A target was not found for an inventory reference."""
IREF_AMBIGUOUS = "iref_ambiguous"
"""Multiple targets were found for an inventory reference."""
LEGACY_DOMAIN = "domains"
"""A legacy domain found, which does not support `resolve_any_xref`."""
# extensions
HEADING_SLUG = "heading_slug"
"""An error occured computing a heading slug."""
STRIKETHROUGH = "strikethrough"
"""Strikethrough warning, since only implemented in HTML."""
HTML_PARSE = "html"
"""HTML could not be parsed."""
INVALID_ATTRIBUTE = "attribute"
"""Invalid attribute value."""
def _is_suppressed_warning(
type: str, subtype: str, suppress_warnings: Sequence[str]
) -> bool:
"""Check whether the warning is suppressed or not.
Mirrors:
https://github.com/sphinx-doc/sphinx/blob/47d9035bca9e83d6db30a0726a02dc9265bd66b1/sphinx/util/logging.py
"""
if type is None:
return False
subtarget: str | None
for warning_type in suppress_warnings:
if "." in warning_type:
target, subtarget = warning_type.split(".", 1)
else:
target, subtarget = warning_type, None
if target == type and subtarget in (None, subtype, "*"):
return True
return False
def create_warning(
document: nodes.document,
message: str,
subtype: MystWarnings,
*,
line: int | None = None,
append_to: nodes.Element | None = None,
) -> nodes.system_message | None:
"""Generate a warning, logging if it is necessary.
If the warning type is listed in the ``suppress_warnings`` configuration,
then ``None`` will be returned and no warning logged.
"""
wtype = "myst"
# figure out whether to suppress the warning, if sphinx is available,
# it will have been set up by the Sphinx environment,
# otherwise we will use the configuration set by docutils
suppress_warnings: Sequence[str] = []
try:
suppress_warnings = document.settings.env.app.config.suppress_warnings
except AttributeError:
suppress_warnings = document.settings.myst_suppress_warnings or []
if _is_suppressed_warning(wtype, subtype.value, suppress_warnings):
return None
kwargs = {"line": line} if line is not None else {}
message = f"{message} [{wtype}.{subtype.value}]"
msg_node = document.reporter.warning(message, **kwargs)
if append_to is not None:
append_to.append(msg_node)
return msg_node