Skip to content

Commit 5e89cd8

Browse files
committed
refactor: Deprecate fallback mechanism
Issue-61: #61
1 parent 7f45227 commit 5e89cd8

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/mkdocs_autorefs/plugin.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pathlib import PurePosixPath as URL # noqa: N814
1919
from typing import TYPE_CHECKING, Any, Callable
2020
from urllib.parse import urlsplit
21+
from warnings import warn
2122

2223
from mkdocs.config.base import Config
2324
from mkdocs.config.config_options import Type
@@ -110,8 +111,26 @@ def __init__(self) -> None:
110111
self._primary_url_map: dict[str, list[str]] = {}
111112
self._secondary_url_map: dict[str, list[str]] = {}
112113
self._abs_url_map: dict[str, str] = {}
113-
114-
self.get_fallback_anchor: Callable[[str], tuple[str, ...]] | None = None
114+
# YORE: Bump 2: Remove line.
115+
self._get_fallback_anchor: Callable[[str], tuple[str, ...]] | None = None
116+
117+
# YORE: Bump 2: Remove block.
118+
@property
119+
def get_fallback_anchor(self) -> Callable[[str], tuple[str, ...]] | None:
120+
"""Fallback anchors getter."""
121+
return self._get_fallback_anchor
122+
123+
# YORE: Bump 2: Remove block.
124+
@get_fallback_anchor.setter
125+
def get_fallback_anchor(self, value: Callable[[str], tuple[str, ...]] | None) -> None:
126+
"""Fallback anchors setter."""
127+
self._get_fallback_anchor = value
128+
if value is not None:
129+
warn(
130+
"Setting a fallback anchor function is deprecated and will be removed in a future release.",
131+
DeprecationWarning,
132+
stacklevel=2,
133+
)
115134

116135
def register_anchor(self, page: str, identifier: str, anchor: str | None = None, *, primary: bool = True) -> None:
117136
"""Register that an anchor corresponding to an identifier was encountered when rendering the page.
@@ -183,12 +202,14 @@ def _get_urls(self, identifier: str) -> tuple[list[str], str]:
183202
def _get_item_url(
184203
self,
185204
identifier: str,
186-
fallback: Callable[[str], Sequence[str]] | None = None,
187205
from_url: str | None = None,
206+
# YORE: Bump 2: Remove line.
207+
fallback: Callable[[str], Sequence[str]] | None = None,
188208
) -> str:
189209
try:
190210
urls, qualifier = self._get_urls(identifier)
191211
except KeyError:
212+
# YORE: Bump 2: Replace block with line 2.
192213
if identifier in self._abs_url_map:
193214
return self._abs_url_map[identifier]
194215
if fallback:
@@ -216,19 +237,20 @@ def get_item_url(
216237
self,
217238
identifier: str,
218239
from_url: str | None = None,
240+
# YORE: Bump 2: Remove line.
219241
fallback: Callable[[str], Sequence[str]] | None = None,
220242
) -> str:
221243
"""Return a site-relative URL with anchor to the identifier, if it's present anywhere.
222244
223245
Arguments:
224246
identifier: The anchor (without '#').
225247
from_url: The URL of the base page, from which we link towards the targeted pages.
226-
fallback: An optional function to suggest alternative anchors to try on failure.
227248
228249
Returns:
229250
A site-relative URL.
230251
"""
231-
url = self._get_item_url(identifier, fallback, from_url)
252+
# YORE: Bump 2: Replace `, fallback` with `` within line.
253+
url = self._get_item_url(identifier, from_url, fallback)
232254
if from_url is not None:
233255
parsed = urlsplit(url)
234256
if not parsed.scheme and not parsed.netloc:
@@ -325,6 +347,7 @@ def on_post_page(self, output: str, page: Page, **kwargs: Any) -> str: # noqa:
325347
"""
326348
log.debug("Fixing references in page %s", page.file.src_path)
327349

350+
# YORE: Bump 2: Replace `, fallback=self.get_fallback_anchor` with `` within line.
328351
url_mapper = functools.partial(self.get_item_url, from_url=page.url, fallback=self.get_fallback_anchor)
329352
fixed_output, unmapped = fix_refs(output, url_mapper, _legacy_refs=self.legacy_refs)
330353

tests/test_plugin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_url_registration_with_from_url() -> None:
3434
plugin.get_item_url("baz", from_url="a/b.html")
3535

3636

37+
# YORE: Bump 2: Remove block.
3738
def test_url_registration_with_fallback() -> None:
3839
"""Check that URLs can be registered, then obtained through a fallback."""
3940
plugin = AutorefsPlugin()
@@ -59,10 +60,7 @@ def test_dont_make_relative_urls_relative_again() -> None:
5960
plugin.register_anchor(identifier="foo.bar.baz", page="foo/bar/baz.html", primary=True)
6061

6162
for _ in range(2):
62-
assert (
63-
plugin.get_item_url("hello", from_url="baz/bar/foo.html", fallback=lambda _: ("foo.bar.baz",))
64-
== "../../foo/bar/baz.html#foo.bar.baz"
65-
)
63+
assert plugin.get_item_url("foo.bar.baz", from_url="baz/bar/foo.html") == "../../foo/bar/baz.html#foo.bar.baz"
6664

6765

6866
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)