Skip to content

Commit

Permalink
refactor: Use new MkDocs plugin logger if available
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 2, 2023
1 parent 94b4af6 commit ca8d758
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/mkdocs_autorefs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
from mkdocs.structure.pages import Page
from mkdocs.structure.toc import AnchorLink

log = logging.getLogger(f"mkdocs.plugins.{__name__}")
try:
from mkdocs.plugins import get_plugin_logger

log = get_plugin_logger(__name__)
except ImportError:
# TODO: remove once support for MkDocs <1.5 is dropped
log = logging.getLogger(f"mkdocs.plugins.{__name__}") # type: ignore[assignment]


class AutorefsPlugin(BasePlugin):
Expand Down Expand Up @@ -126,7 +132,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
Returns:
The modified config.
"""
log.debug(f"{__name__}: Adding AutorefsExtension to the list")
log.debug("Adding AutorefsExtension to the list")
config["markdown_extensions"].append(AutorefsExtension())
return config

Expand Down Expand Up @@ -161,7 +167,7 @@ def on_page_content(self, html: str, page: Page, **kwargs: Any) -> str: # noqa:
The same HTML. We only use this hook to map anchors to URLs.
"""
if self.scan_toc:
log.debug(f"{__name__}: Mapping identifiers to URLs for page {page.file.src_path}")
log.debug(f"Mapping identifiers to URLs for page {page.file.src_path}")
for item in page.toc.items:
self.map_urls(page.url, item)
return html
Expand Down Expand Up @@ -200,15 +206,13 @@ def on_post_page(self, output: str, page: Page, **kwargs: Any) -> str: # noqa:
Returns:
Modified HTML.
"""
log.debug(f"{__name__}: Fixing references in page {page.file.src_path}")
log.debug(f"Fixing references in page {page.file.src_path}")

url_mapper = functools.partial(self.get_item_url, from_url=page.url, fallback=self.get_fallback_anchor)
fixed_output, unmapped = fix_refs(output, url_mapper)

if unmapped and log.isEnabledFor(logging.WARNING):
for ref in unmapped:
log.warning(
f"{__name__}: {page.file.src_path}: Could not find cross-reference target '[{ref}]'",
)
log.warning(f"{page.file.src_path}: Could not find cross-reference target '[{ref}]'")

return fixed_output

0 comments on commit ca8d758

Please sign in to comment.