Skip to content

Commit 9615d13

Browse files
committed
refactor: Move code to internal folder, expose public API in top-level module, document all public objects
1 parent 101651c commit 9615d13

File tree

11 files changed

+1300
-1206
lines changed

11 files changed

+1300
-1206
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Discussions = "https://github.com/mkdocstrings/autorefs/discussions"
4949
Gitter = "https://gitter.im/mkdocstrings/autorefs"
5050

5151
[project.entry-points."mkdocs.plugins"]
52-
autorefs = "mkdocs_autorefs.plugin:AutorefsPlugin"
52+
autorefs = "mkdocs_autorefs:AutorefsPlugin"
5353

5454
[tool.pdm.version]
5555
source = "call"

src/mkdocs_autorefs/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,33 @@
55

66
from __future__ import annotations
77

8-
__all__: list[str] = []
8+
from mkdocs_autorefs._internal.backlinks import Backlink, BacklinkCrumb, BacklinksTreeProcessor
9+
from mkdocs_autorefs._internal.plugin import AutorefsConfig, AutorefsPlugin
10+
from mkdocs_autorefs._internal.references import (
11+
AUTO_REF_RE,
12+
AUTOREF_RE,
13+
AnchorScannerTreeProcessor,
14+
AutorefsExtension,
15+
AutorefsHookInterface,
16+
AutorefsInlineProcessor,
17+
fix_ref,
18+
fix_refs,
19+
relative_url,
20+
)
21+
22+
__all__: list[str] = [
23+
"AUTOREF_RE",
24+
"AUTO_REF_RE",
25+
"AnchorScannerTreeProcessor",
26+
"AutorefsConfig",
27+
"AutorefsExtension",
28+
"AutorefsHookInterface",
29+
"AutorefsInlineProcessor",
30+
"AutorefsPlugin",
31+
"Backlink",
32+
"BacklinkCrumb",
33+
"BacklinksTreeProcessor",
34+
"fix_ref",
35+
"fix_refs",
36+
"relative_url",
37+
]
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Backlinks module."""
1+
# Backlinks module.
22

33
from __future__ import annotations
44

@@ -14,30 +14,33 @@
1414

1515
from markdown import Markdown
1616

17-
from mkdocs_autorefs.plugin import AutorefsPlugin
17+
from mkdocs_autorefs._internal.plugin import AutorefsPlugin
1818

1919
try:
2020
from mkdocs.plugins import get_plugin_logger
2121

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

2727

2828
@dataclass(eq=True, frozen=True, order=True)
2929
class BacklinkCrumb:
3030
"""A navigation breadcrumb for a backlink."""
3131

3232
title: str
33+
"""The title of the page."""
3334
url: str
35+
"""The URL of the page."""
3436

3537

3638
@dataclass(eq=True, frozen=True, order=True)
3739
class Backlink:
3840
"""A backlink (list of breadcrumbs)."""
3941

4042
crumbs: tuple[BacklinkCrumb, ...]
43+
"""The list of breadcrumbs."""
4144

4245

4346
class BacklinksTreeProcessor(Treeprocessor):
@@ -47,7 +50,10 @@ class BacklinksTreeProcessor(Treeprocessor):
4750
"""
4851

4952
name: str = "mkdocs-autorefs-backlinks"
53+
"""The name of the tree processor."""
5054
initial_id: str | None = None
55+
"""The initial heading ID."""
56+
5157
_htags: ClassVar[set[str]] = {"h1", "h2", "h3", "h4", "h5", "h6"}
5258

5359
def __init__(self, plugin: AutorefsPlugin, md: Markdown | None = None) -> None:
@@ -57,25 +63,30 @@ def __init__(self, plugin: AutorefsPlugin, md: Markdown | None = None) -> None:
5763
plugin: A reference to the autorefs plugin, to use its `register_anchor` method.
5864
"""
5965
super().__init__(md)
60-
self.plugin = plugin
61-
self.last_heading_id: str | None = None
66+
self._plugin = plugin
67+
self._last_heading_id: str | None = None
68+
69+
def run(self, root: Element) -> None:
70+
"""Run the tree processor.
6271
63-
def run(self, root: Element) -> None: # noqa: D102
64-
if self.plugin.current_page is not None:
65-
self.last_heading_id = self.initial_id
72+
Arguments:
73+
root: The root element of the document.
74+
"""
75+
if self._plugin.current_page is not None:
76+
self._last_heading_id = self.initial_id
6677
self._enhance_autorefs(root)
6778

6879
def _enhance_autorefs(self, parent: Element) -> None:
6980
for el in parent:
7081
if el.tag == "a": # Markdown anchor.
7182
if not (el.text or el.get("href") or (el.tail and el.tail.strip())) and (anchor_id := el.get("id")):
72-
self.last_heading_id = anchor_id
83+
self._last_heading_id = anchor_id
7384
elif el.tag in self._htags: # Heading.
74-
self.last_heading_id = el.get("id")
85+
self._last_heading_id = el.get("id")
7586
elif el.tag == "autoref":
7687
if "backlink-type" not in el.attrib:
7788
el.set("backlink-type", "referenced-by")
78-
if "backlink-anchor" not in el.attrib and self.last_heading_id:
79-
el.set("backlink-anchor", self.last_heading_id)
89+
if "backlink-anchor" not in el.attrib and self._last_heading_id:
90+
el.set("backlink-anchor", self._last_heading_id)
8091
else:
8192
self._enhance_autorefs(el)

0 commit comments

Comments
 (0)