Debian Codegraph @

test/test_transforms/test_hyperlinks.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_hyperlinks.py 10305 2026-03-27 21:45:52Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for docutils.transforms.references.Hyperlinks.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.transforms.references import PropagateTargets, \
23 AnonymousHyperlinks, IndirectHyperlinks, ExternalTargets, \
24 InternalTargets, DanglingReferences, SectionIDs
25 from docutils.transforms.universal import TestMessages
26 from docutils.utils import new_document
27
28
29 class TransformTestCase(unittest.TestCase):
30 maxDiff = None
31
32 transforms = (PropagateTargets, AnonymousHyperlinks, IndirectHyperlinks,
33 ExternalTargets, InternalTargets, DanglingReferences,
34 SectionIDs, TestMessages)
35
36 def test_transforms(self):
37 parser = Parser()
38 default_settings = get_default_settings(Parser)
39 default_settings.warning_stream = ''
40 for name, (settings_overrides, cases) in totest.items():
41 settings = default_settings.copy()
42 for k, v in settings_overrides.items():
43 setattr(settings, k, v)
44 for casenum, (case_input, case_expected) in enumerate(cases):
45 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
46 document = new_document('test data', settings)
47 parser.parse(case_input, document)
48 # Don't do a ``populate_from_components()`` because that
49 # would enable the Transformer's default transforms.
50 document.transformer.add_transforms(self.transforms)
51 document.transformer.apply_transforms()
52 output = document.pformat()
53 self.assertEqual(case_expected, output)
54
55
56 totest = {}
57
58 # Exhaustive listing of hyperlink variations: every combination of
59 # target/reference, direct/indirect, internal/external, and named/anonymous,
60 # plus embedded URIs.
61 totest['exhaustive_hyperlinks'] = ({}, [
62 ["""\
63 direct_ external
64
65 .. _direct: http://direct
66 """,
67 """\
68 <document source="test data">
69 <paragraph>
70 <reference name="direct" refuri="http://direct">
71 direct
72 external
73 <target ids="direct" names="direct" refuri="http://direct">
74 """],
75 ["""\
76 indirect_ external
77
78 .. _indirect: xtarget_
79 .. _xtarget: http://indirect
80 """,
81 """\
82 <document source="test data">
83 <paragraph>
84 <reference name="indirect" refuri="http://indirect">
85 indirect
86 external
87 <target ids="indirect" names="indirect" refuri="http://indirect">
88 <target ids="xtarget" names="xtarget" refuri="http://indirect">
89 """],
90 ["""\
91 .. _direct:
92
93 direct_ internal
94 """,
95 """\
96 <document source="test data">
97 <target refid="direct">
98 <paragraph ids="direct" names="direct">
99 <reference name="direct" refid="direct">
100 direct
101 internal
102 """],
103 ["""\
104 .. _ztarget:
105
106 indirect_ internal
107
108 .. _indirect2: ztarget_
109 .. _indirect: indirect2_
110 """,
111 """\
112 <document source="test data">
113 <target refid="ztarget">
114 <paragraph ids="ztarget" names="ztarget">
115 <reference name="indirect" refid="ztarget">
116 indirect
117 internal
118 <target ids="indirect2" names="indirect2" refid="ztarget">
119 <target ids="indirect" names="indirect" refid="ztarget">
120 """],
121 ["""\
122 Implicit
123 --------
124
125 indirect_ internal
126
127 .. _indirect: implicit_
128 """,
129 """\
130 <document source="test data">
131 <section ids="implicit" names="implicit">
132 <title>
133 Implicit
134 <paragraph>
135 <reference name="indirect" refid="implicit">
136 indirect
137 internal
138 <target ids="indirect" names="indirect" refid="implicit">
139 """],
140 ["""\
141 Implicit
142 --------
143
144 `multiply-indirect`_ internal
145
146 .. _multiply-indirect: indirect_
147 .. _indirect: implicit_
148 """,
149 """\
150 <document source="test data">
151 <section ids="implicit" names="implicit">
152 <title>
153 Implicit
154 <paragraph>
155 <reference name="multiply-indirect" refid="implicit">
156 multiply-indirect
157 internal
158 <target ids="multiply-indirect" names="multiply-indirect" refid="implicit">
159 <target ids="indirect" names="indirect" refid="implicit">
160 """],
161 ["""\
162 circular_ indirect reference
163
164 .. _circular: indirect_
165 .. _indirect: circular_
166 """,
167 """\
168 <document source="test data">
169 <paragraph>
170 <problematic ids="problematic-1" refid="system-message-1">
171 circular_
172 indirect reference
173 <target ids="circular" names="circular" refid="circular">
174 <problematic ids="problematic-2 indirect" names="indirect" refid="system-message-1">
175 .. _indirect: circular_
176 <system_message backrefs="problematic-1 problematic-2" ids="system-message-1" level="3" line="3" source="test data" type="ERROR">
177 <paragraph>
178 Indirect hyperlink target "circular" (id="circular") refers to target "indirect", forming a circular reference.
179 """],
180 ["""\
181 Implicit
182 --------
183
184 Duplicate implicit targets.
185
186 Implicit
187 --------
188
189 indirect_ internal
190
191 .. _indirect: implicit_
192
193 Direct internal reference: Implicit_
194 """,
195 """\
196 <document source="test data">
197 <section dupnames="implicit" ids="implicit">
198 <title>
199 Implicit
200 <paragraph>
201 Duplicate implicit targets.
202 <section dupnames="implicit" ids="implicit-1">
203 <title>
204 Implicit
205 <system_message backrefs="implicit-1" level="1" line="7" source="test data" type="INFO">
206 <paragraph>
207 Duplicate implicit target name: "implicit".
208 <paragraph>
209 <problematic ids="problematic-1" refid="system-message-1">
210 indirect_
211 internal
212 <target ids="indirect" names="indirect" refname="implicit">
213 <paragraph>
214 Direct internal reference: \n\
215 <problematic ids="problematic-2" refid="system-message-2">
216 Implicit_
217 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="11" source="test data" type="ERROR">
218 <paragraph>
219 Indirect hyperlink target "indirect" (id="indirect") refers to target "implicit", which is a duplicate, and cannot be used as a unique reference.
220 <system_message backrefs="problematic-2" ids="system-message-2" level="3" line="13" source="test data" type="ERROR">
221 <paragraph>
222 Duplicate target name, cannot be used as a unique reference: "implicit".
223 """],
224 ["""\
225 `direct external`__
226
227 __ http://direct
228 """,
229 """\
230 <document source="test data">
231 <paragraph>
232 <reference anonymous="1" name="direct external" refuri="http://direct">
233 direct external
234 <target anonymous="1" ids="target-1" refuri="http://direct">
235 """],
236 ["""\
237 `indirect external`__
238
239 __ xtarget_
240 .. _xtarget: http://indirect
241 """,
242 """\
243 <document source="test data">
244 <paragraph>
245 <reference anonymous="1" name="indirect external" refuri="http://indirect">
246 indirect external
247 <target anonymous="1" ids="target-1" refuri="http://indirect">
248 <target ids="xtarget" names="xtarget" refuri="http://indirect">
249 """],
250 ["""\
251 __
252
253 `direct internal`__
254 """,
255 """\
256 <document source="test data">
257 <target anonymous="1" refid="target-1">
258 <paragraph ids="target-1">
259 <reference anonymous="1" name="direct internal" refid="target-1">
260 direct internal
261 """],
262 ["""\
263 .. _ztarget:
264
265 `indirect internal`__
266
267 __ ztarget_
268 """,
269 """\
270 <document source="test data">
271 <target refid="ztarget">
272 <paragraph ids="ztarget" names="ztarget">
273 <reference anonymous="1" name="indirect internal" refid="ztarget">
274 indirect internal
275 <target anonymous="1" ids="target-1" refid="ztarget">
276 """],
277 ["""\
278 .. _ztarget:
279
280 First
281
282 .. _ztarget:
283
284 Second
285
286 `indirect internal`__
287
288 __ ztarget_
289 """,
290 """\
291 <document source="test data">
292 <target dupnames="ztarget" refid="ztarget">
293 <paragraph ids="ztarget">
294 First
295 <system_message level="2" line="5" source="test data" type="WARNING">
296 <paragraph>
297 Duplicate explicit target name: "ztarget".
298 <target dupnames="ztarget" refid="ztarget-1">
299 <paragraph ids="ztarget-1">
300 Second
301 <paragraph>
302 <problematic ids="problematic-1" refid="system-message-1">
303 `indirect internal`__
304 <target anonymous="1" ids="target-1" refname="ztarget">
305 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="11" source="test data" type="ERROR">
306 <paragraph>
307 Indirect hyperlink target (id="target-1") refers to target "ztarget", which is a duplicate, and cannot be used as a unique reference.
308 """],
309 ["""\
310 The next anonymous hyperlink reference is parsed (and discarded) at
311 some point, but nonetheless anonymous hyperlink references and targets
312 match in this snippet.
313
314 .. |invalid| replace:: anonymous__
315
316 hyperlink__
317
318 __ URL
319 """,
320 """\
321 <document source="test data">
322 <paragraph>
323 The next anonymous hyperlink reference is parsed (and discarded) at
324 some point, but nonetheless anonymous hyperlink references and targets
325 match in this snippet.
326 <system_message level="3" line="5" source="test data" type="ERROR">
327 <paragraph>
328 Anonymous references are not supported in a substitution definition.
329 <literal_block xml:space="preserve">
330 .. |invalid| replace:: anonymous__
331 <paragraph>
332 <reference anonymous="1" name="hyperlink" refuri="URL">
333 hyperlink
334 <target anonymous="1" ids="target-1" refuri="URL">
335 """],
336 ["""\
337 An `embedded uri <http://direct>`_.
338
339 Another reference to the same `embedded URI`_.
340 """,
341 """\
342 <document source="test data">
343 <paragraph>
344 An \n\
345 <reference name="embedded uri" refuri="http://direct">
346 embedded uri
347 <target ids="embedded-uri" names="embedded\\ uri" refuri="http://direct">
348 .
349 <paragraph>
350 Another reference to the same \n\
351 <reference name="embedded URI" refuri="http://direct">
352 embedded URI
353 .
354 """],
355 ["""\
356 An `anonymous embedded uri <http://direct>`__.
357 """,
358 """\
359 <document source="test data">
360 <paragraph>
361 An \n\
362 <reference name="anonymous embedded uri" refuri="http://direct">
363 anonymous embedded uri
364 .
365 """],
366 ["""\
367 An `embedded alias <alias_>`_.
368
369 Another reference to the same `embedded alias`_.
370
371 .. _alias: ham.py
372 """,
373 """\
374 <document source="test data">
375 <paragraph>
376 An \n\
377 <reference name="embedded alias" refuri="ham.py">
378 embedded alias
379 <target ids="embedded-alias" names="embedded\\ alias" refuri="ham.py">
380 .
381 <paragraph>
382 Another reference to the same \n\
383 <reference name="embedded alias" refuri="ham.py">
384 embedded alias
385 .
386 <target ids="alias" names="alias" refuri="ham.py">
387 """],
388 ["""\
389 An `anonymous embedded alias <redirect_>`__.
390
391 .. _redirect: spam.py
392 """,
393 """\
394 <document source="test data">
395 <paragraph>
396 An \n\
397 <reference name="anonymous embedded alias" refuri="spam.py">
398 anonymous embedded alias
399 .
400 <target ids="redirect" names="redirect" refuri="spam.py">
401 """],
402 ["""\
403 An `embedded alias <alias_>`_ with unknown reference.
404 """,
405 """\
406 <document source="test data">
407 <paragraph>
408 An \n\
409 <problematic ids="problematic-1" refid="system-message-2">
410 `embedded alias <alias_>`_
411 <target ids="embedded-alias" names="embedded\\ alias" refname="alias">
412 with unknown reference.
413 <system_message ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
414 <paragraph>
415 Indirect hyperlink target "embedded alias" (id="embedded-alias") refers to target "alias", which does not exist.
416 <system_message backrefs="problematic-1" ids="system-message-2" level="3" line="1" source="test data" type="ERROR">
417 <paragraph>
418 Unknown target name: "alias".
419 <system_message level="1" line="1" source="test data" type="INFO">
420 <paragraph>
421 Hyperlink target "embedded alias" is not referenced.
422 """],
423 ["""\
424 An embedded URI with trailing underline:
425 `__init__ <http:example.py.html#__init__>`__.
426 """,
427 """\
428 <document source="test data">
429 <paragraph>
430 An embedded URI with trailing underline:
431 <reference name="__init__" refuri="http:example.py.html#__init__">
432 __init__
433 .
434 """],
435 ["""\
436 `link <address >`_
437 """,
438 """\
439 <document source="test data">
440 <paragraph>
441 <problematic ids="problematic-1" refid="system-message-1">
442 `link <address >`_
443 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
444 <paragraph>
445 Unknown target name: "link <address >".
446 <paragraph>
447 Did you want to embed a URI or alias?
448 Whitespace around the embedded reference is not allowed.
449 """],
450 ["""\
451 `link < address>`_
452 """,
453 """\
454 <document source="test data">
455 <paragraph>
456 <problematic ids="problematic-1" refid="system-message-1">
457 `link < address>`_
458 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
459 <paragraph>
460 Unknown target name: "link < address>".
461 <paragraph>
462 Did you want to embed a URI or alias?
463 Whitespace around the embedded reference is not allowed.
464 """],
465 ["""\
466 `link <address> e`_
467 """,
468 """\
469 <document source="test data">
470 <paragraph>
471 <problematic ids="problematic-1" refid="system-message-1">
472 `link <address> e`_
473 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
474 <paragraph>
475 Unknown target name: "link <address> e".
476 <paragraph>
477 Did you want to embed a URI or alias?
478 The embedded reference must be the last text before the end string.
479 """],
480 ["""\
481 `link<address>`_
482 """,
483 """\
484 <document source="test data">
485 <paragraph>
486 <problematic ids="problematic-1" refid="system-message-1">
487 `link<address>`_
488 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
489 <paragraph>
490 Unknown target name: "link<address>".
491 <paragraph>
492 Did you want to embed a URI or alias?
493 The embedded reference must be preceded by whitespace.
494 """],
495 ["""\
496 `link <address`_
497 `link address>`_
498 """,
499 """\
500 <document source="test data">
501 <paragraph>
502 <problematic ids="problematic-1" refid="system-message-1">
503 `link <address`_
504 \n\
505 <problematic ids="problematic-2" refid="system-message-2">
506 `link address>`_
507 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
508 <paragraph>
509 Unknown target name: "link <address".
510 <paragraph>
511 Did you want to embed a URI or alias?
512 Closing bracket missing.
513 <system_message backrefs="problematic-2" ids="system-message-2" level="3" line="1" source="test data" type="ERROR">
514 <paragraph>
515 Unknown target name: "link address>".
516 <paragraph>
517 Did you want to embed a URI or alias?
518 Opening bracket missing.
519 """],
520 ["""\
521 Hyperlinks with angle-bracketed text need escaping.
522
523 See `Element \\<a>`_, `Element <b\\>`_, and `Element <c>\\ `_.
524
525 .. _`Element <a>`:
526 .. _`Element <b>`:
527 .. _`Element <c>`: elements.rst
528 """,
529 """\
530 <document source="test data">
531 <paragraph>
532 Hyperlinks with angle-bracketed text need escaping.
533 <paragraph>
534 See \n\
535 <reference name="Element <a>" refuri="elements.rst">
536 Element <a>
537 , \n\
538 <reference name="Element <b>" refuri="elements.rst">
539 Element <b>
540 , and \n\
541 <reference name="Element <c>" refuri="elements.rst">
542 Element <c>
543 .
544 <target refid="element-a">
545 <target refid="element-b">
546 <target ids="element-c element-b element-a" names="element\\ <c> element\\ <b> element\\ <a>" refuri="elements.rst">
547 """],
548 ["""\
549 .. _target:
550
551 .. [1] Footnote; target_
552 """,
553 """\
554 <document source="test data">
555 <target ids="target" names="target">
556 <footnote ids="footnote-1" names="1">
557 <label>
558 1
559 <paragraph>
560 Footnote; \n\
561 <reference name="target" refid="target">
562 target
563 """],
564 ["""\
565 .. _target:
566
567 .. [cit] Citation; target_
568 """,
569 """\
570 <document source="test data">
571 <target ids="target" names="target">
572 <citation ids="cit" names="cit">
573 <label>
574 cit
575 <paragraph>
576 Citation; \n\
577 <reference name="target" refid="target">
578 target
579 """],
580 ])
581
582 totest['hyperlinks legacy'] = ({'legacy_ids': True}, [
583 ["""\
584 .. _internal hyperlink:
585
586 This paragraph referenced.
587
588 By this `internal hyperlink`_ reference.
589 """,
590 """\
591 <document source="test data">
592 <target refid="internal-hyperlink">
593 <paragraph ids="internal-hyperlink" names="internal\\ hyperlink">
594 This paragraph referenced.
595 <paragraph>
596 By this \n\
597 <reference name="internal hyperlink" refid="internal-hyperlink">
598 internal hyperlink
599 reference.
600 """],
601 ["""\
602 .. _chained:
603 .. _internal hyperlink:
604
605 This paragraph referenced.
606
607 By this `internal hyperlink`_ reference
608 as well as by this chained_ reference.
609
610 The results of the transform are not visible at the XML level.
611 """,
612 """\
613 <document source="test data">
614 <target refid="chained">
615 <target refid="internal-hyperlink">
616 <paragraph ids="internal-hyperlink chained" names="internal\\ hyperlink chained">
617 This paragraph referenced.
618 <paragraph>
619 By this \n\
620 <reference name="internal hyperlink" refid="internal-hyperlink">
621 internal hyperlink
622 reference
623 as well as by this \n\
624 <reference name="chained" refid="chained">
625 chained
626 reference.
627 <paragraph>
628 The results of the transform are not visible at the XML level.
629 """],
630 ["""\
631 .. _chained:
632 __ http://anonymous
633
634 Anonymous__ and chained_ both refer to the same URI.
635 """,
636 """\
637 <document source="test data">
638 <target refid="chained">
639 <target anonymous="1" ids="target-1 chained" names="chained" refuri="http://anonymous">
640 <paragraph>
641 <reference anonymous="1" name="Anonymous" refuri="http://anonymous">
642 Anonymous
643 and \n\
644 <reference name="chained" refuri="http://anonymous">
645 chained
646 both refer to the same URI.
647 """],
648 ["""\
649 .. _a:
650 .. _b:
651
652 x
653 """,
654 """\
655 <document source="test data">
656 <target refid="a">
657 <target refid="b">
658 <paragraph ids="b a" names="b a">
659 x
660 <system_message level="1" line="1" source="test data" type="INFO">
661 <paragraph>
662 Hyperlink target "a" is not referenced.
663 <system_message level="1" line="2" source="test data" type="INFO">
664 <paragraph>
665 Hyperlink target "b" is not referenced.
666 """],
667 ["""\
668 .. _a:
669 .. _b:
670
671 a_
672 """,
673 """\
674 <document source="test data">
675 <target refid="a">
676 <target refid="b">
677 <paragraph ids="b a" names="b a">
678 <reference name="a" refid="a">
679 a
680 <system_message level="1" line="2" source="test data" type="INFO">
681 <paragraph>
682 Hyperlink target "b" is not referenced.
683 """],
684 ["""\
685 .. _a:
686 .. _b:
687
688 b_
689 """,
690 """\
691 <document source="test data">
692 <target refid="a">
693 <target refid="b">
694 <paragraph ids="b a" names="b a">
695 <reference name="b" refid="b">
696 b
697 <system_message level="1" line="1" source="test data" type="INFO">
698 <paragraph>
699 Hyperlink target "a" is not referenced.
700 """],
701 ["""\
702 .. _a:
703 .. _b:
704
705 a_\\ b_
706 """,
707 """\
708 <document source="test data">
709 <target refid="a">
710 <target refid="b">
711 <paragraph ids="b a" names="b a">
712 <reference name="a" refid="a">
713 a
714 <reference name="b" refid="b">
715 b
716 """],
717 ["""\
718 .. _external hyperlink: http://uri
719
720 `External hyperlink`_ reference.
721 """,
722 """\
723 <document source="test data">
724 <target ids="external-hyperlink" names="external\\ hyperlink" refuri="http://uri">
725 <paragraph>
726 <reference name="External hyperlink" refuri="http://uri">
727 External hyperlink
728 reference.
729 """],
730 ["""\
731 .. _external hyperlink: http://uri
732 .. _indirect target: `external hyperlink`_
733 """,
734 """\
735 <document source="test data">
736 <target ids="external-hyperlink" names="external\\ hyperlink" refuri="http://uri">
737 <target ids="indirect-target" names="indirect\\ target" refuri="http://uri">
738 <system_message level="1" line="2" source="test data" type="INFO">
739 <paragraph>
740 Hyperlink target "indirect target" is not referenced.
741 """],
742 ["""\
743 .. _chained:
744 .. _external hyperlink: http://uri
745
746 `External hyperlink`_ reference
747 and a chained_ reference too.
748 """,
749 """\
750 <document source="test data">
751 <target refid="chained">
752 <target ids="external-hyperlink chained" names="external\\ hyperlink chained" refuri="http://uri">
753 <paragraph>
754 <reference name="External hyperlink" refuri="http://uri">
755 External hyperlink
756 reference
757 and a \n\
758 <reference name="chained" refuri="http://uri">
759 chained
760 reference too.
761 """],
762 ["""\
763 .. _external hyperlink: http://uri
764 .. _indirect hyperlink: `external hyperlink`_
765
766 `Indirect hyperlink`_ reference.
767 """,
768 """\
769 <document source="test data">
770 <target ids="external-hyperlink" names="external\\ hyperlink" refuri="http://uri">
771 <target ids="indirect-hyperlink" names="indirect\\ hyperlink" refuri="http://uri">
772 <paragraph>
773 <reference name="Indirect hyperlink" refuri="http://uri">
774 Indirect hyperlink
775 reference.
776 """],
777 ["""\
778 .. _external hyperlink: http://uri
779 .. _chained:
780 .. _indirect hyperlink: `external hyperlink`_
781
782 Chained_ `indirect hyperlink`_ reference.
783 """,
784 """\
785 <document source="test data">
786 <target ids="external-hyperlink" names="external\\ hyperlink" refuri="http://uri">
787 <target refuri="http://uri">
788 <target ids="indirect-hyperlink chained" names="indirect\\ hyperlink chained" refuri="http://uri">
789 <paragraph>
790 <reference name="Chained" refuri="http://uri">
791 Chained
792 \n\
793 <reference name="indirect hyperlink" refuri="http://uri">
794 indirect hyperlink
795 reference.
796 """],
797 ["""\
798 .. __: http://full
799 __
800 __ http://simplified
801 .. _external: http://indirect.external
802 __ external_
803 __
804
805 `Full syntax anonymous external hyperlink reference`__,
806 `chained anonymous external reference`__,
807 `simplified syntax anonymous external hyperlink reference`__,
808 `indirect anonymous hyperlink reference`__,
809 `internal anonymous hyperlink reference`__.
810 """,
811 """\
812 <document source="test data">
813 <target anonymous="1" ids="target-1" refuri="http://full">
814 <target anonymous="1" refid="target-2">
815 <target anonymous="1" ids="target-3 target-2" refuri="http://simplified">
816 <target ids="external" names="external" refuri="http://indirect.external">
817 <target anonymous="1" ids="target-4" refuri="http://indirect.external">
818 <target anonymous="1" refid="target-5">
819 <paragraph ids="target-5">
820 <reference anonymous="1" name="Full syntax anonymous external hyperlink reference" refuri="http://full">
821 Full syntax anonymous external hyperlink reference
822 ,
823 <reference anonymous="1" name="chained anonymous external reference" refuri="http://simplified">
824 chained anonymous external reference
825 ,
826 <reference anonymous="1" name="simplified syntax anonymous external hyperlink reference" refuri="http://simplified">
827 simplified syntax anonymous external hyperlink reference
828 ,
829 <reference anonymous="1" name="indirect anonymous hyperlink reference" refuri="http://indirect.external">
830 indirect anonymous hyperlink reference
831 ,
832 <reference anonymous="1" name="internal anonymous hyperlink reference" refid="target-5">
833 internal anonymous hyperlink reference
834 .
835 """],
836 ["""\
837 Duplicate external target_'s (different URIs):
838
839 .. _target: first
840
841 .. _target: second
842 """,
843 """\
844 <document source="test data">
845 <paragraph>
846 Duplicate external \n\
847 <problematic ids="problematic-1" refid="system-message-1">
848 target_
849 's (different URIs):
850 <target dupnames="target" ids="target" refuri="first">
851 <system_message level="2" line="5" source="test data" type="WARNING">
852 <paragraph>
853 Duplicate explicit target name: "target".
854 <target dupnames="target" ids="target-1" refuri="second">
855 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
856 <paragraph>
857 Duplicate target name, cannot be used as a unique reference: "target".
858 """],
859 ["""\
860 Duplicate external targets (different URIs) without reference:
861
862 .. _target: first
863
864 .. _target: second
865 """,
866 """\
867 <document source="test data">
868 <paragraph>
869 Duplicate external targets (different URIs) without reference:
870 <target dupnames="target" ids="target" refuri="first">
871 <system_message level="2" line="5" source="test data" type="WARNING">
872 <paragraph>
873 Duplicate explicit target name: "target".
874 <target dupnames="target" ids="target-1" refuri="second">
875 """],
876 ["""\
877 Several__ anonymous__ hyperlinks__, but not enough targets.
878
879 __ http://example.org
880 """,
881 """\
882 <document source="test data">
883 <paragraph>
884 <problematic ids="problematic-1" refid="system-message-1">
885 Several__
886 \n\
887 <problematic ids="problematic-2" refid="system-message-1">
888 anonymous__
889 \n\
890 <problematic ids="problematic-3" refid="system-message-1">
891 hyperlinks__
892 , but not enough targets.
893 <target anonymous="1" ids="target-1" refuri="http://example.org">
894 <system_message backrefs="problematic-1 problematic-2 problematic-3" ids="system-message-1" level="3" source="test data" type="ERROR">
895 <paragraph>
896 Anonymous hyperlink mismatch: 3 references but 1 targets.
897 See "backrefs" attribute for IDs.
898 """],
899 ["""\
900 .. _external: http://uri
901 .. _indirect: external_
902 .. _internal:
903
904 .. image:: picture.png
905 :target: external_
906
907 .. image:: picture.png
908 :target: indirect_
909
910 .. image:: picture.png
911 :target: internal_
912 """,
913 """\
914 <document source="test data">
915 <target ids="external" names="external" refuri="http://uri">
916 <target ids="indirect" names="indirect" refuri="http://uri">
917 <target refid="internal">
918 <reference ids="internal" name="external" names="internal" refuri="http://uri">
919 <image uri="picture.png">
920 <reference name="indirect" refuri="http://uri">
921 <image uri="picture.png">
922 <reference name="internal" refid="internal">
923 <image uri="picture.png">
924 """],
925 ["""\
926 .. contents:: Table of Contents
927 .. _indirect reference to the table of contents: `table of contents`_
928
929 Section
930 =======
931
932 Testing an `indirect reference to the table of contents`_.
933 """,
934 """\
935 <document source="test data">
936 <topic classes="contents" ids="table-of-contents" names="table\\ of\\ contents">
937 <title>
938 Table of Contents
939 <bullet_list>
940 <list_item>
941 <paragraph>
942 <reference ids="toc-entry-1" refid="section">
943 Section
944 <target ids="indirect-reference-to-the-table-of-contents" names="indirect\\ reference\\ to\\ the\\ table\\ of\\ contents" refid="table-of-contents">
945 <section ids="section" names="section">
946 <title refid="toc-entry-1">
947 Section
948 <paragraph>
949 Testing an \n\
950 <reference name="indirect reference to the table of contents" refid="table-of-contents">
951 indirect reference to the table of contents
952 .
953 """],
954 ["""\
955 .. _explicit target:
956
957 Title
958 -----
959
960 Let's reference it (`explicit target`_) to avoid an irrelevant error.
961 """,
962 """\
963 <document source="test data">
964 <target refid="explicit-target">
965 <section ids="title explicit-target" names="title explicit\\ target">
966 <title>
967 Title
968 <paragraph>
969 Let's reference it (
970 <reference name="explicit target" refid="explicit-target">
971 explicit target
972 ) to avoid an irrelevant error.
973 """],
974 ["""\
975 target1_ should refer to target2_, not the Title.
976
977 .. _target1:
978 .. _target2: URI
979
980 Title
981 =====
982 """,
983 """\
984 <document source="test data">
985 <paragraph>
986 <reference name="target1" refuri="URI">
987 target1
988 should refer to \n\
989 <reference name="target2" refuri="URI">
990 target2
991 , not the Title.
992 <target refid="target1">
993 <target ids="target2 target1" names="target2 target1" refuri="URI">
994 <section ids="title" names="title">
995 <title>
996 Title
997 """],
998 ["""\
999 Unknown reference_.
1000 """,
1001 """\
1002 <document source="test data">
1003 <paragraph>
1004 Unknown \n\
1005 <problematic ids="problematic-1" refid="system-message-1">
1006 reference_
1007 .
1008 <system_message backrefs="problematic-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
1009 <paragraph>
1010 Unknown target name: "reference".
1011 """],
1012 ["""\
1013 Duplicate manual footnote labels, with reference ([1]_):
1014
1015 .. [1] Footnote.
1016
1017 .. [1] Footnote.
1018 """,
1019 """\
1020 <document source="test data">
1021 <paragraph>
1022 Duplicate manual footnote labels, with reference (
1023 <problematic ids="footnote-reference-1" refid="system-message-1">
1024 [1]_
1025 ):
1026 <footnote dupnames="1" ids="footnote-1">
1027 <label>
1028 1
1029 <paragraph>
1030 Footnote.
1031 <footnote dupnames="1" ids="footnote-2">
1032 <label>
1033 1
1034 <system_message backrefs="footnote-2" level="2" line="5" source="test data" type="WARNING">
1035 <paragraph>
1036 Duplicate explicit target name: "1".
1037 <paragraph>
1038 Footnote.
1039 <system_message backrefs="footnote-reference-1" ids="system-message-1" level="3" line="1" source="test data" type="ERROR">
1040 <paragraph>
1041 Duplicate target name, cannot be used as a unique reference: "1".
1042 """],
1043 ["""\
1044 An explicit target _`foo` overrides a homonymous implicit target.
1045
1046 foo
1047 ---
1048
1049 The reference foo_ points to the explicit target.
1050 """,
1051 """\
1052 <document source="test data">
1053 <paragraph>
1054 An explicit target \n\
1055 <target ids="foo" names="foo">
1056 foo
1057 overrides a homonymous implicit target.
1058 <section dupnames="foo" ids="foo-1">
1059 <title>
1060 foo
1061 <system_message backrefs="foo-1" level="1" line="4" source="test data" type="INFO">
1062 <paragraph>
1063 Duplicate implicit target name: "foo".
1064 <paragraph>
1065 The reference \n\
1066 <reference name="foo" refid="foo">
1067 foo
1068 points to the explicit target.
1069 """],
1070 ["""\
1071 foo
1072 ---
1073
1074 If an implicit target precedes a homonymous explicit target _`foo`,
1075 the explicit target takes over the reference name but not the identifier.
1076
1077 The reference foo_ points to the explicit target. However, referencing from
1078 an external document requires the non-obvious fragment identifier "#foo-1".
1079 """,
1080 """\
1081 <document source="test data">
1082 <section dupnames="foo" ids="foo">
1083 <title>
1084 foo
1085 <system_message backrefs="foo-1" level="1" line="5" source="test data" type="INFO">
1086 <paragraph>
1087 Target name overrides implicit target name "foo".
1088 <paragraph>
1089 If an implicit target precedes a homonymous explicit target \n\
1090 <target ids="foo-1" names="foo">
1091 foo
1092 ,
1093 the explicit target takes over the reference name but not the identifier.
1094 <paragraph>
1095 The reference \n\
1096 <reference name="foo" refid="foo-1">
1097 foo
1098 points to the explicit target. However, referencing from
1099 an external document requires the non-obvious fragment identifier "#foo-1".
1100 """],
1101 ])
1102
1103 totest['hyperlinks'] = ({'legacy_ids': False},
1104 # all but the last sample compile as before
1105 totest['hyperlinks legacy'][1][:-1] + [
1106 ["""\
1107 foo
1108 ---
1109
1110 With legacy_ids = False, an explicit target _`foo` takes over the
1111 reference name and identifier of a homonymous implicit target.
1112
1113 The reference foo_ points to the explicit target.
1114 Referencing from an external document can be done
1115 using the matching fragment identifier "#foo".
1116 """,
1117 """\
1118 <document source="test data">
1119 <section dupnames="foo" ids="foo-1">
1120 <title>
1121 foo
1122 <system_message backrefs="foo" level="1" line="5" source="test data" type="INFO">
1123 <paragraph>
1124 Target name overrides implicit target name "foo".
1125 <paragraph>
1126 With legacy_ids = False, an explicit target \n\
1127 <target ids="foo" names="foo">
1128 foo
1129 takes over the
1130 reference name and identifier of a homonymous implicit target.
1131 <paragraph>
1132 The reference \n\
1133 <reference name="foo" refid="foo">
1134 foo
1135 points to the explicit target.
1136 Referencing from an external document can be done
1137 using the matching fragment identifier "#foo".
1138 """],
1139 ])
1140
1141
1142 if __name__ == '__main__':
1143 unittest.main()