Skip to content

Commit

Permalink
refactor: Rename AutoRefInlineProcessor to AutorefsInlineProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 24, 2024
1 parent ae87ff7 commit ffcaa01
Showing 1 changed file with 21 additions and 6 deletions.
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

0 comments on commit ffcaa01

Please sign in to comment.