Debian Codegraph @

test/test_writers/test_html5_template.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # $Id: test_html5_template.py 9911 2024-08-21 08:05:47Z 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 import os
12 import platform
13 from pathlib import Path
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 html5_polyglot
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=html5_polyglot.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 <!DOCTYPE html>
78 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
79 <head>"""
80
81
82 head = """\
83 <meta charset="utf-8" />
84 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
85 <meta name="viewport" content="width=device-width, initial-scale=1" />
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 <main id="document-title">"""
98
99
100 body_pre_docinfo = """\
101 <h1 class="title">Document Title</h1>
102 <p class="subtitle" id="subtitle">Subtitle</p>"""
103
104
105 docinfo = """\
106 <dl class="docinfo simple">
107 <dt class="author">Author<span class="colon">:</span></dt>
108 <dd class="author"><p>Me</p></dd>
109 </dl>"""
110
111
112 body = """\
113 <section id="section">
114 <h2>Section</h2>
115 <p>Some text.</p>
116 </section>"""
117
118
119 body_suffix = """\
120 </main>
121 <footer>
122 <p>footer text</p>
123 </footer>
124 </body>
125 </html>"""
126
127
128 head_prefix = """\
129 <!DOCTYPE html>
130 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
131 <head>"""
132
133
134 head = """\
135 <meta charset="utf-8" />
136 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
137 <meta name="viewport" content="width=device-width, initial-scale=1" />
138 <meta name="author" content="Me" />
139 <title>Document Title</title>"""
140
141
142 stylesheet = """\
143 <link rel="stylesheet" href="{drive_prefix}/test.css" type="text/css" />"""
144
145
146 body_prefix = """\
147 </head>
148 <body>
149 <main id="document-title">"""
150
151
152 body_pre_docinfo = """\
153 <h1 class="title">Document Title</h1>
154 <p class="subtitle" id="subtitle">Subtitle</p>"""
155
156
157 docinfo = """\
158 <dl class="docinfo simple">
159 <dt class="author">Author<span class="colon">:</span></dt>
160 <dd class="author"><p>Me</p></dd>
161 </dl>"""
162
163
164 body = """\
165 <section id="section">
166 <h2>Section</h2>
167 <p>Some text.</p>
168 </section>"""
169
170
171 body_suffix = """\
172 </main>
173 <footer>
174 <p>footer text</p>
175 </footer>
176 </body>
177 </html>"""
178
179
180 title = """\
181 Document Title"""
182
183
184 subtitle = """\
185 Subtitle"""
186
187
188 header = """\
189 """
190
191
192 footer = """\
193 <footer>
194 <p>footer text</p>
195 </footer>"""
196
197
198 meta = """\
199 <meta charset="utf-8" />
200 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
201 <meta name="viewport" content="width=device-width, initial-scale=1" />
202 <meta name="author" content="Me" />"""
203
204
205 fragment = """\
206 <section id="section">
207 <h2>Section</h2>
208 <p>Some text.</p>
209 </section>"""
210
211
212 html_prolog = """\
213 <!DOCTYPE html>"""
214
215
216 html_head = """\
217 <meta charset="%s" />
218 <meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />
219 <meta name="viewport" content="width=device-width, initial-scale=1" />
220 <meta name="author" content="Me" />
221 <title>Document Title</title>"""
222
223
224 html_title = """\
225 <h1 class="title">Document Title</h1>"""
226
227
228 html_subtitle = """\
229 <p class="subtitle" id="subtitle">Subtitle</p>"""
230
231
232 html_body = """\
233 <main id="document-title">
234 <h1 class="title">Document Title</h1>
235 <p class="subtitle" id="subtitle">Subtitle</p>
236 <dl class="docinfo simple">
237 <dt class="author">Author<span class="colon">:</span></dt>
238 <dd class="author"><p>Me</p></dd>
239 </dl>
240 <section id="section">
241 <h2>Section</h2>
242 <p>Some text.</p>
243 </section>
244 </main>
245 <footer>
246 <p>footer text</p>
247 </footer>"""
248 ''']
249 ]
250
251 if __name__ == '__main__':
252 unittest.main()