Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rename AutoRefInlineProcessor to AutorefsInlineProcessor #47

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import re
import warnings
from html import escape, unescape
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Match
from urllib.parse import urlsplit
Expand All @@ -29,6 +30,14 @@
# TODO: remove once support for MkDocs <1.5 is dropped
log = logging.getLogger(f"mkdocs.plugins.{__name__}") # type: ignore[assignment]


def __getattr__(name: str) -> Any:
if name == "AutoRefInlineProcessor":
warnings.warn("AutoRefInlineProcessor was renamed AutorefsInlineProcessor", DeprecationWarning, stacklevel=2)
return AutorefsInlineProcessor
raise AttributeError(f"module 'mkdocs_autorefs.references' has no attribute {name}")


_ATTR_VALUE = r'"[^"<>]+"|[^"<> ]+' # Possibly with double quotes around
AUTO_REF_RE = re.compile(
rf"<span data-(?P<kind>autorefs-(?:identifier|optional|optional-hover))=(?P<identifier>{_ATTR_VALUE})"
Expand All @@ -40,8 +49,8 @@
"""


class AutoRefInlineProcessor(ReferenceInlineProcessor):
"""A Markdown extension."""
class AutorefsInlineProcessor(ReferenceInlineProcessor):
"""A Markdown extension to handle inline references."""

name: str = "mkdocs-autorefs"

Expand Down Expand Up @@ -292,7 +301,13 @@ def flush(self, alias_to: str | None = None) -> None:


class AutorefsExtension(Extension):
"""Extension that inserts auto-references in Markdown."""
"""Markdown extension that transforms unresolved references into auto-references.

Auto-references are then resolved later by the MkDocs plugin.

This extension also scans Markdown anchors (`[](){#some-id}`)
to register them with the MkDocs plugin.
"""

def __init__(
self,
Expand All @@ -311,16 +326,16 @@ def __init__(
def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent method's name)
"""Register the extension.

Add an instance of our [`AutoRefInlineProcessor`][mkdocs_autorefs.references.AutoRefInlineProcessor] to the Markdown parser.
Add an instance of our [`AutorefsInlineProcessor`][mkdocs_autorefs.references.AutorefsInlineProcessor] to the Markdown parser.
Also optionally add an instance of our [`AnchorScannerTreeProcessor`][mkdocs_autorefs.references.AnchorScannerTreeProcessor]
to the Markdown parser if a reference to the autorefs plugin was passed to this extension.

Arguments:
md: A `markdown.Markdown` instance.
"""
md.inlinePatterns.register(
AutoRefInlineProcessor(md),
AutoRefInlineProcessor.name,
AutorefsInlineProcessor(md),
AutorefsInlineProcessor.name,
priority=168, # Right after markdown.inlinepatterns.ReferenceInlineProcessor
)
if self.plugin is not None and self.plugin.scan_toc and "attr_list" in md.treeprocessors:
Expand Down
Loading