Debian Codegraph @

test/test_writers/test_html4css1_parts.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_html4css1_parts.py 10005 2024-12-18 22:03:27Z milde $
4 # Author: reggie dugard <[email protected]>
5 # Maintainer: [email protected]
6 # Copyright: This module has been placed in the public domain.
7
8 """
9 Test `core.publish_parts()`__ with the html4css1 writer.
10
11 __ https://docutils.sourceforge.io/docs/api/publisher.html#publish-parts
12 """
13
14 from pathlib import Path
15 import sys
16 import unittest
17
18 if __name__ == '__main__':
19 # prepend the "docutils root" to the Python library path
20 # so we import the local `docutils` package.
21 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
22
23 import docutils
24 import docutils.core
25 from docutils.writers import html4css1
26
27 # TEST_ROOT is ./test/ from the docutils root
28 TEST_ROOT = Path(__file__).parents[1]
29 DATA_ROOT = TEST_ROOT / 'data'
30 ROOT_PREFIX = (TEST_ROOT / 'functional/input').as_posix()
31
32
33 # Parts returned by `publish_parts()` for the HTML5 writer by default:
34 # * empty input string
35 # * default configuration settings.
36 # See format_parts() below for the substitution of unresolved format markers.
37 default_parts = {
38 'body': '{fragment}',
39 'body_pre_docinfo': '',
40 'body_prefix': '</head>\n<body>\n{header}<div class="document">\n',
41 'body_suffix': '</div>\n{footer}</body>\n</html>\n',
42 'docinfo': '',
43 'encoding': 'utf-8',
44 'errors': 'xmlcharrefreplace',
45 'footer': '',
46 'fragment': '',
47 'head': '{meta}<title>{metatitle}</title>\n',
48 'head_prefix':
49 '<?xml version="1.0" encoding="utf-8"?>\n'
50 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
51 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'
52 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'
53 '<head>\n',
54 'header': '',
55 'html_body': '{header}<div class="document">\n{fragment}</div>\n{footer}',
56 'html_head': '{meta}<title>{metatitle}</title>\n',
57 'html_prolog':
58 '<?xml version="1.0" encoding="%s"?>\n'
59 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
60 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n',
61 'html_subtitle': '',
62 'html_title': '',
63 'meta': '<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n'
64 f'<meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />\n',
65 'stylesheet': '',
66 'subtitle': '',
67 'title': '',
68 'version': f'{docutils.__version__}',
69 'whole':
70 '{head_prefix}\n'
71 '{head}\n'
72 '{stylesheet}\n'
73 '{body_prefix}\n'
74 '{body_pre_docinfo}\n'
75 '{docinfo}\n'
76 '{body}\n'
77 '{body_suffix}\n',
78 }
79
80
81 def format_parts(parts):
82 # fill in part values that depend on other parts
83 # https://docutils.sourceforge.io/docs/api/publisher.html#html4-writer
84
85 # metadata title: either the visible title or document['source']
86 metatitle = parts['title'] or '&lt;string&gt;'
87
88 # "html_head" leaves the encoding unresolved, as "%s":
89 parts['html_head'] = parts['html_head'].format(metatitle=metatitle,
90 **parts)
91 # now resolve encoding:
92 try:
93 parts['meta'] = parts['meta'] % parts['encoding']
94 except TypeError: # charset-<meta> missing if encoding is 'unicode'
95 pass
96 parts['head'] = parts['head'].format(metatitle=metatitle, **parts)
97 parts['body_prefix'] = parts['body_prefix'].format(**parts)
98 parts['body'] = parts['body'].format(**parts)
99 parts['body_suffix'] = parts['body_suffix'].format(**parts)
100 parts['html_body'] = parts['html_body'].format(**parts)
101 # newlines are stripped when parts are used to expand the template file
102 parts['whole'] = parts['whole'].format(**{k: v.rstrip('\n')
103 for k, v in parts.items()})
104 return parts
105
106
107 class Html4WriterPublishPartsTestCase(unittest.TestCase):
108 """Test "html4css1" writer `publish_parts()` interface."""
109
110 maxDiff = None
111
112 def test_publish_parts(self):
113 for name, (settings_overrides, cases) in totest.items():
114 for casenum, (case_input, expected_parts) in enumerate(cases):
115 _stgns = {'_disable_config': True,
116 'strict_visitor': True,
117 'stylesheet_path': '',
118 'section_self_link': True,
119 **settings_overrides,
120 }
121 parts = docutils.core.publish_parts(source=case_input,
122 writer=html4css1.Writer(),
123 settings_overrides=_stgns,
124 )
125 expected = format_parts(default_parts | expected_parts)
126 for key in parts.keys():
127 with self.subTest(id=f'totest[{name!r}][{casenum}][{key}]'):
128 self.assertEqual(f'{expected[key]}',
129 f'{parts[key]}')
130
131
132 totest = {}
133
134 totest['standard'] = ({}, [
135 ['', # empty input string
136 {} # results in default parts
137 ],
138 ['Simple String with *markup*',
139 {'fragment': '<p>Simple String with <em>markup</em></p>\n'}
140 ],
141 ['.. header:: custom document header\n\n'
142 'A paragraph.',
143 {'header': '<div class="header">\ncustom document header\n'
144 '<hr class="header"/>\n</div>\n',
145 'body_prefix': '</head>\n'
146 '<body>\n'
147 '<div class="header">\n'
148 'custom document header\n'
149 '<hr class="header"/>\n'
150 '</div>\n'
151 '<div class="document">\n',
152 'fragment': '<p>A paragraph.</p>\n',
153 }
154 ],
155 ['.. footer:: custom document footer\n\n'
156 'A paragraph.',
157 {'footer': '<div class="footer">\n'
158 '<hr class="footer" />\n'
159 'custom document footer\n</div>\n',
160 'fragment': '<p>A paragraph.</p>\n',
161 }
162 ],
163 ["""\
164 +++++
165 Title
166 +++++
167
168 Subtitle
169 ========
170
171 Some stuff
172
173 Section
174 -------
175
176 Some more stuff
177
178 Another Section
179 ...............
180
181 And even more stuff
182 """,
183 {'body_pre_docinfo': '<h1 class="title">Title</h1>\n'
184 '<h2 class="subtitle" id="subtitle">Subtitle</h2>\n',
185 'body_prefix': '</head>\n<body>\n<div class="document" id="title">\n',
186 'fragment': """\
187 <p>Some stuff</p>
188 <div class="section" id="section">
189 <h1>Section</h1>
190 <p>Some more stuff</p>
191 <div class="section" id="another-section">
192 <h2>Another Section</h2>
193 <p>And even more stuff</p>
194 </div>
195 </div>
196 """,
197 'html_body': """\
198 <div class="document" id="title">
199 <h1 class="title">Title</h1>
200 <h2 class="subtitle" id="subtitle">Subtitle</h2>
201 <p>Some stuff</p>
202 <div class="section" id="section">
203 <h1>Section</h1>
204 <p>Some more stuff</p>
205 <div class="section" id="another-section">
206 <h2>Another Section</h2>
207 <p>And even more stuff</p>
208 </div>
209 </div>
210 </div>
211 """,
212 'html_subtitle': '<h2 class="subtitle" id="subtitle">Subtitle</h2>\n',
213 'html_title': '<h1 class="title">Title</h1>\n',
214 'subtitle': 'Subtitle',
215 'title': 'Title'
216 }],
217 ["""\
218 +++++
219 Title
220 +++++
221
222 :author: me
223
224 Some stuff
225 """,
226 {'body_pre_docinfo': '<h1 class="title">Title</h1>\n',
227 'body_prefix': '</head>\n<body>\n<div class="document" id="title">\n',
228 'docinfo': """\
229 <table class="docinfo" frame="void" rules="none">
230 <col class="docinfo-name" />
231 <col class="docinfo-content" />
232 <tbody valign="top">
233 <tr><th class="docinfo-name">Author:</th>
234 <td>me</td></tr>
235 </tbody>
236 </table>
237 """,
238 'fragment': '<p>Some stuff</p>\n',
239 'html_body': """\
240 <div class="document" id="title">
241 <h1 class="title">Title</h1>
242 <table class="docinfo" frame="void" rules="none">
243 <col class="docinfo-name" />
244 <col class="docinfo-content" />
245 <tbody valign="top">
246 <tr><th class="docinfo-name">Author:</th>
247 <td>me</td></tr>
248 </tbody>
249 </table>
250 <p>Some stuff</p>
251 </div>
252 """,
253 'html_title': '<h1 class="title">Title</h1>\n',
254 'meta': default_parts['meta'] + '<meta name="author" content="me" />\n',
255 'title': 'Title'
256 }],
257 ])
258
259 totest['no_title_promotion'] = ({'doctitle_xform': False}, [
260 ["""\
261 +++++
262 Title
263 +++++
264
265 Not A Subtitle
266 ==============
267
268 Some stuff
269
270 Section
271 -------
272
273 Some more stuff
274
275 Another Section
276 ...............
277
278 And even more stuff
279 """,
280 {'fragment': """\
281 <div class="section" id="title">
282 <h1>Title</h1>
283 <div class="section" id="not-a-subtitle">
284 <h2>Not A Subtitle</h2>
285 <p>Some stuff</p>
286 <div class="section" id="section">
287 <h3>Section</h3>
288 <p>Some more stuff</p>
289 <div class="section" id="another-section">
290 <h4>Another Section</h4>
291 <p>And even more stuff</p>
292 </div>
293 </div>
294 </div>
295 </div>
296 """},
297 ],
298 ])
299
300 totest['unknown-encoding'] = ({'output_encoding': 'unicode'}, [
301 ['Simple String\n',
302 {'encoding': 'unicode',
303 'fragment': '<p>Simple String</p>\n',
304 'head_prefix':
305 '<?xml version="1.0"?>\n'
306 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
307 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'
308 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'
309 '<head>\n',
310 'html_head': f'{default_parts["meta"]}<title>{{metatitle}}</title>\n',
311 'meta': f'<meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />\n',
312 }],
313 ])
314
315
316 if __name__ == '__main__':
317 unittest.main()