Skip to content

Commit

Permalink
fixup! feat: Provide hook interface, use it to expand identifiers and…
Browse files Browse the repository at this point in the history
… attach additional context to references
  • Loading branch information
pawamoy committed May 24, 2024
1 parent 87aca44 commit aad4ed3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from dataclasses import dataclass
from html import escape, unescape
from html.parser import HTMLParser
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Match
from urllib.parse import urlsplit
from xml.etree.ElementTree import Element
Expand All @@ -22,6 +21,8 @@
from markdown.util import HTML_PLACEHOLDER_RE, INLINE_PLACEHOLDER_RE

if TYPE_CHECKING:
from pathlib import Path

from markdown import Markdown

from mkdocs_autorefs.plugin import AutorefsPlugin
Expand Down Expand Up @@ -58,24 +59,27 @@ def __getattr__(name: str) -> Any:
"""


class AutoRefHookInterface(ABC):
class AutorefsHookInterface(ABC):
"""An interface for hooking into how AutoRef handles inline references."""

@dataclass
class Context:
"""The context around an auto-reference."""

domain: str
role: str
origin: str
filepath: str | Path
lineno: int

def as_dict(self) -> dict[str, str]:
"""Convert the context to a dictionary of HTML attributes."""
return {
"data-autorefs-domain": self.domain,
"data-autorefs-role": self.role,
"data-autorefs-origin": self.origin,
"data-autorefs-filepath": str(self.filepath),
"data-autorefs-lineno": str(self.lineno),
"domain": self.domain,
"role": self.role,
"origin": self.origin,
"filepath": str(self.filepath),
"lineno": str(self.lineno),
}

@abstractmethod
Expand All @@ -91,7 +95,7 @@ def expand_identifier(self, identifier: str) -> str:
raise NotImplementedError

@abstractmethod
def get_context(self) -> AutoRefHookInterface.Context:
def get_context(self) -> AutorefsHookInterface.Context:
"""Get the current context.
Returns:
Expand All @@ -104,7 +108,7 @@ class AutorefsInlineProcessor(ReferenceInlineProcessor):
"""A Markdown extension to handle inline references."""

name: str = "mkdocs-autorefs"
hook: AutoRefHookInterface | None = None
hook: AutorefsHookInterface | None = None

def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: D107
super().__init__(REFERENCE_RE, *args, **kwargs)
Expand Down

0 comments on commit aad4ed3

Please sign in to comment.