references ¤
Cross-references module.
Classes:
-
AnchorScannerTreeProcessor
–Tree processor to scan and register HTML anchors.
-
AutoRefInlineProcessor
–A Markdown extension.
-
AutorefsExtension
–Extension that inserts auto-references in Markdown.
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:
-
AUTO_REF_RE
–A regular expression to match mkdocs-autorefs' special reference markers
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,
)
A regular expression to match mkdocs-autorefs' special reference markers in the on_post_page
hook.
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
229 230 231 232 233 234 235 236 |
|
AutoRefInlineProcessor ¤
Bases: ReferenceInlineProcessor
A Markdown extension.
Methods:
-
evalId
–Evaluate the id portion of
[ref][id]
. -
handleMatch
–Handle an element that matched.
Source code in src/mkdocs_autorefs/references.py
46 47 |
|
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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
handleMatch ¤
Handle an element that matched.
Parameters:
Returns:
Source code in src/mkdocs_autorefs/references.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
AutorefsExtension ¤
AutorefsExtension(
plugin: AutorefsPlugin | None = None, **kwargs: Any
)
Bases: Extension
Extension that inserts auto-references in Markdown.
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
294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
extendMarkdown ¤
extendMarkdown(md: Markdown) -> None
Register the extension.
Add an instance of our AutoRefInlineProcessor
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
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
fix_ref ¤
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[str]
) –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
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
fix_refs ¤
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:
Source code in src/mkdocs_autorefs/references.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|