| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Transforms for resolving references. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
__docformat__ = 'reStructuredText' |
| 12 |
|
| 13 |
from docutils import nodes, utils |
| 14 |
from docutils.transforms import Transform |
| 15 |
|
| 16 |
|
| 17 |
class SectionIDs(Transform): |
| 18 |
""" |
| 19 |
Add identifiers to sections. |
| 20 |
|
| 21 |
If the "legacy_ids" configuration setting is False, the rST parser |
| 22 |
does not generate identifiers for implicit targets (e.g. sections) |
| 23 |
in order to give explicit targets preferential access to identifiers |
| 24 |
matching their reference name. |
| 25 |
|
| 26 |
However, the `parts.Contents` transform and most writers |
| 27 |
expect sections to have an identifier, so this transform adds them. |
| 28 |
""" |
| 29 |
default_priority = 240 |
| 30 |
|
| 31 |
def apply(self) -> None: |
| 32 |
if getattr(self.document.settings, "legacy_ids", True): |
| 33 |
return |
| 34 |
for node in self.document.findall(nodes.section): |
| 35 |
self.document.set_id(node) |
| 36 |
|
| 37 |
|
| 38 |
class PropagateTargets(Transform): |
| 39 |
""" |
| 40 |
Propagate empty internal targets to the next element. |
| 41 |
|
| 42 |
Given the following nodes:: |
| 43 |
|
| 44 |
<target ids="internal1" names="internal1"> |
| 45 |
<target anonymous="1" ids="id1"> |
| 46 |
<target ids="internal2" names="internal2"> |
| 47 |
<paragraph> |
| 48 |
This is a test. |
| 49 |
|
| 50 |
`PropagateTargets` propagates the ids and names of the internal |
| 51 |
targets preceding the paragraph to the paragraph itself:: |
| 52 |
|
| 53 |
<target refid="internal1"> |
| 54 |
<target anonymous="1" refid="id1"> |
| 55 |
<target refid="internal2"> |
| 56 |
<paragraph ids="internal2 id1 internal1" names="internal2 internal1"> |
| 57 |
This is a test. |
| 58 |
""" |
| 59 |
|
| 60 |
default_priority = 260 |
| 61 |
|
| 62 |
def apply(self) -> None: |
| 63 |
for target in self.document.findall(nodes.target): |
| 64 |
|
| 65 |
if (isinstance(target.parent, nodes.TextElement) |
| 66 |
or (target.hasattr('refid') or target.hasattr('refuri') |
| 67 |
or target.hasattr('refname'))): |
| 68 |
continue |
| 69 |
assert len(target) == 0, 'error: block-level target has children' |
| 70 |
next_node = target.next_node(ascend=True) |
| 71 |
|
| 72 |
while isinstance(next_node, nodes.system_message): |
| 73 |
next_node = next_node.next_node(ascend=True, descend=False) |
| 74 |
|
| 75 |
|
| 76 |
if (next_node is None |
| 77 |
or isinstance(next_node, (nodes.Invisible, nodes.Targetable)) |
| 78 |
and not isinstance(next_node, nodes.target)): |
| 79 |
continue |
| 80 |
next_node['ids'].extend(target['ids']) |
| 81 |
next_node['names'].extend(target['names']) |
| 82 |
|
| 83 |
if not hasattr(next_node, 'expect_referenced_by_name'): |
| 84 |
next_node.expect_referenced_by_name = {} |
| 85 |
if not hasattr(next_node, 'expect_referenced_by_id'): |
| 86 |
next_node.expect_referenced_by_id = {} |
| 87 |
for id in target['ids']: |
| 88 |
|
| 89 |
self.document.ids[id] = next_node |
| 90 |
|
| 91 |
|
| 92 |
next_node.expect_referenced_by_id[id] = target |
| 93 |
for name in target['names']: |
| 94 |
next_node.expect_referenced_by_name[name] = target |
| 95 |
|
| 96 |
|
| 97 |
next_node.expect_referenced_by_name.update( |
| 98 |
getattr(target, 'expect_referenced_by_name', {})) |
| 99 |
next_node.expect_referenced_by_id.update( |
| 100 |
getattr(target, 'expect_referenced_by_id', {})) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if isinstance(target.parent, nodes.figure) and isinstance( |
| 107 |
next_node, nodes.caption): |
| 108 |
target.parent.remove(target) |
| 109 |
continue |
| 110 |
|
| 111 |
|
| 112 |
target['refid'] = target['ids'][0] |
| 113 |
|
| 114 |
target['ids'] = [] |
| 115 |
target['names'] = [] |
| 116 |
self.document.note_refid(target) |
| 117 |
|
| 118 |
|
| 119 |
class AnonymousHyperlinks(Transform): |
| 120 |
|
| 121 |
""" |
| 122 |
Link anonymous references to targets. Given:: |
| 123 |
|
| 124 |
<paragraph> |
| 125 |
<reference anonymous="1"> |
| 126 |
internal |
| 127 |
<reference anonymous="1"> |
| 128 |
external |
| 129 |
<target anonymous="1" ids="id1"> |
| 130 |
<target anonymous="1" ids="id2" refuri="http://external"> |
| 131 |
|
| 132 |
Corresponding references are linked via "refid" or resolved via "refuri":: |
| 133 |
|
| 134 |
<paragraph> |
| 135 |
<reference anonymous="1" refid="id1"> |
| 136 |
text |
| 137 |
<reference anonymous="1" refuri="http://external"> |
| 138 |
external |
| 139 |
<target anonymous="1" ids="id1"> |
| 140 |
<target anonymous="1" ids="id2" refuri="http://external"> |
| 141 |
""" |
| 142 |
|
| 143 |
default_priority = 440 |
| 144 |
|
| 145 |
def apply(self) -> None: |
| 146 |
anonymous_refs = [ |
| 147 |
node for node in self.document.findall(nodes.reference) |
| 148 |
if node.get('anonymous')] |
| 149 |
anonymous_targets = [ |
| 150 |
node for node in self.document.findall(nodes.target) |
| 151 |
if node.get('anonymous')] |
| 152 |
if len(anonymous_refs) != len(anonymous_targets): |
| 153 |
msg = self.document.reporter.error( |
| 154 |
'Anonymous hyperlink mismatch: %s references but %s ' |
| 155 |
'targets.\nSee "backrefs" attribute for IDs.' |
| 156 |
% (len(anonymous_refs), len(anonymous_targets))) |
| 157 |
msgid = self.document.set_id(msg) |
| 158 |
for ref in anonymous_refs: |
| 159 |
prb = nodes.problematic( |
| 160 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 161 |
prbid = self.document.set_id(prb) |
| 162 |
msg.add_backref(prbid) |
| 163 |
ref.replace_self(prb) |
| 164 |
return |
| 165 |
for ref, target in zip(anonymous_refs, anonymous_targets): |
| 166 |
if ref.hasattr('refid') or ref.hasattr('refuri'): |
| 167 |
continue |
| 168 |
target.referenced = True |
| 169 |
while True: |
| 170 |
if target.hasattr('refuri'): |
| 171 |
ref['refuri'] = target['refuri'] |
| 172 |
ref.resolved = True |
| 173 |
break |
| 174 |
else: |
| 175 |
if not target['ids']: |
| 176 |
|
| 177 |
target = self.document.ids[target['refid']] |
| 178 |
continue |
| 179 |
ref['refid'] = target['ids'][0] |
| 180 |
self.document.note_refid(ref) |
| 181 |
break |
| 182 |
|
| 183 |
|
| 184 |
class IndirectHyperlinks(Transform): |
| 185 |
|
| 186 |
""" |
| 187 |
a) Indirect external references:: |
| 188 |
|
| 189 |
<paragraph> |
| 190 |
<reference refname="indirect-external"> |
| 191 |
indirect external |
| 192 |
<target ids="id1" names="direct-external" |
| 193 |
refuri="http://indirect"> |
| 194 |
<target ids="id2" names="indirect-external" |
| 195 |
refname="direct-external"> |
| 196 |
|
| 197 |
The "refuri" attribute is migrated back to all indirect targets |
| 198 |
from the final direct target (i.e. a target not referring to |
| 199 |
another indirect target):: |
| 200 |
|
| 201 |
<paragraph> |
| 202 |
<reference refname="indirect-external"> |
| 203 |
indirect external |
| 204 |
<target ids="id1" names="direct-external" |
| 205 |
refuri="http://indirect"> |
| 206 |
<target ids="id2" names="indirect-external" |
| 207 |
refuri="http://indirect"> |
| 208 |
|
| 209 |
Once the attribute is migrated, the preexisting "refname" attribute |
| 210 |
is dropped. |
| 211 |
|
| 212 |
b) Indirect internal references:: |
| 213 |
|
| 214 |
<target ids="id1" names="final-target"> |
| 215 |
<paragraph> |
| 216 |
<reference refname="indirect-internal"> |
| 217 |
indirect internal |
| 218 |
<target ids="id2" names="indirect-internal-2" |
| 219 |
refname="final target"> |
| 220 |
<target ids="id3" names="indirect-internal" |
| 221 |
refname="indirect-internal-2"> |
| 222 |
|
| 223 |
Targets which indirectly refer to an internal target become one-hop |
| 224 |
indirect (their "refid" attributes are directly set to the internal |
| 225 |
target's "id"). References which indirectly refer to an internal |
| 226 |
target become direct internal references:: |
| 227 |
|
| 228 |
<target ids="id1" names="final-target"> |
| 229 |
<paragraph> |
| 230 |
<reference refid="id1"> |
| 231 |
indirect internal |
| 232 |
<target ids="id2" names="indirect-internal-2" refid="id1"> |
| 233 |
<target ids="id3" names="indirect-internal" refid="id1"> |
| 234 |
""" |
| 235 |
|
| 236 |
default_priority = 460 |
| 237 |
|
| 238 |
def apply(self) -> None: |
| 239 |
for target in self.document.indirect_targets: |
| 240 |
if not target.resolved: |
| 241 |
self.resolve_indirect_target(target) |
| 242 |
self.resolve_indirect_references(target) |
| 243 |
|
| 244 |
def resolve_indirect_target(self, target) -> None: |
| 245 |
|
| 246 |
refname = target.get('refname') |
| 247 |
refid = target.get('refid') |
| 248 |
if refid: |
| 249 |
reftarget = self.document.ids.get(refid) |
| 250 |
else: |
| 251 |
reftarget = self.document.names.get(refname) |
| 252 |
refid = self.document.nameids.get(refname) |
| 253 |
if reftarget and not refid: |
| 254 |
refid = self.document.set_id(reftarget) |
| 255 |
if not reftarget: |
| 256 |
|
| 257 |
for resolver_function in \ |
| 258 |
self.document.transformer.unknown_reference_resolvers: |
| 259 |
if resolver_function(target): |
| 260 |
break |
| 261 |
else: |
| 262 |
self.nonexistent_indirect_target(target) |
| 263 |
return |
| 264 |
reftarget.note_referenced_by(id=refid) |
| 265 |
if (isinstance(reftarget, nodes.target) |
| 266 |
and not reftarget.resolved |
| 267 |
and reftarget.hasattr('refname')): |
| 268 |
if hasattr(target, 'multiply_indirect'): |
| 269 |
self.circular_indirect_reference(target) |
| 270 |
return |
| 271 |
target.multiply_indirect = 1 |
| 272 |
self.resolve_indirect_target(reftarget) |
| 273 |
del target.multiply_indirect |
| 274 |
if reftarget.hasattr('refuri'): |
| 275 |
target['refuri'] = reftarget['refuri'] |
| 276 |
if 'refid' in target: |
| 277 |
del target['refid'] |
| 278 |
elif reftarget.hasattr('refid'): |
| 279 |
target['refid'] = reftarget['refid'] |
| 280 |
self.document.note_refid(target) |
| 281 |
else: |
| 282 |
if reftarget['ids']: |
| 283 |
target['refid'] = refid |
| 284 |
self.document.note_refid(target) |
| 285 |
else: |
| 286 |
self.nonexistent_indirect_target(target) |
| 287 |
return |
| 288 |
if refname is not None: |
| 289 |
del target['refname'] |
| 290 |
target.resolved = True |
| 291 |
|
| 292 |
def nonexistent_indirect_target(self, target) -> None: |
| 293 |
if self.document.names.get(target['refname'], '') is None: |
| 294 |
self.indirect_target_error(target, 'which is a duplicate, and ' |
| 295 |
'cannot be used as a unique reference') |
| 296 |
else: |
| 297 |
self.indirect_target_error(target, 'which does not exist') |
| 298 |
|
| 299 |
def circular_indirect_reference(self, target) -> None: |
| 300 |
self.indirect_target_error(target, 'forming a circular reference') |
| 301 |
|
| 302 |
def indirect_target_error(self, target, explanation) -> None: |
| 303 |
naming = '' |
| 304 |
reflist = [] |
| 305 |
if target['names']: |
| 306 |
naming = '"%s" ' % target['names'][0] |
| 307 |
for name in target['names']: |
| 308 |
reflist.extend(self.document.refnames.get(name, [])) |
| 309 |
for id in target['ids']: |
| 310 |
reflist.extend(self.document.refids.get(id, [])) |
| 311 |
if target['ids']: |
| 312 |
naming += '(id="%s")' % target['ids'][0] |
| 313 |
msg = self.document.reporter.error( |
| 314 |
'Indirect hyperlink target %s refers to target "%s", %s.' |
| 315 |
% (naming, target['refname'], explanation), base_node=target) |
| 316 |
msgid = self.document.set_id(msg) |
| 317 |
for ref in utils.uniq(reflist): |
| 318 |
prb = nodes.problematic( |
| 319 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 320 |
prbid = self.document.set_id(prb) |
| 321 |
msg.add_backref(prbid) |
| 322 |
ref.replace_self(prb) |
| 323 |
target.resolved = True |
| 324 |
|
| 325 |
def resolve_indirect_references(self, target) -> None: |
| 326 |
if target.hasattr('refid'): |
| 327 |
attname = 'refid' |
| 328 |
call_method = self.document.note_refid |
| 329 |
elif target.hasattr('refuri'): |
| 330 |
attname = 'refuri' |
| 331 |
call_method = None |
| 332 |
else: |
| 333 |
return |
| 334 |
attval = target[attname] |
| 335 |
for name in target['names']: |
| 336 |
reflist = self.document.refnames.get(name, []) |
| 337 |
if reflist: |
| 338 |
target.note_referenced_by(name=name) |
| 339 |
for ref in reflist: |
| 340 |
if ref.resolved: |
| 341 |
continue |
| 342 |
del ref['refname'] |
| 343 |
ref[attname] = attval |
| 344 |
if call_method: |
| 345 |
call_method(ref) |
| 346 |
ref.resolved = True |
| 347 |
if isinstance(ref, nodes.target): |
| 348 |
self.resolve_indirect_references(ref) |
| 349 |
for id in target['ids']: |
| 350 |
reflist = self.document.refids.get(id, []) |
| 351 |
if reflist: |
| 352 |
target.note_referenced_by(id=id) |
| 353 |
for ref in reflist: |
| 354 |
if ref.resolved: |
| 355 |
continue |
| 356 |
del ref['refid'] |
| 357 |
ref[attname] = attval |
| 358 |
if call_method: |
| 359 |
call_method(ref) |
| 360 |
ref.resolved = True |
| 361 |
if isinstance(ref, nodes.target): |
| 362 |
self.resolve_indirect_references(ref) |
| 363 |
|
| 364 |
|
| 365 |
class ExternalTargets(Transform): |
| 366 |
|
| 367 |
""" |
| 368 |
Given:: |
| 369 |
|
| 370 |
<paragraph> |
| 371 |
<reference refname="direct-external"> |
| 372 |
direct external |
| 373 |
<target ids="id1" names="direct-external" refuri="http://direct"> |
| 374 |
|
| 375 |
The "refname" attribute is replaced by the direct "refuri" attribute:: |
| 376 |
|
| 377 |
<paragraph> |
| 378 |
<reference refuri="http://direct"> |
| 379 |
direct external |
| 380 |
<target ids="id1" names="direct-external" refuri="http://direct"> |
| 381 |
""" |
| 382 |
|
| 383 |
default_priority = 640 |
| 384 |
|
| 385 |
def apply(self) -> None: |
| 386 |
for target in self.document.findall(nodes.target): |
| 387 |
if target.hasattr('refuri'): |
| 388 |
refuri = target['refuri'] |
| 389 |
for name in target['names']: |
| 390 |
reflist = self.document.refnames.get(name, []) |
| 391 |
if reflist: |
| 392 |
target.note_referenced_by(name=name) |
| 393 |
for ref in reflist: |
| 394 |
if ref.resolved: |
| 395 |
continue |
| 396 |
del ref['refname'] |
| 397 |
ref['refuri'] = refuri |
| 398 |
ref.resolved = True |
| 399 |
|
| 400 |
|
| 401 |
class InternalTargets(Transform): |
| 402 |
|
| 403 |
default_priority = 660 |
| 404 |
|
| 405 |
def apply(self) -> None: |
| 406 |
for target in self.document.findall(nodes.target): |
| 407 |
if not target.hasattr('refuri') and not target.hasattr('refid'): |
| 408 |
self.resolve_reference_ids(target) |
| 409 |
|
| 410 |
def resolve_reference_ids(self, target) -> None: |
| 411 |
""" |
| 412 |
Given:: |
| 413 |
|
| 414 |
<paragraph> |
| 415 |
<reference refname="direct-internal"> |
| 416 |
direct internal |
| 417 |
<target ids="id1" names="direct-internal"> |
| 418 |
|
| 419 |
The "refname" attribute is replaced by "refid" linking to the target's |
| 420 |
"id":: |
| 421 |
|
| 422 |
<paragraph> |
| 423 |
<reference refid="id1"> |
| 424 |
direct internal |
| 425 |
<target ids="id1" names="direct-internal"> |
| 426 |
""" |
| 427 |
for name in target['names']: |
| 428 |
refid = self.document.nameids.get(name) |
| 429 |
reflist = self.document.refnames.get(name, []) |
| 430 |
if reflist: |
| 431 |
target.note_referenced_by(name=name) |
| 432 |
for ref in reflist: |
| 433 |
if ref.resolved: |
| 434 |
continue |
| 435 |
if refid: |
| 436 |
del ref['refname'] |
| 437 |
ref['refid'] = refid |
| 438 |
ref.resolved = True |
| 439 |
|
| 440 |
|
| 441 |
class Footnotes(Transform): |
| 442 |
|
| 443 |
""" |
| 444 |
Assign numbers to autonumbered footnotes, and resolve links to footnotes, |
| 445 |
citations, and their references. |
| 446 |
|
| 447 |
Given the following ``document`` as input:: |
| 448 |
|
| 449 |
<document> |
| 450 |
<paragraph> |
| 451 |
A labeled autonumbered footnote reference: |
| 452 |
<footnote_reference auto="1" ids="id1" refname="footnote"> |
| 453 |
<paragraph> |
| 454 |
An unlabeled autonumbered footnote reference: |
| 455 |
<footnote_reference auto="1" ids="id2"> |
| 456 |
<footnote auto="1" ids="id3"> |
| 457 |
<paragraph> |
| 458 |
Unlabeled autonumbered footnote. |
| 459 |
<footnote auto="1" ids="footnote" names="footnote"> |
| 460 |
<paragraph> |
| 461 |
Labeled autonumbered footnote. |
| 462 |
|
| 463 |
Auto-numbered footnotes have attribute ``auto="1"`` and no label. |
| 464 |
Auto-numbered footnote_references have no reference text (they're |
| 465 |
empty elements). When resolving the numbering, a ``label`` element |
| 466 |
is added to the beginning of the ``footnote``, and reference text |
| 467 |
to the ``footnote_reference``. |
| 468 |
|
| 469 |
The transformed result will be:: |
| 470 |
|
| 471 |
<document> |
| 472 |
<paragraph> |
| 473 |
A labeled autonumbered footnote reference: |
| 474 |
<footnote_reference auto="1" ids="id1" refid="footnote"> |
| 475 |
2 |
| 476 |
<paragraph> |
| 477 |
An unlabeled autonumbered footnote reference: |
| 478 |
<footnote_reference auto="1" ids="id2" refid="id3"> |
| 479 |
1 |
| 480 |
<footnote auto="1" ids="id3" backrefs="id2"> |
| 481 |
<label> |
| 482 |
1 |
| 483 |
<paragraph> |
| 484 |
Unlabeled autonumbered footnote. |
| 485 |
<footnote auto="1" ids="footnote" names="footnote" backrefs="id1"> |
| 486 |
<label> |
| 487 |
2 |
| 488 |
<paragraph> |
| 489 |
Labeled autonumbered footnote. |
| 490 |
|
| 491 |
Note that the footnotes are not in the same order as the references. |
| 492 |
|
| 493 |
The labels and reference text are added to the auto-numbered ``footnote`` |
| 494 |
and ``footnote_reference`` elements. Footnote elements are backlinked to |
| 495 |
their references via "refids" attributes. References are assigned "id" |
| 496 |
and "refid" attributes. |
| 497 |
|
| 498 |
After adding labels and reference text, the "auto" attributes can be |
| 499 |
ignored. |
| 500 |
""" |
| 501 |
|
| 502 |
default_priority = 620 |
| 503 |
|
| 504 |
autofootnote_labels = None |
| 505 |
"""Keep track of unlabeled autonumbered footnotes.""" |
| 506 |
|
| 507 |
symbols = [ |
| 508 |
|
| 509 |
|
| 510 |
'*', |
| 511 |
'\u2020', er |
| 512 |
'\u2021', er |
| 513 |
'\u00A7', k |
| 514 |
'\u00B6', ) |
| 515 |
|
| 516 |
'#', |
| 517 |
|
| 518 |
'\u2660', it |
| 519 |
'\u2665', it |
| 520 |
'\u2666', it |
| 521 |
'\u2663', it |
| 522 |
] |
| 523 |
|
| 524 |
def apply(self) -> None: |
| 525 |
self.autofootnote_labels = [] |
| 526 |
startnum = self.document.autofootnote_start |
| 527 |
self.document.autofootnote_start = self.number_footnotes(startnum) |
| 528 |
self.number_footnote_references(startnum) |
| 529 |
self.symbolize_footnotes() |
| 530 |
self.resolve_footnotes_and_citations() |
| 531 |
|
| 532 |
def number_footnotes(self, startnum): |
| 533 |
""" |
| 534 |
Assign numbers to autonumbered footnotes. |
| 535 |
|
| 536 |
For labeled autonumbered footnotes, copy the number over to |
| 537 |
corresponding footnote references. |
| 538 |
""" |
| 539 |
for footnote in self.document.autofootnotes: |
| 540 |
while True: |
| 541 |
label = str(startnum) |
| 542 |
startnum += 1 |
| 543 |
if label not in self.document.nameids: |
| 544 |
break |
| 545 |
footnote.insert(0, nodes.label('', label)) |
| 546 |
for name in footnote['names']: |
| 547 |
for ref in self.document.footnote_refs.get(name, []): |
| 548 |
ref += nodes.Text(label) |
| 549 |
ref.delattr('refname') |
| 550 |
assert len(footnote['ids']) == len(ref['ids']) == 1 |
| 551 |
ref['refid'] = footnote['ids'][0] |
| 552 |
footnote.add_backref(ref['ids'][0]) |
| 553 |
self.document.note_refid(ref) |
| 554 |
ref.resolved = True |
| 555 |
if not footnote['names'] and not footnote['dupnames']: |
| 556 |
footnote['names'].append(label) |
| 557 |
self.document.note_explicit_target(footnote, footnote) |
| 558 |
self.autofootnote_labels.append(label) |
| 559 |
return startnum |
| 560 |
|
| 561 |
def number_footnote_references(self, startnum) -> None: |
| 562 |
"""Assign numbers to autonumbered footnote references.""" |
| 563 |
i = 0 |
| 564 |
for ref in self.document.autofootnote_refs: |
| 565 |
if ref.resolved or ref.hasattr('refid'): |
| 566 |
continue |
| 567 |
try: |
| 568 |
label = self.autofootnote_labels[i] |
| 569 |
except IndexError: |
| 570 |
n = len(self.autofootnote_labels) |
| 571 |
s = 's' if n > 1 else '' |
| 572 |
msg = self.document.reporter.error( |
| 573 |
'Too many autonumbered footnote references: ' |
| 574 |
f'only {n} corresponding footnote{s} available.', |
| 575 |
base_node=ref) |
| 576 |
msgid = self.document.set_id(msg) |
| 577 |
for ref in self.document.autofootnote_refs[i:]: |
| 578 |
if ref.resolved or ref.hasattr('refname'): |
| 579 |
continue |
| 580 |
prb = nodes.problematic( |
| 581 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 582 |
prbid = self.document.set_id(prb) |
| 583 |
msg.add_backref(prbid) |
| 584 |
ref.replace_self(prb) |
| 585 |
break |
| 586 |
ref += nodes.Text(label) |
| 587 |
id = self.document.nameids[label] |
| 588 |
footnote = self.document.ids[id] |
| 589 |
ref['refid'] = id |
| 590 |
self.document.note_refid(ref) |
| 591 |
assert len(ref['ids']) == 1 |
| 592 |
footnote.add_backref(ref['ids'][0]) |
| 593 |
ref.resolved = True |
| 594 |
i += 1 |
| 595 |
|
| 596 |
def symbolize_footnotes(self) -> None: |
| 597 |
"""Add symbols indexes to "[*]"-style footnotes and references.""" |
| 598 |
labels = [] |
| 599 |
for footnote in self.document.symbol_footnotes: |
| 600 |
reps, index = divmod(self.document.symbol_footnote_start, |
| 601 |
len(self.symbols)) |
| 602 |
labeltext = self.symbols[index] * (reps + 1) |
| 603 |
labels.append(labeltext) |
| 604 |
footnote.insert(0, nodes.label('', labeltext)) |
| 605 |
self.document.symbol_footnote_start += 1 |
| 606 |
self.document.set_id(footnote) |
| 607 |
i = 0 |
| 608 |
for ref in self.document.symbol_footnote_refs: |
| 609 |
try: |
| 610 |
ref += nodes.Text(labels[i]) |
| 611 |
except IndexError: |
| 612 |
msg = self.document.reporter.error( |
| 613 |
'Too many symbol footnote references: only %s ' |
| 614 |
'corresponding footnotes available.' % len(labels), |
| 615 |
base_node=ref) |
| 616 |
msgid = self.document.set_id(msg) |
| 617 |
for ref in self.document.symbol_footnote_refs[i:]: |
| 618 |
if ref.resolved or ref.hasattr('refid'): |
| 619 |
continue |
| 620 |
prb = nodes.problematic( |
| 621 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 622 |
prbid = self.document.set_id(prb) |
| 623 |
msg.add_backref(prbid) |
| 624 |
ref.replace_self(prb) |
| 625 |
break |
| 626 |
footnote = self.document.symbol_footnotes[i] |
| 627 |
assert len(footnote['ids']) == 1 |
| 628 |
ref['refid'] = footnote['ids'][0] |
| 629 |
self.document.note_refid(ref) |
| 630 |
footnote.add_backref(ref['ids'][0]) |
| 631 |
i += 1 |
| 632 |
|
| 633 |
def resolve_footnotes_and_citations(self) -> None: |
| 634 |
""" |
| 635 |
Link manually-labeled footnotes and citations to/from their |
| 636 |
references. |
| 637 |
""" |
| 638 |
for footnote in self.document.footnotes: |
| 639 |
for label in footnote['names']: |
| 640 |
if label in self.document.footnote_refs: |
| 641 |
reflist = self.document.footnote_refs[label] |
| 642 |
self.resolve_references(footnote, reflist) |
| 643 |
for citation in self.document.citations: |
| 644 |
for label in citation['names']: |
| 645 |
if label in self.document.citation_refs: |
| 646 |
reflist = self.document.citation_refs[label] |
| 647 |
self.resolve_references(citation, reflist) |
| 648 |
|
| 649 |
def resolve_references(self, note, reflist) -> None: |
| 650 |
assert len(note['ids']) == 1 |
| 651 |
id = note['ids'][0] |
| 652 |
for ref in reflist: |
| 653 |
if ref.resolved: |
| 654 |
continue |
| 655 |
ref.delattr('refname') |
| 656 |
ref['refid'] = id |
| 657 |
assert len(ref['ids']) == 1 |
| 658 |
note.add_backref(ref['ids'][0]) |
| 659 |
ref.resolved = True |
| 660 |
note.resolved = True |
| 661 |
|
| 662 |
|
| 663 |
class CircularSubstitutionDefinitionError(Exception): |
| 664 |
pass |
| 665 |
|
| 666 |
|
| 667 |
class Substitutions(Transform): |
| 668 |
|
| 669 |
""" |
| 670 |
Given the following ``document`` as input:: |
| 671 |
|
| 672 |
<document> |
| 673 |
<paragraph> |
| 674 |
The |
| 675 |
<substitution_reference refname="biohazard"> |
| 676 |
biohazard |
| 677 |
symbol is deservedly scary-looking. |
| 678 |
<substitution_definition names="biohazard"> |
| 679 |
<image alt="biohazard" uri="biohazard.png"> |
| 680 |
|
| 681 |
The ``substitution_reference`` will simply be replaced by the |
| 682 |
contents of the corresponding ``substitution_definition``. |
| 683 |
|
| 684 |
The transformed result will be:: |
| 685 |
|
| 686 |
<document> |
| 687 |
<paragraph> |
| 688 |
The |
| 689 |
<image alt="biohazard" uri="biohazard.png"> |
| 690 |
symbol is deservedly scary-looking. |
| 691 |
<substitution_definition names="biohazard"> |
| 692 |
<image alt="biohazard" uri="biohazard.png"> |
| 693 |
""" |
| 694 |
|
| 695 |
default_priority = 220 |
| 696 |
"""The Substitutions transform has to be applied very early, before |
| 697 |
`docutils.transforms.frontmatter.DocTitle` and others.""" |
| 698 |
|
| 699 |
def apply(self): |
| 700 |
defs = self.document.substitution_defs |
| 701 |
normed = self.document.substitution_names |
| 702 |
nested = {} |
| 703 |
line_length_limit = getattr(self.document.settings, |
| 704 |
"line_length_limit", 10000) |
| 705 |
|
| 706 |
subreflist = list(self.document.findall(nodes.substitution_reference)) |
| 707 |
for ref in subreflist: |
| 708 |
msg = '' |
| 709 |
refname = ref['refname'] |
| 710 |
if refname in defs: |
| 711 |
key = refname |
| 712 |
else: |
| 713 |
normed_name = refname.lower() |
| 714 |
key = normed.get(normed_name, None) |
| 715 |
if key is None: |
| 716 |
msg = self.document.reporter.error( |
| 717 |
'Undefined substitution referenced: "%s".' |
| 718 |
% refname, base_node=ref) |
| 719 |
else: |
| 720 |
subdef = defs[key] |
| 721 |
if len(subdef.astext()) > line_length_limit: |
| 722 |
msg = self.document.reporter.error( |
| 723 |
'Substitution definition "%s" exceeds the' |
| 724 |
' line-length-limit.' % key) |
| 725 |
if msg: |
| 726 |
msgid = self.document.set_id(msg) |
| 727 |
prb = nodes.problematic( |
| 728 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 729 |
prbid = self.document.set_id(prb) |
| 730 |
msg.add_backref(prbid) |
| 731 |
ref.replace_self(prb) |
| 732 |
continue |
| 733 |
|
| 734 |
parent = ref.parent |
| 735 |
index = parent.index(ref) |
| 736 |
if ('ltrim' in subdef.attributes |
| 737 |
or 'trim' in subdef.attributes): |
| 738 |
if index > 0 and isinstance(parent[index - 1], |
| 739 |
nodes.Text): |
| 740 |
parent[index - 1] = parent[index - 1].rstrip() |
| 741 |
if ('rtrim' in subdef.attributes |
| 742 |
or 'trim' in subdef.attributes): |
| 743 |
if (len(parent) > index + 1 |
| 744 |
and isinstance(parent[index + 1], nodes.Text)): |
| 745 |
parent[index + 1] = parent[index + 1].lstrip() |
| 746 |
subdef_copy = subdef.deepcopy() |
| 747 |
try: |
| 748 |
|
| 749 |
for nested_ref in subdef_copy.findall( |
| 750 |
nodes.substitution_reference): |
| 751 |
nested_name = normed[nested_ref['refname'].lower()] |
| 752 |
if nested_name in nested.setdefault(nested_name, []): |
| 753 |
raise CircularSubstitutionDefinitionError |
| 754 |
nested[nested_name].append(key) |
| 755 |
nested_ref['ref-origin'] = ref |
| 756 |
subreflist.append(nested_ref) |
| 757 |
except CircularSubstitutionDefinitionError: |
| 758 |
parent = ref.parent |
| 759 |
if isinstance(parent, nodes.substitution_definition): |
| 760 |
msg = self.document.reporter.error( |
| 761 |
'Circular substitution definition detected:', |
| 762 |
nodes.literal_block(parent.rawsource, |
| 763 |
parent.rawsource), |
| 764 |
line=parent.line, base_node=parent) |
| 765 |
parent.replace_self(msg) |
| 766 |
else: |
| 767 |
|
| 768 |
ref_origin = ref |
| 769 |
while ref_origin.hasattr('ref-origin'): |
| 770 |
ref_origin = ref_origin['ref-origin'] |
| 771 |
msg = self.document.reporter.error( |
| 772 |
'Circular substitution definition referenced: ' |
| 773 |
'"%s".' % refname, base_node=ref_origin) |
| 774 |
msgid = self.document.set_id(msg) |
| 775 |
prb = nodes.problematic( |
| 776 |
ref.rawsource, ref.rawsource, refid=msgid) |
| 777 |
prbid = self.document.set_id(prb) |
| 778 |
msg.add_backref(prbid) |
| 779 |
ref.replace_self(prb) |
| 780 |
continue |
| 781 |
ref.replace_self(subdef_copy.children) |
| 782 |
|
| 783 |
|
| 784 |
for node in subdef_copy.children: |
| 785 |
if isinstance(node, nodes.Referential): |
| 786 |
|
| 787 |
|
| 788 |
if 'refname' in node: |
| 789 |
self.document.note_refname(node) |
| 790 |
|
| 791 |
|
| 792 |
class TargetNotes(Transform): |
| 793 |
|
| 794 |
""" |
| 795 |
Creates a footnote for each external target in the text, and corresponding |
| 796 |
footnote references after each reference. |
| 797 |
""" |
| 798 |
|
| 799 |
default_priority = 540 |
| 800 |
"""The TargetNotes transform has to be applied after `IndirectHyperlinks` |
| 801 |
but before `Footnotes`.""" |
| 802 |
|
| 803 |
def __init__(self, document, startnode) -> None: |
| 804 |
Transform.__init__(self, document, startnode=startnode) |
| 805 |
|
| 806 |
self.classes = startnode.details.get('class', []) |
| 807 |
|
| 808 |
def apply(self) -> None: |
| 809 |
notes = {} |
| 810 |
nodelist = [] |
| 811 |
for target in self.document.findall(nodes.target): |
| 812 |
|
| 813 |
if not target.hasattr('refuri'): |
| 814 |
continue |
| 815 |
names = target['names'] |
| 816 |
refs = [] |
| 817 |
for name in names: |
| 818 |
refs.extend(self.document.refnames.get(name, [])) |
| 819 |
if not refs: |
| 820 |
continue |
| 821 |
footnote = self.make_target_footnote(target['refuri'], refs, |
| 822 |
notes) |
| 823 |
if target['refuri'] not in notes: |
| 824 |
notes[target['refuri']] = footnote |
| 825 |
nodelist.append(footnote) |
| 826 |
|
| 827 |
for ref in self.document.findall(nodes.reference): |
| 828 |
if not ref.get('anonymous'): |
| 829 |
continue |
| 830 |
if ref.hasattr('refuri'): |
| 831 |
footnote = self.make_target_footnote(ref['refuri'], [ref], |
| 832 |
notes) |
| 833 |
if ref['refuri'] not in notes: |
| 834 |
notes[ref['refuri']] = footnote |
| 835 |
nodelist.append(footnote) |
| 836 |
self.startnode.replace_self(nodelist) |
| 837 |
|
| 838 |
def make_target_footnote(self, refuri, refs, notes): |
| 839 |
if refuri in notes: |
| 840 |
footnote = notes[refuri] |
| 841 |
assert len(footnote['names']) == 1 |
| 842 |
footnote_name = footnote['names'][0] |
| 843 |
else: |
| 844 |
footnote = nodes.footnote() |
| 845 |
footnote_id = self.document.set_id(footnote) |
| 846 |
|
| 847 |
|
| 848 |
footnote_name = 'TARGET_NOTE: ' + footnote_id |
| 849 |
footnote['auto'] = 1 |
| 850 |
footnote['names'] = [footnote_name] |
| 851 |
footnote_paragraph = nodes.paragraph() |
| 852 |
footnote_paragraph += nodes.reference('', refuri, refuri=refuri) |
| 853 |
footnote += footnote_paragraph |
| 854 |
self.document.note_autofootnote(footnote) |
| 855 |
self.document.note_explicit_target(footnote, footnote) |
| 856 |
for ref in refs: |
| 857 |
if isinstance(ref, nodes.target): |
| 858 |
continue |
| 859 |
refnode = nodes.footnote_reference(refname=footnote_name, auto=1) |
| 860 |
refnode['classes'] += self.classes |
| 861 |
self.document.note_autofootnote_ref(refnode) |
| 862 |
self.document.note_footnote_ref(refnode) |
| 863 |
index = ref.parent.index(ref) + 1 |
| 864 |
reflist = [refnode] |
| 865 |
if not utils.get_trim_footnote_ref_space(self.document.settings): |
| 866 |
if self.classes: |
| 867 |
reflist.insert( |
| 868 |
0, nodes.inline(text=' ', Classes=self.classes)) |
| 869 |
else: |
| 870 |
reflist.insert(0, nodes.Text(' ')) |
| 871 |
ref.parent.insert(index, reflist) |
| 872 |
return footnote |
| 873 |
|
| 874 |
|
| 875 |
class CitationReferences(Transform): |
| 876 |
"""Resolve <citation_references>. |
| 877 |
|
| 878 |
The 'use_bibtex'__ configuration setting indicates that citation entries |
| 879 |
are fetched from a BibTeX database by the backend (LaTeX). |
| 880 |
|
| 881 |
__ https://docutils.sourceforge.io/docs/user/config.html#use-bibtex |
| 882 |
""" |
| 883 |
|
| 884 |
|
| 885 |
default_priority = 770 |
| 886 |
|
| 887 |
|
| 888 |
def apply(self) -> None: |
| 889 |
if not getattr(self.document.settings, 'use_bibtex', []): |
| 890 |
return |
| 891 |
for node in self.document.findall(nodes.citation_reference): |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
if node.resolved or self.document.nameids.get(node.get('refname')): |
| 897 |
continue |
| 898 |
if node.astext(): |
| 899 |
node.delattr('refname') |
| 900 |
node.resolved = True |
| 901 |
|
| 902 |
|
| 903 |
class DanglingReferences(Transform): |
| 904 |
|
| 905 |
""" |
| 906 |
Check for dangling references (incl. footnote & citation) and for |
| 907 |
unreferenced targets. |
| 908 |
|
| 909 |
Provisional : pending deprecation |
| 910 |
Docutils readers will add separate transforms for resolving |
| 911 |
refnames to refids and for reporting unresolved references |
| 912 |
instead of this transform (to make space for reference-resolving |
| 913 |
transforms added by extensions or applications) in Docutils 1.0. |
| 914 |
This transform will be removed in Docutils 2.0. |
| 915 |
""" |
| 916 |
|
| 917 |
default_priority = 850 |
| 918 |
|
| 919 |
def apply(self) -> None: |
| 920 |
visitor = DanglingReferencesVisitor( |
| 921 |
self.document, |
| 922 |
self.document.transformer.unknown_reference_resolvers) |
| 923 |
self.document.walk(visitor) |
| 924 |
|
| 925 |
|
| 926 |
for target in self.document.findall(nodes.target): |
| 927 |
if not target.referenced: |
| 928 |
if target.get('anonymous'): |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
continue |
| 934 |
if target['names']: |
| 935 |
naming = target['names'][0] |
| 936 |
elif target['ids']: |
| 937 |
naming = target['ids'][0] |
| 938 |
else: |
| 939 |
|
| 940 |
|
| 941 |
naming = target['refid'] |
| 942 |
self.document.reporter.info( |
| 943 |
'Hyperlink target "%s" is not referenced.' |
| 944 |
% naming, base_node=target) |
| 945 |
|
| 946 |
|
| 947 |
class DanglingReferencesVisitor(nodes.SparseNodeVisitor): |
| 948 |
"""Provisional : pending deprecation |
| 949 |
|
| 950 |
This auxiliary class is used by the `DanglingReferences` transform |
| 951 |
which will no longer be used in Docutils 1.0. |
| 952 |
It will be removed in Docutils 2.0. |
| 953 |
""" |
| 954 |
|
| 955 |
def __init__(self, document, unknown_reference_resolvers) -> None: |
| 956 |
nodes.SparseNodeVisitor.__init__(self, document) |
| 957 |
self.document = document |
| 958 |
self.unknown_reference_resolvers = unknown_reference_resolvers |
| 959 |
|
| 960 |
def unknown_visit(self, node) -> None: |
| 961 |
pass |
| 962 |
|
| 963 |
def visit_reference(self, node) -> None: |
| 964 |
if node.resolved or not node.hasattr('refname'): |
| 965 |
return |
| 966 |
refname = node['refname'] |
| 967 |
id = self.document.nameids.get(refname, '') |
| 968 |
if id: |
| 969 |
|
| 970 |
del node['refname'] |
| 971 |
node['refid'] = id |
| 972 |
self.document.ids[id].note_referenced_by(id=id) |
| 973 |
node.resolved = True |
| 974 |
return |
| 975 |
|
| 976 |
|
| 977 |
for resolver_function in self.unknown_reference_resolvers: |
| 978 |
if resolver_function(node): |
| 979 |
return |
| 980 |
|
| 981 |
if id is None: |
| 982 |
msg = self.document.reporter.error( |
| 983 |
'Duplicate target name, cannot be used as a unique ' |
| 984 |
f'reference: "{refname}".', base_node=node) |
| 985 |
else: |
| 986 |
if '<' in refname or '>' in refname: |
| 987 |
hint = 'Did you want to embed a URI or alias?' |
| 988 |
if '<' not in refname: |
| 989 |
hint += '\nOpening bracket missing.' |
| 990 |
elif ' <' not in refname: |
| 991 |
hint += ('\nThe embedded reference must be preceded' |
| 992 |
' by whitespace.') |
| 993 |
if '>' not in refname: |
| 994 |
hint += '\nClosing bracket missing.' |
| 995 |
elif not refname.endswith('>'): |
| 996 |
hint += ('\nThe embedded reference must be the last text' |
| 997 |
' before the end string.') |
| 998 |
if '< ' in refname or ' >' in refname: |
| 999 |
hint += ('\nWhitespace around the embedded reference' |
| 1000 |
' is not allowed.') |
| 1001 |
details = [nodes.paragraph('', hint)] |
| 1002 |
else: |
| 1003 |
details = [] |
| 1004 |
msg = self.document.reporter.error( |
| 1005 |
f'Unknown target name: "{refname}".', |
| 1006 |
*details, base_node=node) |
| 1007 |
msgid = self.document.set_id(msg) |
| 1008 |
prb = nodes.problematic(node.rawsource, node.rawsource, refid=msgid) |
| 1009 |
try: |
| 1010 |
prbid = node['ids'][0] |
| 1011 |
except IndexError: |
| 1012 |
prbid = self.document.set_id(prb) |
| 1013 |
msg.add_backref(prbid) |
| 1014 |
node.replace_self(prb) |
| 1015 |
|
| 1016 |
visit_footnote_reference = visit_citation_reference = visit_reference |