Skip to content

Commit 4f2be46

Browse files
committed
refactor: Emit deprecation warnings when old-style spans are found
1 parent e422990 commit 4f2be46

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/mkdocs_autorefs/references.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ def inner(match: Match) -> str:
216216
return f"[{identifier}][]"
217217
return f"[{title}][{identifier}]"
218218

219+
warnings.warn(
220+
"autorefs `span` elements are deprecated in favor of `autoref` elements: "
221+
f'`<span data-autorefs-identifier="{identifier}">...</span>` becomes `<autoref identifer="{identifier}">...</autoref>`',
222+
DeprecationWarning,
223+
stacklevel=1,
224+
)
219225
parsed = urlsplit(url)
220226
external = parsed.scheme or parsed.netloc
221227
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal", *classes]

tests/test_references.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def test_legacy_custom_required_reference() -> None:
216216
"""Check that external HTML-based references are expanded or reported missing."""
217217
url_map = {"ok": "ok.html#ok"}
218218
source = "<span data-autorefs-identifier=bar>foo</span> <span data-autorefs-identifier=ok>ok</span>"
219-
output, unmapped = fix_refs(source, url_map.__getitem__)
219+
with pytest.warns(DeprecationWarning, match="`span` elements are deprecated"):
220+
output, unmapped = fix_refs(source, url_map.__getitem__)
220221
assert output == '[foo][bar] <a class="autorefs autorefs-internal" href="ok.html#ok">ok</a>'
221222
assert unmapped == ["bar"]
222223

@@ -234,7 +235,8 @@ def test_legacy_custom_optional_reference() -> None:
234235
"""Check that optional HTML-based references are expanded and never reported missing."""
235236
url_map = {"ok": "ok.html#ok"}
236237
source = '<span data-autorefs-optional="bar">foo</span> <span data-autorefs-optional=ok>ok</span>'
237-
output, unmapped = fix_refs(source, url_map.__getitem__)
238+
with pytest.warns(DeprecationWarning, match="`span` elements are deprecated"):
239+
output, unmapped = fix_refs(source, url_map.__getitem__)
238240
assert output == 'foo <a class="autorefs autorefs-internal" href="ok.html#ok">ok</a>'
239241
assert unmapped == []
240242

@@ -252,7 +254,8 @@ def test_legacy_custom_optional_hover_reference() -> None:
252254
"""Check that optional-hover HTML-based references are expanded and never reported missing."""
253255
url_map = {"ok": "ok.html#ok"}
254256
source = '<span data-autorefs-optional-hover="bar">foo</span> <span data-autorefs-optional-hover=ok>ok</span>'
255-
output, unmapped = fix_refs(source, url_map.__getitem__)
257+
with pytest.warns(DeprecationWarning, match="`span` elements are deprecated"):
258+
output, unmapped = fix_refs(source, url_map.__getitem__)
256259
assert (
257260
output
258261
== '<span title="bar">foo</span> <a class="autorefs autorefs-internal" title="ok" href="ok.html#ok">ok</a>'
@@ -276,7 +279,8 @@ def test_legacy_external_references() -> None:
276279
"""Check that external references are marked as such."""
277280
url_map = {"example": "https://example.com"}
278281
source = '<span data-autorefs-optional="example">example</span>'
279-
output, unmapped = fix_refs(source, url_map.__getitem__)
282+
with pytest.warns(DeprecationWarning, match="`span` elements are deprecated"):
283+
output, unmapped = fix_refs(source, url_map.__getitem__)
280284
assert output == '<a class="autorefs autorefs-external" href="https://example.com">example</a>'
281285
assert unmapped == []
282286

@@ -382,7 +386,8 @@ def test_legacy_keep_data_attributes() -> None:
382386
"""Keep HTML data attributes from autorefs spans."""
383387
url_map = {"example": "https://e.com"}
384388
source = '<span data-autorefs-optional="example" class="hi ho" data-foo data-bar="0">e</span>'
385-
output, _ = fix_refs(source, url_map.__getitem__)
389+
with pytest.warns(DeprecationWarning, match="`span` elements are deprecated"):
390+
output, _ = fix_refs(source, url_map.__getitem__)
386391
assert output == '<a class="autorefs autorefs-external hi ho" href="https://e.com" data-foo data-bar="0">e</a>'
387392

388393

0 commit comments

Comments
 (0)