Debian Codegraph @

tests/test_syntax/extensions/test_abbr.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Python Markdown
4
5 A Python implementation of John Gruber's Markdown.
6
7 Documentation: https://python-markdown.github.io/
8 GitHub: https://github.com/Python-Markdown/markdown/
9 PyPI: https://pypi.org/project/Markdown/
10
11 Started by Manfred Stienstra (http://www.dwerg.net/).
12 Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
13 Currently maintained by Waylan Limberg (https://github.com/waylan),
14 Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
15
16 Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later)
17 Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
18 Copyright 2004 Manfred Stienstra (the original version)
19
20 License: BSD (see LICENSE.md for details).
21 """
22
23 from markdown.test_tools import TestCase
24 from markdown import Markdown
25 from markdown.extensions.abbr import AbbrExtension
26
27
28 class TestAbbr(TestCase):
29 maxDiff = None
30
31 default_kwargs = {'extensions': ['abbr']}
32
33 def test_ignore_atomic(self):
34 self.assertMarkdownRenders(
35 self.dedent(
36 """
37 This <https://example.com/{YAFR}>
38
39 *[YAFR]: Yet Another Feature Request
40 """
41 ),
42 '<p>This <a href="https://example.com/{YAFR}">https://example.com/{YAFR}</a></p>'
43 )
44
45 def test_abbr_upper(self):
46 self.assertMarkdownRenders(
47 self.dedent(
48 """
49 ABBR
50
51 *[ABBR]: Abbreviation
52 """
53 ),
54 self.dedent(
55 """
56 <p><abbr title="Abbreviation">ABBR</abbr></p>
57 """
58 )
59 )
60
61 def test_abbr_lower(self):
62 self.assertMarkdownRenders(
63 self.dedent(
64 """
65 abbr
66
67 *[abbr]: Abbreviation
68 """
69 ),
70 self.dedent(
71 """
72 <p><abbr title="Abbreviation">abbr</abbr></p>
73 """
74 )
75 )
76
77 def test_abbr_multiple_in_text(self):
78 self.assertMarkdownRenders(
79 self.dedent(
80 """
81 The HTML specification
82 is maintained by the W3C.
83
84 *[HTML]: Hyper Text Markup Language
85 *[W3C]: World Wide Web Consortium
86 """
87 ),
88 self.dedent(
89 """
90 <p>The <abbr title="Hyper Text Markup Language">HTML</abbr> specification
91 is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
92 """
93 )
94 )
95
96 def test_abbr_multiple_in_tail(self):
97 self.assertMarkdownRenders(
98 self.dedent(
99 """
100 *The* HTML specification
101 is maintained by the W3C.
102
103 *[HTML]: Hyper Text Markup Language
104 *[W3C]: World Wide Web Consortium
105 """
106 ),
107 self.dedent(
108 """
109 <p><em>The</em> <abbr title="Hyper Text Markup Language">HTML</abbr> specification
110 is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
111 """
112 )
113 )
114
115 def test_abbr_multiple_nested(self):
116 self.assertMarkdownRenders(
117 self.dedent(
118 """
119 The *HTML* specification
120 is maintained by the *W3C*.
121
122 *[HTML]: Hyper Text Markup Language
123 *[W3C]: World Wide Web Consortium
124 """
125 ),
126 self.dedent(
127 """
128 <p>The <em><abbr title="Hyper Text Markup Language">HTML</abbr></em> specification
129 is maintained by the <em><abbr title="World Wide Web Consortium">W3C</abbr></em>.</p>
130 """
131 )
132 )
133
134 def test_abbr_override(self):
135 self.assertMarkdownRenders(
136 self.dedent(
137 """
138 ABBR
139
140 *[ABBR]: Ignored
141 *[ABBR]: The override
142 """
143 ),
144 self.dedent(
145 """
146 <p><abbr title="The override">ABBR</abbr></p>
147 """
148 )
149 )
150
151 def test_abbr_glossary(self):
152
153 glossary = {
154 "ABBR": "Abbreviation",
155 "abbr": "Abbreviation",
156 "HTML": "Hyper Text Markup Language",
157 "W3C": "World Wide Web Consortium"
158 }
159
160 self.assertMarkdownRenders(
161 self.dedent(
162 """
163 ABBR
164 abbr
165
166 HTML
167 W3C
168 """
169 ),
170 self.dedent(
171 """
172 <p><abbr title="Abbreviation">ABBR</abbr>
173 <abbr title="Abbreviation">abbr</abbr></p>
174 <p><abbr title="Hyper Text Markup Language">HTML</abbr>
175 <abbr title="World Wide Web Consortium">W3C</abbr></p>
176 """
177 ),
178 extensions=[AbbrExtension(glossary=glossary)]
179 )
180
181 def test_abbr_glossary_2(self):
182
183 glossary = {
184 "ABBR": "Abbreviation",
185 "abbr": "Abbreviation",
186 "HTML": "Hyper Text Markup Language",
187 "W3C": "World Wide Web Consortium"
188 }
189
190 glossary_2 = {
191 "ABBR": "New Abbreviation"
192 }
193
194 abbr_ext = AbbrExtension(glossary=glossary)
195 abbr_ext.load_glossary(glossary_2)
196
197 self.assertMarkdownRenders(
198 self.dedent(
199 """
200 ABBR abbr HTML W3C
201 """
202 ),
203 self.dedent(
204 """
205 <p><abbr title="New Abbreviation">ABBR</abbr> """
206 + """<abbr title="Abbreviation">abbr</abbr> """
207 + """<abbr title="Hyper Text Markup Language">HTML</abbr> """
208 + """<abbr title="World Wide Web Consortium">W3C</abbr></p>
209 """
210 ),
211 extensions=[abbr_ext]
212 )
213
214 def test_abbr_nested(self):
215 self.assertMarkdownRenders(
216 self.dedent(
217 """
218 [ABBR](/foo)
219
220 _ABBR_
221
222 *[ABBR]: Abbreviation
223 """
224 ),
225 self.dedent(
226 """
227 <p><a href="/foo"><abbr title="Abbreviation">ABBR</abbr></a></p>
228 <p><em><abbr title="Abbreviation">ABBR</abbr></em></p>
229 """
230 )
231 )
232
233 def test_abbr_no_blank_Lines(self):
234 self.assertMarkdownRenders(
235 self.dedent(
236 """
237 ABBR
238 *[ABBR]: Abbreviation
239 ABBR
240 """
241 ),
242 self.dedent(
243 """
244 <p><abbr title="Abbreviation">ABBR</abbr></p>
245 <p><abbr title="Abbreviation">ABBR</abbr></p>
246 """
247 )
248 )
249
250 def test_abbr_no_space(self):
251 self.assertMarkdownRenders(
252 self.dedent(
253 """
254 ABBR
255
256 *[ABBR]:Abbreviation
257 """
258 ),
259 self.dedent(
260 """
261 <p><abbr title="Abbreviation">ABBR</abbr></p>
262 """
263 )
264 )
265
266 def test_abbr_extra_space(self):
267 self.assertMarkdownRenders(
268 self.dedent(
269 """
270 ABBR
271
272 *[ABBR] : Abbreviation
273 """
274 ),
275 self.dedent(
276 """
277 <p><abbr title="Abbreviation">ABBR</abbr></p>
278 """
279 )
280 )
281
282 def test_abbr_line_break(self):
283 self.assertMarkdownRenders(
284 self.dedent(
285 """
286 ABBR
287
288 *[ABBR]:
289 Abbreviation
290 """
291 ),
292 self.dedent(
293 """
294 <p><abbr title="Abbreviation">ABBR</abbr></p>
295 """
296 )
297 )
298
299 def test_abbr_ignore_unmatched_case(self):
300 self.assertMarkdownRenders(
301 self.dedent(
302 """
303 ABBR abbr
304
305 *[ABBR]: Abbreviation
306 """
307 ),
308 self.dedent(
309 """
310 <p><abbr title="Abbreviation">ABBR</abbr> abbr</p>
311 """
312 )
313 )
314
315 def test_abbr_partial_word(self):
316 self.assertMarkdownRenders(
317 self.dedent(
318 """
319 ABBR ABBREVIATION
320
321 *[ABBR]: Abbreviation
322 """
323 ),
324 self.dedent(
325 """
326 <p><abbr title="Abbreviation">ABBR</abbr> ABBREVIATION</p>
327 """
328 )
329 )
330
331 def test_abbr_unused(self):
332 self.assertMarkdownRenders(
333 self.dedent(
334 """
335 foo bar
336
337 *[ABBR]: Abbreviation
338 """
339 ),
340 self.dedent(
341 """
342 <p>foo bar</p>
343 """
344 )
345 )
346
347 def test_abbr_double_quoted(self):
348 self.assertMarkdownRenders(
349 self.dedent(
350 """
351 ABBR
352
353 *[ABBR]: "Abbreviation"
354 """
355 ),
356 self.dedent(
357 """
358 <p><abbr title="&quot;Abbreviation&quot;">ABBR</abbr></p>
359 """
360 )
361 )
362
363 def test_abbr_single_quoted(self):
364 self.assertMarkdownRenders(
365 self.dedent(
366 """
367 ABBR
368
369 *[ABBR]: 'Abbreviation'
370 """
371 ),
372 self.dedent(
373 """
374 <p><abbr title="'Abbreviation'">ABBR</abbr></p>
375 """
376 )
377 )
378
379 def test_abbr_ignore_backslash(self):
380 self.assertMarkdownRenders(
381 self.dedent(
382 r"""
383 \\foo
384
385 *[\\foo]: Not an abbreviation
386 """
387 ),
388 self.dedent(
389 r"""
390 <p>\foo</p>
391 <p>*[\foo]: Not an abbreviation</p>
392 """
393 )
394 )
395
396 def test_abbr_hyphen(self):
397 self.assertMarkdownRenders(
398 self.dedent(
399 """
400 ABBR-abbr
401
402 *[ABBR-abbr]: Abbreviation
403 """
404 ),
405 self.dedent(
406 """
407 <p><abbr title="Abbreviation">ABBR-abbr</abbr></p>
408 """
409 )
410 )
411
412 def test_abbr_carrot(self):
413 self.assertMarkdownRenders(
414 self.dedent(
415 """
416 ABBR^abbr
417
418 *[ABBR^abbr]: Abbreviation
419 """
420 ),
421 self.dedent(
422 """
423 <p><abbr title="Abbreviation">ABBR^abbr</abbr></p>
424 """
425 )
426 )
427
428 def test_abbr_bracket(self):
429 self.assertMarkdownRenders(
430 self.dedent(
431 """
432 ABBR]abbr
433
434 *[ABBR]abbr]: Abbreviation
435 """
436 ),
437 self.dedent(
438 """
439 <p><abbr title="Abbreviation">ABBR]abbr</abbr></p>
440 """
441 )
442 )
443
444 def test_abbr_with_attr_list(self):
445 self.assertMarkdownRenders(
446 self.dedent(
447 """
448 *[abbr]: Abbreviation Definition
449
450 ![Image with abbr in title](abbr.png){title="Image with abbr in title"}
451 """
452 ),
453 self.dedent(
454 """
455 <p><img alt="Image with abbr in title" src="abbr.png" title="Image with abbr in title" /></p>
456 """
457 ),
458 extensions=['abbr', 'attr_list']
459 )
460
461 def test_abbr_superset_vs_subset(self):
462 self.assertMarkdownRenders(
463 self.dedent(
464 """
465 abbr, SS, and abbr-SS should have different definitions.
466
467 *[abbr]: Abbreviation Definition
468 *[abbr-SS]: Abbreviation Superset Definition
469 *[SS]: Superset Definition
470 """
471 ),
472 self.dedent(
473 """
474 <p><abbr title="Abbreviation Definition">abbr</abbr>, """
475 + """<abbr title="Superset Definition">SS</abbr>, """
476 + """and <abbr title="Abbreviation Superset Definition">abbr-SS</abbr> """
477 + """should have different definitions.</p>
478 """
479 )
480 )
481
482 def test_abbr_empty(self):
483 self.assertMarkdownRenders(
484 self.dedent(
485 """
486 *[abbr]:
487 Abbreviation Definition
488
489 abbr
490
491 *[]: Empty
492
493 *[ ]: Empty
494
495 *[abbr]:
496
497 *[ABBR]:
498
499 Testing document text.
500 """
501 ),
502 self.dedent(
503 """
504 <p><abbr title="Abbreviation Definition">abbr</abbr></p>\n"""
505 + """<p>*[]: Empty</p>\n"""
506 + """<p>*[ ]: Empty</p>\n"""
507 + """<p>*[<abbr title="Abbreviation Definition">abbr</abbr>]:</p>\n"""
508 + """<p>*[ABBR]:</p>\n"""
509 + """<p>Testing document text.</p>
510 """
511 )
512 )
513
514 def test_abbr_clear(self):
515 self.assertMarkdownRenders(
516 self.dedent(
517 """
518 *[abbr]: Abbreviation Definition
519 *[ABBR]: Abbreviation Definition
520
521 abbr ABBR
522
523 *[abbr]: ""
524 *[ABBR]: ''
525 """
526 ),
527 self.dedent(
528 """
529 <p>abbr ABBR</p>
530 """
531 )
532 )
533
534 def test_abbr_reset(self):
535 ext = AbbrExtension()
536 md = Markdown(extensions=[ext])
537 md.convert('*[abbr]: Abbreviation Definition')
538 self.assertEqual(ext.abbrs, {'abbr': 'Abbreviation Definition'})
539 md.convert('*[ABBR]: Capitalised Abbreviation')
540 self.assertEqual(ext.abbrs, {'abbr': 'Abbreviation Definition', 'ABBR': 'Capitalised Abbreviation'})
541 md.reset()
542 self.assertEqual(ext.abbrs, {})
543 md.convert('*[foo]: Foo Definition')
544 self.assertEqual(ext.abbrs, {'foo': 'Foo Definition'})