1010and fixes them using the previously stored identifier-URL mapping.
1111"""
1212
13+ from __future__ import annotations
14+
1315import contextlib
1416import functools
1517import logging
16- from typing import Callable , Dict , Optional , Sequence
18+ from typing import TYPE_CHECKING , Any , Callable , Sequence
1719from urllib .parse import urlsplit
1820
19- from mkdocs .config import Config
2021from mkdocs .plugins import BasePlugin
21- from mkdocs .structure .pages import Page
22- from mkdocs .structure .toc import AnchorLink
2322from mkdocs .utils import warning_filter
2423
2524from mkdocs_autorefs .references import AutorefsExtension , fix_refs , relative_url
2625
26+ if TYPE_CHECKING :
27+ from mkdocs .config import Config
28+ from mkdocs .structure .pages import Page
29+ from mkdocs .structure .toc import AnchorLink
30+
2731log = logging .getLogger (f"mkdocs.plugins.{ __name__ } " )
2832log .addFilter (warning_filter )
2933
@@ -42,16 +46,16 @@ class AutorefsPlugin(BasePlugin):
4246 """
4347
4448 scan_toc : bool = True
45- current_page : Optional [ str ] = None
49+ current_page : str | None = None
4650
4751 def __init__ (self ) -> None :
4852 """Initialize the object."""
4953 super ().__init__ ()
50- self ._url_map : Dict [str , str ] = {}
51- self ._abs_url_map : Dict [str , str ] = {}
52- self .get_fallback_anchor : Optional [ Callable [[str ], Optional [ str ]]] = None # noqa: WPS234
54+ self ._url_map : dict [str , str ] = {}
55+ self ._abs_url_map : dict [str , str ] = {}
56+ self .get_fallback_anchor : Callable [[str ], str | None ] | None = None
5357
54- def register_anchor (self , page : str , identifier : str ):
58+ def register_anchor (self , page : str , identifier : str ) -> None :
5559 """Register that an anchor corresponding to an identifier was encountered when rendering the page.
5660
5761 Arguments:
@@ -60,7 +64,7 @@ def register_anchor(self, page: str, identifier: str):
6064 """
6165 self ._url_map [identifier ] = f"{ page } #{ identifier } "
6266
63- def register_url (self , identifier : str , url : str ):
67+ def register_url (self , identifier : str , url : str ) -> None :
6468 """Register that the identifier should be turned into a link to this URL.
6569
6670 Arguments:
@@ -69,10 +73,10 @@ def register_url(self, identifier: str, url: str):
6973 """
7074 self ._abs_url_map [identifier ] = url
7175
72- def _get_item_url ( # noqa: WPS234
76+ def _get_item_url (
7377 self ,
7478 identifier : str ,
75- fallback : Optional [ Callable [[str ], Sequence [str ]]] = None ,
79+ fallback : Callable [[str ], Sequence [str ]] | None = None ,
7680 ) -> str :
7781 try :
7882 return self ._url_map [identifier ]
@@ -88,11 +92,11 @@ def _get_item_url( # noqa: WPS234
8892 return url
8993 raise
9094
91- def get_item_url ( # noqa: WPS234
95+ def get_item_url (
9296 self ,
9397 identifier : str ,
94- from_url : Optional [ str ] = None ,
95- fallback : Optional [ Callable [[str ], Sequence [str ]]] = None ,
98+ from_url : str | None = None ,
99+ fallback : Callable [[str ], Sequence [str ]] | None = None ,
96100 ) -> str :
97101 """Return a site-relative URL with anchor to the identifier, if it's present anywhere.
98102
@@ -111,7 +115,7 @@ def get_item_url( # noqa: WPS234
111115 return relative_url (from_url , url )
112116 return url
113117
114- def on_config (self , config : Config , ** kwargs ) -> Config : # noqa: W0613,R0201 (unused arguments, cannot be static)
118+ def on_config (self , config : Config , ** kwargs : Any ) -> Config : # noqa: ARG002
115119 """Instantiate our Markdown extension.
116120
117121 Hook for the [`on_config` event](https://www.mkdocs.org/user-guide/plugins/#on_config).
@@ -129,7 +133,7 @@ def on_config(self, config: Config, **kwargs) -> Config: # noqa: W0613,R0201 (u
129133 config ["markdown_extensions" ].append (AutorefsExtension ())
130134 return config
131135
132- def on_page_markdown (self , markdown : str , page : Page , ** kwargs ) -> str : # noqa: W0613 (unused arguments)
136+ def on_page_markdown (self , markdown : str , page : Page , ** kwargs : Any ) -> str : # noqa: ARG002
133137 """Remember which page is the current one.
134138
135139 Arguments:
@@ -140,10 +144,10 @@ def on_page_markdown(self, markdown: str, page: Page, **kwargs) -> str: # noqa:
140144 Returns:
141145 The same Markdown. We only use this hook to map anchors to URLs.
142146 """
143- self .current_page = page .url # noqa: WPS601
147+ self .current_page = page .url
144148 return markdown
145149
146- def on_page_content (self , html : str , page : Page , ** kwargs ) -> str : # noqa: W0613 (unused arguments)
150+ def on_page_content (self , html : str , page : Page , ** kwargs : Any ) -> str : # noqa: ARG002
147151 """Map anchors to URLs.
148152
149153 Hook for the [`on_page_content` event](https://www.mkdocs.org/user-guide/plugins/#on_page_content).
@@ -178,7 +182,7 @@ def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
178182 for child in anchor .children :
179183 self .map_urls (base_url , child )
180184
181- def on_post_page (self , output : str , page : Page , ** kwargs ) -> str : # noqa: W0613 (unused arguments)
185+ def on_post_page (self , output : str , page : Page , ** kwargs : Any ) -> str : # noqa: ARG002
182186 """Fix cross-references.
183187
184188 Hook for the [`on_post_page` event](https://www.mkdocs.org/user-guide/plugins/#on_post_page).
0 commit comments