references ¤
Cross-references module.
Classes:
-
AnchorScannerTreeProcessor
–Tree processor to scan and register HTML anchors.
-
AutorefsExtension
–Markdown extension that transforms unresolved references into auto-references.
-
AutorefsHookInterface
–An interface for hooking into how AutoRef handles inline references.
-
AutorefsInlineProcessor
–A Markdown extension to handle inline references.
Functions:
-
fix_ref
–Return a
repl
function forre.sub
. -
fix_refs
–Fix all references in the given HTML text.
-
relative_url
–Compute the relative path from URL A to URL B.
Attributes:
-
AUTOREF_RE
–The autoref HTML tag regular expression.
-
AUTO_REF_RE
–Deprecated. Use
AUTOREF_RE
instead.
AUTOREF_RE module-attribute
¤
The autoref HTML tag regular expression.
A regular expression to match mkdocs-autorefs' special reference markers in the on_post_page
hook.
AUTO_REF_RE module-attribute
¤
AUTO_REF_RE = compile(
f"<span data-(?P<kind>autorefs-(?:identifier|optional|optional-hover))=(?P<identifier>{_ATTR_VALUE})(?: class=(?P<class>{_ATTR_VALUE}))?(?P<attrs> [^<>]+)?>(?P<title>.*?)</span>",
flags=DOTALL,
)
Deprecated. Use AUTOREF_RE
instead.
AnchorScannerTreeProcessor ¤
AnchorScannerTreeProcessor(
plugin: AutorefsPlugin, md: Markdown | None = None
)
Bases: Treeprocessor
Tree processor to scan and register HTML anchors.
Parameters:
-
plugin
(AutorefsPlugin
) –A reference to the autorefs plugin, to use its
register_anchor
method.
Source code in src/mkdocs_autorefs/references.py
427 428 429 430 431 432 433 434 |
|
AutorefsExtension ¤
AutorefsExtension(
plugin: AutorefsPlugin | None = None, **kwargs: Any
)
Bases: Extension
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.
Parameters:
-
plugin
(AutorefsPlugin | None
, default:None
) –An optional reference to the autorefs plugin (to pass it to the anchor scanner tree processor).
-
**kwargs
(Any
, default:{}
) –Keyword arguments passed to the base constructor.
Methods:
-
extendMarkdown
–Register the extension.
Source code in src/mkdocs_autorefs/references.py
503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
extendMarkdown ¤
extendMarkdown(md: Markdown) -> None
Register the extension.
Add an instance of our AutorefsInlineProcessor
to the Markdown parser. Also optionally add an instance of our AnchorScannerTreeProcessor
to the Markdown parser if a reference to the autorefs plugin was passed to this extension.
Parameters:
-
md
(Markdown
) –A
markdown.Markdown
instance.
Source code in src/mkdocs_autorefs/references.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
AutorefsHookInterface ¤
Bases: ABC
An interface for hooking into how AutoRef handles inline references.
Classes:
-
Context
–The context around an auto-reference.
Methods:
-
expand_identifier
–Expand an identifier in a given context.
-
get_context
–Get the current context.
Context dataclass
¤
The context around an auto-reference.
Methods:
-
as_dict
–Convert the context to a dictionary of HTML attributes.
as_dict ¤
Convert the context to a dictionary of HTML attributes.
Source code in src/mkdocs_autorefs/references.py
79 80 81 82 83 84 85 86 87 |
|
expand_identifier abstractmethod
¤
Expand an identifier in a given context.
Parameters:
-
identifier
(str
) –The identifier to expand.
Returns:
-
str
–The expanded identifier.
Source code in src/mkdocs_autorefs/references.py
89 90 91 92 93 94 95 96 97 98 99 |
|
get_context abstractmethod
¤
get_context() -> Context
Get the current context.
Returns:
-
Context
–The current context.
Source code in src/mkdocs_autorefs/references.py
101 102 103 104 105 106 107 108 |
|
AutorefsInlineProcessor ¤
Bases: ReferenceInlineProcessor
A Markdown extension to handle inline references.
Methods:
-
evalId
–Evaluate the id portion of
[ref][id]
. -
handleMatch
–Handle an element that matched.
Source code in src/mkdocs_autorefs/references.py
117 118 |
|
evalId ¤
Evaluate the id portion of [ref][id]
.
If [ref][]
use [ref]
.
Parameters:
-
data
(str
) –The data to evaluate.
-
index
(int
) –The starting position.
-
text
(str
) –The text to use when no identifier.
Returns:
-
tuple[str | None, int, bool]
–A tuple containing the identifier, its end position, and whether it matched.
Source code in src/mkdocs_autorefs/references.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
handleMatch ¤
Handle an element that matched.
Parameters:
Returns:
Source code in src/mkdocs_autorefs/references.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
fix_ref ¤
fix_ref(
url_mapper: Callable[[str], str],
unmapped: list[tuple[str, Context | None]],
) -> Callable
Return a repl
function for re.sub
.
In our context, we match Markdown references and replace them with HTML links.
When the matched reference's identifier was not mapped to an URL, we append the identifier to the outer unmapped
list. It generally means the user is trying to cross-reference an object that was not collected and rendered, making it impossible to link to it. We catch this exception in the caller to issue a warning.
Parameters:
-
url_mapper
(Callable[[str], str]
) –A callable that gets an object's site URL by its identifier, such as mkdocs_autorefs.plugin.AutorefsPlugin.get_item_url.
-
unmapped
(list[tuple[str, Context | None]]
) –A list to store unmapped identifiers.
Returns:
-
Callable
–The actual function accepting a
Match
object -
Callable
–and returning the replacement strings.
Source code in src/mkdocs_autorefs/references.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
|
fix_refs ¤
fix_refs(
html: str,
url_mapper: Callable[[str], str],
*,
_legacy_refs: bool = True
) -> tuple[str, list[tuple[str, Context | None]]]
Fix all references in the given HTML text.
Parameters:
-
html
(str
) –The text to fix.
-
url_mapper
(Callable[[str], str]
) –A callable that gets an object's site URL by its identifier, such as mkdocs_autorefs.plugin.AutorefsPlugin.get_item_url.
Returns:
-
tuple[str, list[tuple[str, Context | None]]]
–The fixed HTML, and a list of unmapped identifiers (string and optional context).
Source code in src/mkdocs_autorefs/references.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
relative_url ¤
Compute the relative path from URL A to URL B.
Parameters:
Returns:
-
str
–The relative URL to go from A to B.
Source code in src/mkdocs_autorefs/references.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|