Debian Codegraph @

test/test_writers/test_html4css1_template.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # $Id: test_html4css1_template.py 9914 2024-08-21 13:45:08Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for the HTML writer.
9 """
10
11 from pathlib import Path
12 import os
13 import platform
14 import sys
15 import unittest
16
17 if __name__ == '__main__':
18 # prepend the "docutils root" to the Python library path
19 # so we import the local `docutils` package.
20 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
21
22 import docutils
23 from docutils.core import publish_string
24 from docutils.writers import html4css1
25
26 # TEST_ROOT is ./test/ from the docutils root
27 TEST_ROOT = Path(__file__).parents[1]
28
29
30 class WriterPublishTestCase(unittest.TestCase):
31 # maxDiff = None
32 def test_publish(self):
33 template_path = TEST_ROOT / 'data/full-template.txt'
34 for name, cases in totest.items():
35 for casenum, (case_input, case_expected) in enumerate(cases):
36 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
37 output = publish_string(
38 source=case_input,
39 writer=html4css1.Writer(),
40 settings_overrides={
41 '_disable_config': True,
42 'strict_visitor': True,
43 'template': template_path,
44 'stylesheet_path': '/test.css',
45 'embed_stylesheet': False,
46 }).decode()
47 self.assertEqual(case_expected, output)
48
49
50 if platform.system() == "Windows":
51 drive_prefix = os.path.splitdrive(os.getcwd())[0]
52 else:
53 drive_prefix = ""
54
55
56 totest = {}
57
58 totest['template'] = [
59 ["""\
60 ================
61 Document Title
62 ================
63 ----------
64 Subtitle
65 ----------
66
67 :Author: Me
68
69 .. footer:: footer text
70
71 Section
72 =======
73
74 Some text.
75 """,
76 fr'''head_prefix = """\
77 <?xml version="1.0" encoding="utf-8"?>
78 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
79 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
80 <head>"""
81
82
83 head = """\
84 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
85 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
86 <meta name="author" content="Me" />
87 <title>Document Title</title>"""
88
89
90 stylesheet = """\
91 <link rel="stylesheet" href="{drive_prefix}/test.css" type="text/css" />"""
92
93
94 body_prefix = """\
95 </head>
96 <body>
97 <div class="document" id="document-title">"""
98
99
100 body_pre_docinfo = """\
101 <h1 class="title">Document Title</h1>
102 <h2 class="subtitle" id="subtitle">Subtitle</h2>"""
103
104
105 docinfo = """\
106 <table class="docinfo" frame="void" rules="none">
107 <col class="docinfo-name" />
108 <col class="docinfo-content" />
109 <tbody valign="top">
110 <tr><th class="docinfo-name">Author:</th>
111 <td>Me</td></tr>
112 </tbody>
113 </table>"""
114
115
116 body = """\
117 <div class="section" id="section">
118 <h1>Section</h1>
119 <p>Some text.</p>
120 </div>"""
121
122
123 body_suffix = """\
124 </div>
125 <div class="footer">
126 <hr class="footer" />
127 footer text
128 </div>
129 </body>
130 </html>"""
131
132
133 head_prefix = """\
134 <?xml version="1.0" encoding="utf-8"?>
135 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
136 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
137 <head>"""
138
139
140 head = """\
141 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
142 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
143 <meta name="author" content="Me" />
144 <title>Document Title</title>"""
145
146
147 stylesheet = """\
148 <link rel="stylesheet" href="{drive_prefix}/test.css" type="text/css" />"""
149
150
151 body_prefix = """\
152 </head>
153 <body>
154 <div class="document" id="document-title">"""
155
156
157 body_pre_docinfo = """\
158 <h1 class="title">Document Title</h1>
159 <h2 class="subtitle" id="subtitle">Subtitle</h2>"""
160
161
162 docinfo = """\
163 <table class="docinfo" frame="void" rules="none">
164 <col class="docinfo-name" />
165 <col class="docinfo-content" />
166 <tbody valign="top">
167 <tr><th class="docinfo-name">Author:</th>
168 <td>Me</td></tr>
169 </tbody>
170 </table>"""
171
172
173 body = """\
174 <div class="section" id="section">
175 <h1>Section</h1>
176 <p>Some text.</p>
177 </div>"""
178
179
180 body_suffix = """\
181 </div>
182 <div class="footer">
183 <hr class="footer" />
184 footer text
185 </div>
186 </body>
187 </html>"""
188
189
190 title = """\
191 Document Title"""
192
193
194 subtitle = """\
195 Subtitle"""
196
197
198 header = """\
199 """
200
201
202 footer = """\
203 <div class="footer">
204 <hr class="footer" />
205 footer text
206 </div>"""
207
208
209 meta = """\
210 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
211 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
212 <meta name="author" content="Me" />"""
213
214
215 fragment = """\
216 <div class="section" id="section">
217 <h1>Section</h1>
218 <p>Some text.</p>
219 </div>"""
220
221
222 html_prolog = """\
223 <?xml version="1.0" encoding="%s"?>
224 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"""
225
226
227 html_head = """\
228 <meta http-equiv="Content-Type" content="text/html; charset=%s" />
229 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
230 <meta name="author" content="Me" />
231 <title>Document Title</title>"""
232
233
234 html_title = """\
235 <h1 class="title">Document Title</h1>"""
236
237
238 html_subtitle = """\
239 <h2 class="subtitle" id="subtitle">Subtitle</h2>"""
240
241
242 html_body = """\
243 <div class="document" id="document-title">
244 <h1 class="title">Document Title</h1>
245 <h2 class="subtitle" id="subtitle">Subtitle</h2>
246 <table class="docinfo" frame="void" rules="none">
247 <col class="docinfo-name" />
248 <col class="docinfo-content" />
249 <tbody valign="top">
250 <tr><th class="docinfo-name">Author:</th>
251 <td>Me</td></tr>
252 </tbody>
253 </table>
254 <div class="section" id="section">
255 <h1>Section</h1>
256 <p>Some text.</p>
257 </div>
258 </div>
259 <div class="footer">
260 <hr class="footer" />
261 footer text
262 </div>"""
263 ''']
264 ]
265
266 if __name__ == '__main__':
267 unittest.main()