-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_plugin.py
More file actions
801 lines (697 loc) · 24.4 KB
/
test_plugin.py
File metadata and controls
801 lines (697 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
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
116
117
118
119
120
121
122
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
148
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
"""mkdocs-alias-plugin tests
Various unit tests for the non-mkdocs functions in plugin.py.
"""
import logging
import re
from mkdocs.structure.files import File
from alias.plugin import (
ALIAS_TAG_REGEX,
REFERENCE_REGEX,
get_alias_names,
get_page_title,
replace_reference,
replace_tag,
ReplaceTagContext,
split_anchor,
find_anchor_by_id,
)
PAGE_FILE = File("/folder1/folder4/folder5/test.md", "/src/", "/dest/", False)
def test_split_anchor():
"""Test splitting an alias tag into the alias and anchor parts"""
assert split_anchor("my-alias#anchor") == ("my-alias", "anchor")
assert split_anchor("my-alias#anchor#another") == ("my-alias", "anchor")
assert split_anchor("my-alias") == ("my-alias", None)
assert split_anchor("#anchor") == (None, "anchor")
assert split_anchor("#anchor#another") == (None, "anchor")
assert split_anchor("") == ('', None)
def test_find_anchor_by_id():
"""Test finding an anchor by its ID in a list of anchors"""
anchors = [
{"id": "anchor1", "name": "Anchor 1"},
{"id": "anchor2", "name": "Anchor 2"},
{"id": "anchor3", "name": "Anchor 3"},
{"id": "anchor4", "name": "Anchor 4", "children": [
{"id": "nested1", "name": "Nested Anchor 1"},
{"id": "nested2", "name": "Nested Anchor 2"},
]},
]
assert find_anchor_by_id(anchors, "anchor1") == anchors[0]
assert find_anchor_by_id(anchors, "anchor2") == anchors[1]
assert find_anchor_by_id(anchors, "anchor3") == anchors[2]
assert find_anchor_by_id(anchors, "nested1") == anchors[3]["children"][0]
assert find_anchor_by_id(anchors, "nested2") == anchors[3]["children"][1]
assert find_anchor_by_id(anchors, "not-an-anchor") is None
def test_get_page_title_1():
"""Test title from H1 tag extraction"""
source = "# The actual title\n\nSome body text"
meta = {}
assert get_page_title(source, meta) == "The actual title"
def test_get_page_title_2():
"""Test title from meta block extraction"""
source = "# NOT The actual title\n\nSome body text"
meta = {"title": "The actual title"}
assert get_page_title(source, meta) == "The actual title"
def test_get_page_title_3():
"""Test page without a title"""
source = "Some body text"
meta = {}
assert get_page_title(source, meta) is None
def test_get_page_title_4():
"""Test page with both meta and H1 titles"""
source = "# Not the title\n\nsome text\n"
meta = {"title": "The actual title"}
assert get_page_title(source, meta) == "The actual title"
def test_get_page_title_5():
"""Test page with both meta title and icon enabled"""
source = "# Not the title\n\nsome text\n"
meta = {"title": "The actual title", "icon": "path/to/my-icon"}
assert (
get_page_title(source, meta, include_icon=True)
== ":path-to-my-icon: The actual title"
)
def test_get_page_title_6():
"""Test page with both meta title and icon but disabled"""
source = "# Not the title\n\nsome text\n"
meta = {"title": "The actual title", "icon": "path/to/my-icon"}
assert get_page_title(source, meta, include_icon=False) == "The actual title"
def test_get_alias_name_1():
"""Test alias from meta extraction"""
meta = {"alias": "my-alias"}
assert get_alias_names(meta) == ["my-alias"]
def test_get_alias_name_2():
"""Test alias from empty meta extraction"""
meta = {}
assert get_alias_names(meta) is None
def test_get_alias_name_3():
"""Test alias from name/title pair"""
meta = {"alias": {"name": "my-alias", "title": "My Title"}}
assert get_alias_names(meta) == ["my-alias"]
def test_get_alias_name_4():
"""Test aliases from array"""
meta = {"alias": ["my-alias", "my-other-alias", "foobar"]}
assert get_alias_names(meta) == ["my-alias", "my-other-alias", "foobar"]
def test_get_alias_name_5():
"""Test aliases from array with non-string entries"""
meta = {"alias": ["my-alias", 1, False, None, 4.821]}
assert get_alias_names(meta) == ["my-alias"]
def test_get_multiple_aliases():
"""Allow for the use of multiple aliases"""
meta = {"alias": ["my-alias", "ma", "myalias"]}
assert get_alias_names(meta) == ["my-alias", "ma", "myalias"]
def test_replace_tag_1():
"""Should match a simple alias tag"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my-alias]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [link text](../../../my-alias.md)"
def test_replace_tag_2():
"""Should replace an alias with a specified title"""
context = ReplaceTagContext(
aliases = {
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my-alias|Alternate Text]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [Alternate Text](../../../my-alias.md)"
def test_replace_tag_3():
"""Should not replace an escaped alias"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: \\[[my-alias|Alternate Text]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [[my-alias|Alternate Text]]"
def test_replace_tag_4():
"""Should not parse an escaped alias"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "\\[[ ! -d $HOME/myfolder ]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "[[ ! -d $HOME/myfolder ]]"
def test_replace_tag_5():
"""Should return the original string if the alias wasn't found"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[unknown]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_replace_tag_6():
"""Should use the URL as text if the text wasn't set"""
context = ReplaceTagContext(
log=logging.getLogger(),
aliases={"my-alias": {"text": None, "alias": "my-alias", "url": "my-alias.md"}},
page_file=PAGE_FILE,
)
markdown = "Test: [[my-alias]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [my-alias.md](../../../my-alias.md)"
def test_replace_tag_7():
"""Should handle aliases with spaces"""
context = ReplaceTagContext(
aliases={
" my spacey alias ": {
"text": "The Text",
"alias": " my spacey alias ",
"url": "my-alias.md",
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[ my spacey alias ]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md)"
def test_replace_tag_with_anchor():
"""Should handle aliases with URL anchors"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my alias#anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md#anchor)"
def test_replace_tag_with_anchor2():
"""Should handle aliases with multiple URL anchors"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my alias#anchor#another ignored anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md#anchor)"
def test_replace_tag_with_anchor3():
"""Should handle aliases with an anchor and a title"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my alias#my anchor|The Title]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Title](../../../my-alias.md#my anchor)"
def test_replace_tag_shouldnt_break_alias_not_found():
"""An alias that doesn't exist shouldn't break the plugin"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[unknown]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_replace_tag_shouldnt_break_alias_not_found_with_anchor():
"""An alias that doesn't exist shouldn't break the plugin"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[unknown#anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_replace_tag_shouldnt_break_alias_not_found_with_text():
"""An alias that doesn't exist shouldn't break the plugin"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[unknown|Some bad reference]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_replace_tag_shouldnt_break_alias_not_found_with_text_and_anchor():
"""An alias that doesn't exist shouldn't break the plugin"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[unknown#anchor|Some bad reference]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_plugin_shouldnt_break_with_text():
"""An alias with the word 'text' in it shouldn't break the plugin"""
context = ReplaceTagContext(
aliases={"text": {"text": "The Text", "alias": "text", "url": "my-alias.md"}},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[text]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md)"
def test_plugin_should_replace_anchors_with_titles():
"""An alias with an anchor should be replaced with the title if the
config option is set"""
context = ReplaceTagContext(
aliases={
"my alias": {
"text": "The Text",
"alias": "my alias",
"url": "my-alias.md",
"anchors": [{"id": "anchor", "name": "The Anchor"}],
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
use_anchor_titles=True,
)
markdown = "Test: [[my alias#anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Anchor](../../../my-alias.md#anchor)"
def test_plugin_should_note_replace_anchors_with_titles():
"""An alias with an anchor should be NOT replaced with the title if the
config option is not set"""
context = ReplaceTagContext(
aliases={
"my alias": {
"text": "The Text",
"alias": "my alias",
"url": "my-alias.md",
"anchors": [{"id": "anchor", "name": "The Anchor"}],
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
use_anchor_titles=False,
)
markdown = "Test: [[my alias#anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md#anchor)"
def test_plugin_should_replace_anchors_with_nesting():
"""An alias with an anchor should be replaced with the title if the
config option is set with the appropriate nested child anchor"""
context = ReplaceTagContext(
aliases={
"my alias": {
"text": "The Text",
"alias": "my alias",
"url": "my-alias.md",
"anchors": [
{
"id": "anchor",
"name": "The Anchor",
"children": [{"id": "nested", "name": "The Nested Anchor"}],
}
],
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
use_anchor_titles=True,
)
markdown = "Test: [[my alias#nested]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Nested Anchor](../../../my-alias.md#nested)"
def test_plugin_should_function_as_usual_without_anchors():
"""An alias with an anchor, with the config option set to true, but no
anchors should still function as usual"""
context = ReplaceTagContext(
aliases={
"my alias": {
"text": "The Text",
"alias": "my alias",
"url": "my-alias.md",
"anchors": [],
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
use_anchor_titles=True,
)
markdown = "Test: [[my alias#anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [The Text](../../../my-alias.md#anchor)"
def test_plugin_should_use_custom_title_with_anchor_titles():
"""An alias with an anchor, with the config option set to true, with
anchors, but with a custom title should use the custom title"""
context = ReplaceTagContext(
aliases={
"my alias": {
"text": "The Text",
"alias": "my alias",
"url": "my-alias.md",
"anchors": [
{
"id": "anchor",
"name": "The Anchor",
"children": [{"id": "nested", "name": "The Nested Anchor"}],
}
],
}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
use_anchor_titles=True,
)
markdown = "Test: [[my alias#nested|Custom Title]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [Custom Title](../../../my-alias.md#nested)"
def test_plugin_should_link_to_anchor_on_current_page():
"""An alias with an anchor on the current page should link to it
without the page path"""
page_file = File(
"foo/bar.md",
src_dir=None,
dest_dir="/path/to/site",
use_directory_urls=False,
)
page_file.content_string = "Test: [[#anchor]]\n\n## Anchor\n\nSome text"
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=page_file,
)
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
page_file.content_string,
)
assert result == "Test: [Anchor](#anchor)\n\n## Anchor\n\nSome text"
def test_plugin_should_not_break_on_anchor_not_found_on_current_page():
"""An alias that doesn't exist shouldn't break the plugin"""
page_file = File(
"foo/bar.md",
src_dir=None,
dest_dir="/path/to/site",
use_directory_urls=False,
)
page_file.content_string = "Test: [[#anchor]]\n\n## Anchor\n\nSome text"
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=page_file,
)
markdown = "Test: [[#not-anchor]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == markdown
def test_get_alias_names_1():
"""Test alias from meta extraction"""
meta = {"alias": "my-alias"}
assert get_alias_names(meta) == ["my-alias"]
def test_get_alias_names_2():
"""Test alias from empty meta extraction"""
meta = {}
assert get_alias_names(meta) is None
def test_get_alias_names_3():
"""Test alias from meta extraction using a custom key"""
meta = {"custom-key": "my-alias"}
assert get_alias_names(meta, "custom-key") == ["my-alias"]
def test_get_alias_names_4():
"""Test alias from name/title pair"""
meta = {"alias": {"name": "my-alias", "title": "My Title"}}
assert get_alias_names(meta, "alias") == ["my-alias"]
def test_replace_reference():
"""reference syntax: basic alias"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "[alias]: [[my alias]]"
result = re.sub(
REFERENCE_REGEX,
lambda match: replace_reference(match, context),
markdown,
)
assert result == "[alias]: ../../../my-alias.md"
def test_replace_reference_anchor():
"""reference syntax: alias with anchor"""
context = ReplaceTagContext(
aliases={
"my alias": {"text": "The Text", "alias": "my alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "[alias]: [[my alias#anchor]]"
result = re.sub(
REFERENCE_REGEX,
lambda match: replace_reference(match, context),
markdown,
)
assert result == "[alias]: ../../../my-alias.md#anchor"
def test_accept_escaped_brackets_1():
"""allow escaping square brackets within the alias"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my-alias|Alternate \\[alt\\] Text]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [Alternate \\[alt\\] Text](../../../my-alias.md)"
def test_dont_accept_unescaped_brackets():
"""don't allow unescaped square brackets within the alias"""
context = ReplaceTagContext(
aliases={
"my-alias": {"text": "link text", "alias": "my-alias", "url": "my-alias.md"}
},
log=logging.getLogger(),
page_file=PAGE_FILE,
)
markdown = "Test: [[my-alias|Alternate [alt] Text]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [[my-alias|Alternate [alt] Text]]"
def test_interwiki_alias_basic():
"""Test interwiki alias replacement without a title"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[wp:Python (programming language)]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == (
"Test: [Python (programming language)]"
"(https://en.wikipedia.org/wiki/Python%20%28programming%20language%29)"
)
def test_interwiki_alias_basic_with_anchor():
"""Test interwiki alias replacement without a title with an anchor"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[wp:Python (programming language)#History]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == (
"Test: [Python (programming language)#History]"
"(https://en.wikipedia.org/wiki/Python%20%28programming%20language%29%23History)"
)
def test_interwiki_alias_with_title():
"""Test interwiki alias replacement with a title"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[wp:Python (programming language)|Wikipedia Article]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == (
"Test: [Wikipedia Article]"
"(https://en.wikipedia.org/wiki/Python%20%28programming%20language%29)"
)
def test_interwiki_prefix_not_in_interwiki():
"""Test that an interwiki alias with a prefix not in the interwiki config is not replaced"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[unknown:Python (programming language)]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == "Test: [[unknown:Python (programming language)]]"
def test_interwiki_alias_with_anchor_and_title():
"""Test interwiki alias replacement with an anchor and a title"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[wp:Python (programming language)#History|History]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == (
"Test: [History]"
"(https://en.wikipedia.org/wiki/Python%20%28programming%20language%29%23History)"
)
def test_interwiki_alias_with_multiple_anchors():
"""Test interwiki alias replacement with multiple anchors, only the first
anchor should be used"""
context = ReplaceTagContext(
aliases={},
log=logging.getLogger(),
page_file=PAGE_FILE,
interwiki={"wp": "https://en.wikipedia.org/wiki/{{alias}}"},
)
markdown = "Test: [[wp:Python (programming language)#History#Another Anchor|History]]"
result = re.sub(
ALIAS_TAG_REGEX,
lambda match: replace_tag(match, context),
markdown,
)
assert result == (
"Test: [History]"
"(https://en.wikipedia.org/wiki/Python%20%28programming%20language%29%23History)"
)