File: test_html4css1.py

package info (click to toggle)
python-docutils 0.22%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 11,448 kB
  • sloc: python: 53,302; lisp: 14,475; xml: 1,807; javascript: 1,032; makefile: 102; sh: 96
file content (605 lines) | stat: -rw-r--r-- 14,252 bytes parent folder | download
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
#! /usr/bin/env python3

# $Id: test_html4css1.py 10102 2025-04-23 15:54:44Z milde $
# Author: reggie dugard <reggie@users.sourceforge.net>
# Copyright: This module has been placed in the public domain.

"""Test HTML4 writer output ("fragment" part).

This is the document body (not HTML <body>).
"""

from pathlib import Path
import sys
import unittest

if __name__ == '__main__':
    # prepend the "docutils root" to the Python library path
    # so we import the local `docutils` package.
    sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

import docutils
import docutils.core
from docutils.parsers.rst.directives.images import PIL
from docutils.utils.code_analyzer import with_pygments
from docutils.writers import html4css1

if with_pygments:
    import pygments

    if tuple(map(int, pygments.__version__.split('.')[:2])) >= (2, 14):
        # pygments output changed in version 2.14
        with_pygments = False


# TEST_ROOT is ./test/ from the docutils root
TEST_ROOT = Path(__file__).parents[1]
DATA_ROOT = TEST_ROOT / 'data'
ROOT_PREFIX = (TEST_ROOT / 'functional/input').as_posix()

# Pillow/PIL is optional:
if PIL:
    SCALING_OUTPUT = 'style="width: 32.0px; height: 32.0px;" '
else:
    SCALING_OUTPUT = ''


class Html4WriterPublishPartsTestCase(unittest.TestCase):
    """Test case for "html4css1" writer via the publish_parts() interface."""

    maxDiff = None

    def test_publish(self):
        for name, (settings_overrides, cases) in totest.items():
            if name == 'syntax_highlight' and not with_pygments:
                self.skipTest('syntax highlight requires pygments')
            for casenum, (case_input, case_expected) in enumerate(cases):
                with self.subTest(id=f'totest[{name!r}][{casenum}]'):
                    parts = docutils.core.publish_parts(
                        source=case_input,
                        writer=html4css1.Writer(),
                        settings_overrides={
                            '_disable_config': True,
                            'strict_visitor': True,
                            'stylesheet_path': '',
                            'section_self_link': True,
                            **settings_overrides,
                        }
                    )
                    self.assertEqual(case_expected, parts['body'])


totest = {}

totest['standard'] = ({}, [
["""\
Simple String
""",
'<p>Simple String</p>\n',
],
["""\
Simple String with *markup*
""",
'<p>Simple String with <em>markup</em></p>\n',
],
["""\
Simple String with an even simpler ``inline literal``
""",
'<p>Simple String with an even simpler <tt class="docutils literal">inline literal</tt></p>\n',
],
["""\
A simple `anonymous reference`__

__ http://www.test.com/test_url
""",
'<p>A simple <a class="reference external" href="http://www.test.com/test_url">anonymous reference</a></p>\n',
],
["""\
One paragraph.

Two paragraphs.
""",
"""\
<p>One paragraph.</p>
<p>Two paragraphs.</p>
""",
],
["""\
A simple `named reference`_ with stuff in between the
reference and the target.

.. _`named reference`: http://www.test.com/test_url
""",
"""\
<p>A simple <a class="reference external" href="http://www.test.com/test_url">named reference</a> with stuff in between the
reference and the target.</p>
""",
],
["""\
.. [CIT2022] A citation.
""",
"""\
<table class="docutils citation" frame="void" id="cit2022" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[CIT2022]</td><td>A citation.</td></tr>
</tbody>
</table>
""",
],
])


totest['no_title_promotion'] = ({'doctitle_xform': False}, [
["""\
+++++
Title
+++++

Not A Subtitle
==============

Some stuff

Section
-------

Some more stuff

Another Section
...............

And even more stuff
""",
"""\
<div class="section" id="title">
<h1>Title</h1>
<div class="section" id="not-a-subtitle">
<h2>Not A Subtitle</h2>
<p>Some stuff</p>
<div class="section" id="section">
<h3>Section</h3>
<p>Some more stuff</p>
<div class="section" id="another-section">
<h4>Another Section</h4>
<p>And even more stuff</p>
</div>
</div>
</div>
</div>
""",
],
["""\
* bullet
* list
""",
"""\
<ul class="simple">
<li>bullet</li>
<li>list</li>
</ul>
""",
],
["""\
.. table::
   :align: right
   :width: 320

   +-----+-----+
   |  1  |  2  |
   +-----+-----+
   |  3  |  4  |
   +-----+-----+
""",
"""\
<table border="1" class="docutils align-right" style="width: 320px">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<tbody valign="top">
<tr><td>1</td>
<td>2</td>
</tr>
<tr><td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
""",
],
["""\
Not a docinfo.

:This: .. _target:

       is
:a:
:simple:
:field: list
""",
"""\
<p>Not a docinfo.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">This:</th><td class="field-body"><p class="first last" id="target">is</p>
</td>
</tr>
<tr class="field"><th class="field-name">a:</th><td class="field-body"></td>
</tr>
<tr class="field"><th class="field-name">simple:</th><td class="field-body"></td>
</tr>
<tr class="field"><th class="field-name">field:</th><td class="field-body">list</td>
</tr>
</tbody>
</table>
""",
],
["""\
Not a docinfo.

:This is: a
:simple field list with loooong field: names
""",
"""\
<p>Not a docinfo.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">This is:</th><td class="field-body">a</td>
</tr>
<tr class="field"><th class="field-name" colspan="2">simple field list with loooong field:</th></tr>
<tr class="field"><td>&nbsp;</td><td class="field-body">names</td>
</tr>
</tbody>
</table>
""",
],
["""\
Not a docinfo.

.. class:: field-indent-200

:This: is a
:simple: field list with custom indent.
""",
"""\
<p>Not a docinfo.</p>
<table class="field-indent-200 docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">This:</th><td class="field-body">is a</td>
</tr>
<tr class="field"><th class="field-name">simple:</th><td class="field-body">field list with custom indent.</td>
</tr>
</tbody>
</table>
""",
],
["""\
Not a docinfo.

.. class:: field-indent-200uf

:This: is a
:simple: field list without custom indent,
         because the unit "uf" is invalid.
""",
"""\
<p>Not a docinfo.</p>
<table class="field-indent-200uf docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">This:</th><td class="field-body">is a</td>
</tr>
<tr class="field"><th class="field-name">simple:</th><td class="field-body">field list without custom indent,
because the unit &quot;uf&quot; is invalid.</td>
</tr>
</tbody>
</table>
""",
],
["""\
.. figure:: dummy.png
   :figname: fig:dummy

   The figure's caption.

   A legend.

   The legend's second paragraph.
""",
"""\
<div class="figure" id="fig-dummy">
<img alt="dummy.png" src="dummy.png" />
<p class="caption">The figure's caption.</p>
<div class="legend">
<p>A legend.</p>
<p>The legend's second paragraph.</p>
</div>
</div>
""",
],
["""\
.. figure:: dummy.png

   The figure's caption, no legend.
""",
"""\
<div class="figure">
<img alt="dummy.png" src="dummy.png" />
<p class="caption">The figure's caption, no legend.</p>
</div>
""",
],
["""\
.. figure:: dummy.png

   ..

   A legend without caption.
""",
"""\
<div class="figure">
<img alt="dummy.png" src="dummy.png" />
<div class="legend">
A legend without caption.</div>
</div>
""",
],
["""\
.. figure:: dummy.png

No caption nor legend.
""",
"""\
<div class="figure">
<img alt="dummy.png" src="dummy.png" />
</div>
<p>No caption nor legend.</p>
""",
],
[f"""\
.. include:: {DATA_ROOT}/multiple-term-definition.xml
   :parser: xml
""",
"""\
<dl class="docutils">
<dt>New in Docutils 0.22</dt>
<dd><p class="first">A definition list item may contain several
terms with optional classifier(s).</p>
<p class="last">However, there is currently no corresponding
reStructuredText syntax.</p>
</dd>
<dt>term 2a</dt>
<dt>term 2b</dt>
<dd>definition 2</dd>
<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>
<dd>definition 3</dd>
</dl>
""",
],
])


totest['lazy_loading'] = ({'image_loading': 'lazy',
                           'report_level': 4}, [
["""\
Lazy loading by default, overridden by :loading: option
("cannot embed" warning ignored).

.. image:: dummy.png
.. image:: dummy.png
   :loading: link
.. figure:: dummy.png
.. figure:: dummy.png
   :loading: embed
""",
"""\
<p>Lazy loading by default, overridden by :loading: option
(&quot;cannot embed&quot; warning ignored).</p>
<img alt="dummy.png" src="dummy.png" />
<img alt="dummy.png" src="dummy.png" />
<div class="figure">
<img alt="dummy.png" src="dummy.png" />
</div>
<div class="figure">
<img alt="dummy.png" src="dummy.png" />
</div>
""",
],
])


totest['root_prefix'] = ({'root_prefix': ROOT_PREFIX,
                          'image_loading': 'embed',
                          'warning_stream': '',
                          }, [
["""\
.. image:: /data/blue%20square.png
   :scale: 100%
.. figure:: /data/blue%20square.png
""",
f"""\
<img alt="/data/blue%20square.png" src="/data/blue%20square.png" {SCALING_OUTPUT}/>
<div class="figure">
<img alt="/data/blue%20square.png" src="/data/blue%20square.png" />
</div>
"""
],
])


totest['no_backlinks'] = ({'footnote_backlinks': False}, [

["""\
Two footnotes [#f1]_ [#f2]_ and two citations [once]_ [twice]_.

The latter are referenced a second time [#f2]_ [twice]_.

.. [#f1] referenced once
.. [#f2] referenced twice
.. [once] citation referenced once
.. [twice] citation referenced twice
""",
"""\
<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>
<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>
<table class="docutils footnote" frame="void" id="f1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td>referenced once</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[2]</td><td>referenced twice</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="once" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[once]</td><td>citation referenced once</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="twice" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[twice]</td><td>citation referenced twice</td></tr>
</tbody>
</table>
""",
],
])


totest['syntax_highlight'] = ({'syntax_highlight': 'short',
                               }, [
["""\
.. code:: shell

    cat <<EOF
    Hello World
    EOF
""",
"""\
<pre class="code shell literal-block">
cat <span class="s">&lt;&lt;EOF
Hello World
EOF</span>
</pre>
""",
],
["""\
.. role:: shell(code)
   :language: shell

:shell:`cat <<EOF Hello World EOF`
""",
"""\
<p><code class="shell">cat <span class="s">&lt;&lt;EOF Hello World EOF</span></code></p>
""",
],
])


totest['system_messages'] = ({'math_output': 'mathml',
                              'warning_stream': '',
                              }, [
# No warning with HTML4 ("embed" error is silently ignored).
["""\
.. image:: https://dummy.png
   :loading: embed
""",
"""\
<img alt="https://dummy.png" src="https://dummy.png" />
""",
],
# No error with HTML4 (silently ignored)
[f"""\
.. image:: {DATA_ROOT.as_uri()}/circle-broken.svg
   :loading: embed
""",
f"""\
<object data="{DATA_ROOT.as_uri()}/circle-broken.svg" type="image/svg+xml">{DATA_ROOT.as_uri()}/circle-broken.svg</object>
"""
],
[r"""Broken :math:`\sin \my`.
""",
"""\
<p>Broken <span class="math problematic">\\sin \\my</span>.</p>
<div class="system-message">
<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>
Unknown LaTeX command &quot;\\my&quot;.</div>
"""],
])

totest['system_messages-PIL'] = ({'math_output': 'mathml',
                                  'warning_stream': '',
                                  }, [
["""\
.. image:: dummy.png
   :scale: 100%
   :loading: embed
""",
"""\
<img alt="dummy.png" src="dummy.png" />
""",
],
["""\
.. image:: dummy.mp4
   :scale: 100%
""",
"""\
<object data="dummy.mp4" type="video/mp4">dummy.mp4</object>
""",
],
["""\
.. image:: https://dummy.png
   :scale: 100%
   :loading: embed
""",
"""\
<img alt="https://dummy.png" src="https://dummy.png" />
""",
],
])

totest['no_system_messages'] = ({'math_output': 'mathml',
                                 'report_level': 4,
                                 'warning_stream': '',
                                 }, [
["""\
.. image:: dummy.png
   :scale: 100%
   :loading: embed

.. image:: dummy.mp4
   :scale: 100%
""",
"""\
<img alt="dummy.png" src="dummy.png" />
<object data="dummy.mp4" type="video/mp4">dummy.mp4</object>
""",
],
[f"""\
.. image:: {DATA_ROOT.as_uri()}/circle-broken.svg
   :loading: embed
""",
f"""\
<object data="{DATA_ROOT.as_uri()}/circle-broken.svg" type="image/svg+xml">{DATA_ROOT.as_uri()}/circle-broken.svg</object>
"""],
[r'Broken :math:`\sin \my`.',
'<p>Broken <tt class="math">\\sin \\my</tt>.</p>\n'
],
])


if __name__ == '__main__':
    unittest.main()