Debian Codegraph @

test/test_writers/test_html4css1.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_html4css1.py 10102 2025-04-23 15:54:44Z milde $
4 # Author: reggie dugard <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """Test HTML4 writer output ("fragment" part).
8
9 This is the document body (not HTML <body>).
10 """
11
12 from pathlib import Path
13 import sys
14 import unittest
15
16 if __name__ == '__main__':
17 # prepend the "docutils root" to the Python library path
18 # so we import the local `docutils` package.
19 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
20
21 import docutils
22 import docutils.core
23 from docutils.parsers.rst.directives.images import PIL
24 from docutils.utils.code_analyzer import with_pygments
25 from docutils.writers import html4css1
26
27 if with_pygments:
28 import pygments
29
30 if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 14):
31 # pygments output changed in version 2.14
32 with_pygments = False
33
34
35 # TEST_ROOT is ./test/ from the docutils root
36 TEST_ROOT = Path(__file__).parents[1]
37 DATA_ROOT = TEST_ROOT / 'data'
38 ROOT_PREFIX = (TEST_ROOT / 'functional/input').as_posix()
39
40 # Pillow/PIL is optional:
41 if PIL:
42 SCALING_OUTPUT = 'style="width: 32.0px; height: 32.0px;" '
43 else:
44 SCALING_OUTPUT = ''
45
46
47 class Html4WriterPublishPartsTestCase(unittest.TestCase):
48 """Test case for "html4css1" writer via the publish_parts() interface."""
49
50 maxDiff = None
51
52 def test_publish(self):
53 for name, (settings_overrides, cases) in totest.items():
54 if name == 'syntax_highlight' and not with_pygments:
55 self.skipTest('syntax highlight requires pygments')
56 for casenum, (case_input, case_expected) in enumerate(cases):
57 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
58 parts = docutils.core.publish_parts(
59 source=case_input,
60 writer=html4css1.Writer(),
61 settings_overrides={
62 '_disable_config': True,
63 'strict_visitor': True,
64 'stylesheet_path': '',
65 'section_self_link': True,
66 **settings_overrides,
67 }
68 )
69 self.assertEqual(case_expected, parts['body'])
70
71
72 totest = {}
73
74 totest['standard'] = ({}, [
75 ["""\
76 Simple String
77 """,
78 '<p>Simple String</p>\n',
79 ],
80 ["""\
81 Simple String with *markup*
82 """,
83 '<p>Simple String with <em>markup</em></p>\n',
84 ],
85 ["""\
86 Simple String with an even simpler ``inline literal``
87 """,
88 '<p>Simple String with an even simpler <tt class="docutils literal">inline literal</tt></p>\n',
89 ],
90 ["""\
91 A simple `anonymous reference`__
92
93 __ http://www.test.com/test_url
94 """,
95 '<p>A simple <a class="reference external" href="http://www.test.com/test_url">anonymous reference</a></p>\n',
96 ],
97 ["""\
98 One paragraph.
99
100 Two paragraphs.
101 """,
102 """\
103 <p>One paragraph.</p>
104 <p>Two paragraphs.</p>
105 """,
106 ],
107 ["""\
108 A simple `named reference`_ with stuff in between the
109 reference and the target.
110
111 .. _`named reference`: http://www.test.com/test_url
112 """,
113 """\
114 <p>A simple <a class="reference external" href="http://www.test.com/test_url">named reference</a> with stuff in between the
115 reference and the target.</p>
116 """,
117 ],
118 ["""\
119 .. [CIT2022] A citation.
120 """,
121 """\
122 <table class="docutils citation" frame="void" id="cit2022" rules="none">
123 <colgroup><col class="label" /><col /></colgroup>
124 <tbody valign="top">
125 <tr><td class="label">[CIT2022]</td><td>A citation.</td></tr>
126 </tbody>
127 </table>
128 """,
129 ],
130 ])
131
132
133 totest['no_title_promotion'] = ({'doctitle_xform': False}, [
134 ["""\
135 +++++
136 Title
137 +++++
138
139 Not A Subtitle
140 ==============
141
142 Some stuff
143
144 Section
145 -------
146
147 Some more stuff
148
149 Another Section
150 ...............
151
152 And even more stuff
153 """,
154 """\
155 <div class="section" id="title">
156 <h1>Title</h1>
157 <div class="section" id="not-a-subtitle">
158 <h2>Not A Subtitle</h2>
159 <p>Some stuff</p>
160 <div class="section" id="section">
161 <h3>Section</h3>
162 <p>Some more stuff</p>
163 <div class="section" id="another-section">
164 <h4>Another Section</h4>
165 <p>And even more stuff</p>
166 </div>
167 </div>
168 </div>
169 </div>
170 """,
171 ],
172 ["""\
173 * bullet
174 * list
175 """,
176 """\
177 <ul class="simple">
178 <li>bullet</li>
179 <li>list</li>
180 </ul>
181 """,
182 ],
183 ["""\
184 .. table::
185 :align: right
186 :width: 320
187
188 +-----+-----+
189 | 1 | 2 |
190 +-----+-----+
191 | 3 | 4 |
192 +-----+-----+
193 """,
194 """\
195 <table border="1" class="docutils align-right" style="width: 320px">
196 <colgroup>
197 <col width="50%" />
198 <col width="50%" />
199 </colgroup>
200 <tbody valign="top">
201 <tr><td>1</td>
202 <td>2</td>
203 </tr>
204 <tr><td>3</td>
205 <td>4</td>
206 </tr>
207 </tbody>
208 </table>
209 """,
210 ],
211 ["""\
212 Not a docinfo.
213
214 :This: .. _target:
215
216 is
217 :a:
218 :simple:
219 :field: list
220 """,
221 """\
222 <p>Not a docinfo.</p>
223 <table class="docutils field-list" frame="void" rules="none">
224 <col class="field-name" />
225 <col class="field-body" />
226 <tbody valign="top">
227 <tr class="field"><th class="field-name">This:</th><td class="field-body"><p class="first last" id="target">is</p>
228 </td>
229 </tr>
230 <tr class="field"><th class="field-name">a:</th><td class="field-body"></td>
231 </tr>
232 <tr class="field"><th class="field-name">simple:</th><td class="field-body"></td>
233 </tr>
234 <tr class="field"><th class="field-name">field:</th><td class="field-body">list</td>
235 </tr>
236 </tbody>
237 </table>
238 """,
239 ],
240 ["""\
241 Not a docinfo.
242
243 :This is: a
244 :simple field list with loooong field: names
245 """,
246 """\
247 <p>Not a docinfo.</p>
248 <table class="docutils field-list" frame="void" rules="none">
249 <col class="field-name" />
250 <col class="field-body" />
251 <tbody valign="top">
252 <tr class="field"><th class="field-name">This is:</th><td class="field-body">a</td>
253 </tr>
254 <tr class="field"><th class="field-name" colspan="2">simple field list with loooong field:</th></tr>
255 <tr class="field"><td>&nbsp;</td><td class="field-body">names</td>
256 </tr>
257 </tbody>
258 </table>
259 """,
260 ],
261 ["""\
262 Not a docinfo.
263
264 .. class:: field-indent-200
265
266 :This: is a
267 :simple: field list with custom indent.
268 """,
269 """\
270 <p>Not a docinfo.</p>
271 <table class="field-indent-200 docutils field-list" frame="void" rules="none">
272 <col class="field-name" />
273 <col class="field-body" />
274 <tbody valign="top">
275 <tr class="field"><th class="field-name">This:</th><td class="field-body">is a</td>
276 </tr>
277 <tr class="field"><th class="field-name">simple:</th><td class="field-body">field list with custom indent.</td>
278 </tr>
279 </tbody>
280 </table>
281 """,
282 ],
283 ["""\
284 Not a docinfo.
285
286 .. class:: field-indent-200uf
287
288 :This: is a
289 :simple: field list without custom indent,
290 because the unit "uf" is invalid.
291 """,
292 """\
293 <p>Not a docinfo.</p>
294 <table class="field-indent-200uf docutils field-list" frame="void" rules="none">
295 <col class="field-name" />
296 <col class="field-body" />
297 <tbody valign="top">
298 <tr class="field"><th class="field-name">This:</th><td class="field-body">is a</td>
299 </tr>
300 <tr class="field"><th class="field-name">simple:</th><td class="field-body">field list without custom indent,
301 because the unit &quot;uf&quot; is invalid.</td>
302 </tr>
303 </tbody>
304 </table>
305 """,
306 ],
307 ["""\
308 .. figure:: dummy.png
309 :figname: fig:dummy
310
311 The figure's caption.
312
313 A legend.
314
315 The legend's second paragraph.
316 """,
317 """\
318 <div class="figure" id="fig-dummy">
319 <img alt="dummy.png" src="dummy.png" />
320 <p class="caption">The figure's caption.</p>
321 <div class="legend">
322 <p>A legend.</p>
323 <p>The legend's second paragraph.</p>
324 </div>
325 </div>
326 """,
327 ],
328 ["""\
329 .. figure:: dummy.png
330
331 The figure's caption, no legend.
332 """,
333 """\
334 <div class="figure">
335 <img alt="dummy.png" src="dummy.png" />
336 <p class="caption">The figure's caption, no legend.</p>
337 </div>
338 """,
339 ],
340 ["""\
341 .. figure:: dummy.png
342
343 ..
344
345 A legend without caption.
346 """,
347 """\
348 <div class="figure">
349 <img alt="dummy.png" src="dummy.png" />
350 <div class="legend">
351 A legend without caption.</div>
352 </div>
353 """,
354 ],
355 ["""\
356 .. figure:: dummy.png
357
358 No caption nor legend.
359 """,
360 """\
361 <div class="figure">
362 <img alt="dummy.png" src="dummy.png" />
363 </div>
364 <p>No caption nor legend.</p>
365 """,
366 ],
367 [f"""\
368 .. include:: {DATA_ROOT}/multiple-term-definition.xml
369 :parser: xml
370 """,
371 """\
372 <dl class="docutils">
373 <dt>New in Docutils 0.22</dt>
374 <dd><p class="first">A definition list item may contain several
375 terms with optional classifier(s).</p>
376 <p class="last">However, there is currently no corresponding
377 reStructuredText syntax.</p>
378 </dd>
379 <dt>term 2a</dt>
380 <dt>term 2b</dt>
381 <dd>definition 2</dd>
382 <dt>term 3a <span class="classifier-delimiter">:</span> <span class="classifier">classifier 3a</span> <span class="classifier-delimiter">:</span> <span class="classifier">classifier 3aa</span><dt>term 3b <span class="classifier-delimiter">:</span> <span class="classifier">classifier 3b</span></dt>
383 <dd>definition 3</dd>
384 </dl>
385 """,
386 ],
387 ])
388
389
390 totest['lazy_loading'] = ({'image_loading': 'lazy',
391 'report_level': 4}, [
392 ["""\
393 Lazy loading by default, overridden by :loading: option
394 ("cannot embed" warning ignored).
395
396 .. image:: dummy.png
397 .. image:: dummy.png
398 :loading: link
399 .. figure:: dummy.png
400 .. figure:: dummy.png
401 :loading: embed
402 """,
403 """\
404 <p>Lazy loading by default, overridden by :loading: option
405 (&quot;cannot embed&quot; warning ignored).</p>
406 <img alt="dummy.png" src="dummy.png" />
407 <img alt="dummy.png" src="dummy.png" />
408 <div class="figure">
409 <img alt="dummy.png" src="dummy.png" />
410 </div>
411 <div class="figure">
412 <img alt="dummy.png" src="dummy.png" />
413 </div>
414 """,
415 ],
416 ])
417
418
419 totest['root_prefix'] = ({'root_prefix': ROOT_PREFIX,
420 'image_loading': 'embed',
421 'warning_stream': '',
422 }, [
423 ["""\
424 .. image:: /data/blue%20square.png
425 :scale: 100%
426 .. figure:: /data/blue%20square.png
427 """,
428 f"""\
429 <img alt="/data/blue%20square.png" src="/data/blue%20square.png" {SCALING_OUTPUT}/>
430 <div class="figure">
431 <img alt="/data/blue%20square.png" src="/data/blue%20square.png" />
432 </div>
433 """
434 ],
435 ])
436
437
438 totest['no_backlinks'] = ({'footnote_backlinks': False}, [
439
440 ["""\
441 Two footnotes [#f1]_ [#f2]_ and two citations [once]_ [twice]_.
442
443 The latter are referenced a second time [#f2]_ [twice]_.
444
445 .. [#f1] referenced once
446 .. [#f2] referenced twice
447 .. [once] citation referenced once
448 .. [twice] citation referenced twice
449 """,
450 """\
451 <p>Two footnotes <a class="footnote-reference" href="#f1" id="footnote-reference-1">[1]</a> <a class="footnote-reference" href="#f2" id="footnote-reference-2">[2]</a> and two citations <a class="citation-reference" href="#once" id="citation-reference-1">[once]</a> <a class="citation-reference" href="#twice" id="citation-reference-2">[twice]</a>.</p>
452 <p>The latter are referenced a second time <a class="footnote-reference" href="#f2" id="footnote-reference-3">[2]</a> <a class="citation-reference" href="#twice" id="citation-reference-3">[twice]</a>.</p>
453 <table class="docutils footnote" frame="void" id="f1" rules="none">
454 <colgroup><col class="label" /><col /></colgroup>
455 <tbody valign="top">
456 <tr><td class="label">[1]</td><td>referenced once</td></tr>
457 </tbody>
458 </table>
459 <table class="docutils footnote" frame="void" id="f2" rules="none">
460 <colgroup><col class="label" /><col /></colgroup>
461 <tbody valign="top">
462 <tr><td class="label">[2]</td><td>referenced twice</td></tr>
463 </tbody>
464 </table>
465 <table class="docutils citation" frame="void" id="once" rules="none">
466 <colgroup><col class="label" /><col /></colgroup>
467 <tbody valign="top">
468 <tr><td class="label">[once]</td><td>citation referenced once</td></tr>
469 </tbody>
470 </table>
471 <table class="docutils citation" frame="void" id="twice" rules="none">
472 <colgroup><col class="label" /><col /></colgroup>
473 <tbody valign="top">
474 <tr><td class="label">[twice]</td><td>citation referenced twice</td></tr>
475 </tbody>
476 </table>
477 """,
478 ],
479 ])
480
481
482 totest['syntax_highlight'] = ({'syntax_highlight': 'short',
483 }, [
484 ["""\
485 .. code:: shell
486
487 cat <<EOF
488 Hello World
489 EOF
490 """,
491 """\
492 <pre class="code shell literal-block">
493 cat <span class="s">&lt;&lt;EOF
494 Hello World
495 EOF</span>
496 </pre>
497 """,
498 ],
499 ["""\
500 .. role:: shell(code)
501 :language: shell
502
503 :shell:`cat <<EOF Hello World EOF`
504 """,
505 """\
506 <p><code class="shell">cat <span class="s">&lt;&lt;EOF Hello World EOF</span></code></p>
507 """,
508 ],
509 ])
510
511
512 totest['system_messages'] = ({'math_output': 'mathml',
513 'warning_stream': '',
514 }, [
515 # No warning with HTML4 ("embed" error is silently ignored).
516 ["""\
517 .. image:: https://dummy.png
518 :loading: embed
519 """,
520 """\
521 <img alt="https://dummy.png" src="https://dummy.png" />
522 """,
523 ],
524 # No error with HTML4 (silently ignored)
525 [f"""\
526 .. image:: {DATA_ROOT.as_uri()}/circle-broken.svg
527 :loading: embed
528 """,
529 f"""\
530 <object data="{DATA_ROOT.as_uri()}/circle-broken.svg" type="image/svg+xml">{DATA_ROOT.as_uri()}/circle-broken.svg</object>
531 """
532 ],
533 [r"""Broken :math:`\sin \my`.
534 """,
535 """\
536 <p>Broken <span class="math problematic">\\sin \\my</span>.</p>
537 <div class="system-message">
538 <p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>
539 Unknown LaTeX command &quot;\\my&quot;.</div>
540 """],
541 ])
542
543 totest['system_messages-PIL'] = ({'math_output': 'mathml',
544 'warning_stream': '',
545 }, [
546 ["""\
547 .. image:: dummy.png
548 :scale: 100%
549 :loading: embed
550 """,
551 """\
552 <img alt="dummy.png" src="dummy.png" />
553 """,
554 ],
555 ["""\
556 .. image:: dummy.mp4
557 :scale: 100%
558 """,
559 """\
560 <object data="dummy.mp4" type="video/mp4">dummy.mp4</object>
561 """,
562 ],
563 ["""\
564 .. image:: https://dummy.png
565 :scale: 100%
566 :loading: embed
567 """,
568 """\
569 <img alt="https://dummy.png" src="https://dummy.png" />
570 """,
571 ],
572 ])
573
574 totest['no_system_messages'] = ({'math_output': 'mathml',
575 'report_level': 4,
576 'warning_stream': '',
577 }, [
578 ["""\
579 .. image:: dummy.png
580 :scale: 100%
581 :loading: embed
582
583 .. image:: dummy.mp4
584 :scale: 100%
585 """,
586 """\
587 <img alt="dummy.png" src="dummy.png" />
588 <object data="dummy.mp4" type="video/mp4">dummy.mp4</object>
589 """,
590 ],
591 [f"""\
592 .. image:: {DATA_ROOT.as_uri()}/circle-broken.svg
593 :loading: embed
594 """,
595 f"""\
596 <object data="{DATA_ROOT.as_uri()}/circle-broken.svg" type="image/svg+xml">{DATA_ROOT.as_uri()}/circle-broken.svg</object>
597 """],
598 [r'Broken :math:`\sin \my`.',
599 '<p>Broken <tt class="math">\\sin \\my</tt>.</p>\n'
600 ],
601 ])
602
603
604 if __name__ == '__main__':
605 unittest.main()