Skip to content

Commit 9615d13

Browse files
committedFeb 24, 2025
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
 

Diff for: ‎pyproject.toml

+1-1
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"

Diff for: ‎src/mkdocs_autorefs/__init__.py

+30-1
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+
]
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)

Diff for: ‎src/mkdocs_autorefs/_internal/plugin.py

+519
Large diffs are not rendered by default.

Diff for: ‎src/mkdocs_autorefs/_internal/references.py

+694
Large diffs are not rendered by default.

Diff for: ‎src/mkdocs_autorefs/plugin.py

+12-512
Large diffs are not rendered by default.

Diff for: ‎src/mkdocs_autorefs/references.py

+9-670
Large diffs are not rendered by default.

Diff for: ‎tests/test_api.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ def test_inventory_matches_api(
152152
not_in_api = []
153153
public_api_paths = {obj.path for obj in public_objects}
154154
public_api_paths.add("mkdocs_autorefs")
155+
ignore = {"mkdocs_autorefs.plugin", "mkdocs_autorefs.references"}
155156
for item in inventory.values():
156157
if item.domain == "py" and "(" not in item.name:
157158
obj = loader.modules_collection[item.name]
158-
if obj.path not in public_api_paths and not any(path in public_api_paths for path in obj.aliases):
159+
if (
160+
obj.path not in ignore
161+
and obj.path not in public_api_paths
162+
and not any(path in public_api_paths for path in obj.aliases)
163+
):
159164
not_in_api.append(item.name)
160165
msg = "Inventory objects not in public API (try running `make run mkdocs build`):\n{paths}"
161166
assert not not_in_api, msg.format(paths="\n".join(sorted(not_in_api)))
@@ -174,4 +179,4 @@ def _modules(obj: griffe.Module) -> Iterator[griffe.Module]:
174179
yield from _modules(member)
175180

176181
for obj in _modules(internal_api):
177-
assert not obj.docstring
182+
assert not obj.docstring, f"Object {obj.path} has a docstring."

Diff for: ‎tests/test_backlinks.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
from markdown import Markdown
88

9-
from mkdocs_autorefs.backlinks import Backlink, BacklinkCrumb
10-
from mkdocs_autorefs.plugin import AutorefsPlugin
11-
from mkdocs_autorefs.references import AUTOREF_RE, AutorefsExtension, _html_attrs_parser
9+
from mkdocs_autorefs import AUTOREF_RE, AutorefsExtension, AutorefsPlugin, Backlink, BacklinkCrumb
10+
from mkdocs_autorefs._internal.references import _html_attrs_parser
1211
from tests.helpers import create_page
1312

1413

Diff for: ‎tests/test_plugin.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from mkdocs.config.defaults import MkDocsConfig
1010
from mkdocs.theme import Theme
1111

12-
from mkdocs_autorefs.plugin import AutorefsConfig, AutorefsPlugin
13-
from mkdocs_autorefs.references import fix_refs
12+
from mkdocs_autorefs import AutorefsConfig, AutorefsPlugin, fix_refs
1413
from tests.helpers import create_page
1514

1615

Diff for: ‎tests/test_references.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import markdown
99
import pytest
1010

11-
from mkdocs_autorefs.plugin import AutorefsPlugin
12-
from mkdocs_autorefs.references import AutorefsExtension, AutorefsHookInterface, fix_refs, relative_url
11+
from mkdocs_autorefs import AutorefsExtension, AutorefsHookInterface, AutorefsPlugin, fix_refs, relative_url
1312
from tests.helpers import create_page
1413

1514
if TYPE_CHECKING:

0 commit comments

Comments
 (0)
Please sign in to comment.