| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test `core.publish_parts()`__ with the HTML5 writer. |
| 9 |
|
| 10 |
__ https://docutils.sourceforge.io/docs/api/publisher.html#publish-parts |
| 11 |
""" |
| 12 |
|
| 13 |
from pathlib import Path |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
|
| 17 |
if __name__ == '__main__': |
| 18 |
|
| 19 |
|
| 20 |
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 21 |
|
| 22 |
import docutils |
| 23 |
import docutils.core |
| 24 |
from docutils.writers import html5_polyglot |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
default_parts = { |
| 31 |
'body': '{fragment}', |
| 32 |
'body_pre_docinfo': '', |
| 33 |
'body_prefix': '</head>\n<body>\n{header}<main>\n', |
| 34 |
'body_suffix': '</main>\n{footer}</body>\n</html>\n', |
| 35 |
'docinfo': '', |
| 36 |
'encoding': 'utf-8', |
| 37 |
'errors': 'xmlcharrefreplace', |
| 38 |
'footer': '', |
| 39 |
'fragment': '', |
| 40 |
'head': '{meta}<title>{metatitle}</title>\n', |
| 41 |
'head_prefix': |
| 42 |
'<!DOCTYPE html>\n' |
| 43 |
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n' |
| 44 |
'<head>\n', |
| 45 |
'header': '', |
| 46 |
'html_body': '{header}<main>\n{fragment}</main>\n{footer}', |
| 47 |
'html_head': '{meta}<title>{metatitle}</title>\n', |
| 48 |
'html_prolog': '<!DOCTYPE html>\n', |
| 49 |
'html_subtitle': '', |
| 50 |
'html_title': '', |
| 51 |
'meta': '<meta charset="%s" />\n' |
| 52 |
f'<meta name="generator" content="Docutils {docutils.__version__}: https://docutils.sourceforge.io/" />\n' |
| 53 |
'<meta name="viewport" content="width=device-width, initial-scale=1" />\n', |
| 54 |
'stylesheet': '', |
| 55 |
'subtitle': '', |
| 56 |
'title': '', |
| 57 |
'version': f'{docutils.__version__}', |
| 58 |
'whole': |
| 59 |
'{head_prefix}\n' |
| 60 |
'{head}\n' |
| 61 |
'{stylesheet}\n' |
| 62 |
'{body_prefix}\n' |
| 63 |
'{body_pre_docinfo}\n' |
| 64 |
'{docinfo}\n' |
| 65 |
'{body}\n' |
| 66 |
'{body_suffix}\n', |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
def format_parts(parts): |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
metatitle = parts['title'] or '<string>' |
| 76 |
|
| 77 |
|
| 78 |
parts['html_head'] = parts['html_head'].format(metatitle=metatitle, |
| 79 |
**parts) |
| 80 |
|
| 81 |
try: |
| 82 |
parts['meta'] = parts['meta'] % parts['encoding'] |
| 83 |
except TypeError: |
| 84 |
pass |
| 85 |
parts['head'] = parts['head'].format(metatitle=metatitle, **parts) |
| 86 |
parts['body_prefix'] = parts['body_prefix'].format(**parts) |
| 87 |
parts['body'] = parts['body'].format(**parts) |
| 88 |
parts['body_suffix'] = parts['body_suffix'].format(**parts) |
| 89 |
parts['html_body'] = parts['html_body'].format(**parts) |
| 90 |
|
| 91 |
parts['whole'] = parts['whole'].format(**{k: v.rstrip('\n') |
| 92 |
for k, v in parts.items()}) |
| 93 |
return parts |
| 94 |
|
| 95 |
|
| 96 |
class Html5WriterPublishPartsTestCase(unittest.TestCase): |
| 97 |
"""Test HTML5 writer `publish_parts()` interface.""" |
| 98 |
|
| 99 |
maxDiff = None |
| 100 |
|
| 101 |
def test_publish_parts(self): |
| 102 |
for name, (settings_overrides, cases) in totest.items(): |
| 103 |
for casenum, (case_input, expected_parts) in enumerate(cases): |
| 104 |
_stgns = {'_disable_config': True, |
| 105 |
'strict_visitor': True, |
| 106 |
'stylesheet_path': '', |
| 107 |
'section_self_link': True, |
| 108 |
**settings_overrides, |
| 109 |
} |
| 110 |
parts = docutils.core.publish_parts(source=case_input, |
| 111 |
writer=html5_polyglot.Writer(), |
| 112 |
settings_overrides=_stgns, |
| 113 |
) |
| 114 |
expected = format_parts(default_parts | expected_parts) |
| 115 |
for key in parts.keys(): |
| 116 |
with self.subTest(id=f'totest[{name!r}][{casenum}][{key}]'): |
| 117 |
self.assertEqual(f'{expected[key]}', f'{parts[key]}') |
| 118 |
|
| 119 |
|
| 120 |
totest = {} |
| 121 |
|
| 122 |
totest['standard'] = ({'initial_header_level': 'auto' |
| 123 |
}, [ |
| 124 |
['', |
| 125 |
{} |
| 126 |
], |
| 127 |
['Simple String with *markup*', |
| 128 |
{'fragment': '<p>Simple String with <em>markup</em></p>\n'} |
| 129 |
], |
| 130 |
['.. header:: custom document header\n\n' |
| 131 |
'A paragraph.', |
| 132 |
{'header': '<header>\n<p>custom document header</p>\n</header>\n', |
| 133 |
'body_prefix': '</head>\n' |
| 134 |
'<body>\n' |
| 135 |
'<header>\n' |
| 136 |
'<p>custom document header</p>\n</header>\n' |
| 137 |
'<main>\n', |
| 138 |
'fragment': '<p>A paragraph.</p>\n', |
| 139 |
} |
| 140 |
], |
| 141 |
['.. footer:: custom document footer\n\n' |
| 142 |
'A paragraph.', |
| 143 |
{'footer': '<footer>\n<p>custom document footer</p>\n</footer>\n', |
| 144 |
'fragment': '<p>A paragraph.</p>\n', |
| 145 |
} |
| 146 |
], |
| 147 |
["""\ |
| 148 |
+++++ |
| 149 |
Title |
| 150 |
+++++ |
| 151 |
|
| 152 |
Subtitle |
| 153 |
======== |
| 154 |
|
| 155 |
Some stuff |
| 156 |
|
| 157 |
Section |
| 158 |
------- |
| 159 |
|
| 160 |
Some more stuff |
| 161 |
|
| 162 |
Another Section |
| 163 |
............... |
| 164 |
|
| 165 |
And even more stuff |
| 166 |
""", |
| 167 |
{'body_pre_docinfo': '<h1 class="title">Title</h1>\n' |
| 168 |
'<p class="subtitle" id="subtitle">Subtitle</p>\n', |
| 169 |
'body_prefix': '</head>\n<body>\n<main id="title">\n', |
| 170 |
'fragment': """\ |
| 171 |
<p>Some stuff</p> |
| 172 |
<section id="section"> |
| 173 |
<h2>Section<a class="self-link" title="link to this section" href="#section"></a></h2> |
| 174 |
<p>Some more stuff</p> |
| 175 |
<section id="another-section"> |
| 176 |
<h3>Another Section<a class="self-link" title="link to this section" href="#another-section"></a></h3> |
| 177 |
<p>And even more stuff</p> |
| 178 |
</section> |
| 179 |
</section> |
| 180 |
""", |
| 181 |
'html_body': """\ |
| 182 |
<main id="title"> |
| 183 |
<h1 class="title">Title</h1> |
| 184 |
<p class="subtitle" id="subtitle">Subtitle</p> |
| 185 |
<p>Some stuff</p> |
| 186 |
<section id="section"> |
| 187 |
<h2>Section<a class="self-link" title="link to this section" href="#section"></a></h2> |
| 188 |
<p>Some more stuff</p> |
| 189 |
<section id="another-section"> |
| 190 |
<h3>Another Section<a class="self-link" title="link to this section" href="#another-section"></a></h3> |
| 191 |
<p>And even more stuff</p> |
| 192 |
</section> |
| 193 |
</section> |
| 194 |
</main> |
| 195 |
""", |
| 196 |
'html_subtitle': '<p class="subtitle" id="subtitle">Subtitle</p>\n', |
| 197 |
'html_title': '<h1 class="title">Title</h1>\n', |
| 198 |
'subtitle': 'Subtitle', |
| 199 |
'title': 'Title' |
| 200 |
}], |
| 201 |
["""\ |
| 202 |
+++++ |
| 203 |
Title |
| 204 |
+++++ |
| 205 |
|
| 206 |
:author: me |
| 207 |
|
| 208 |
Some stuff |
| 209 |
""", |
| 210 |
{'body_pre_docinfo': '<h1 class="title">Title</h1>\n', |
| 211 |
'body_prefix': '</head>\n<body>\n<main id="title">\n', |
| 212 |
'docinfo': """\ |
| 213 |
<dl class="docinfo simple"> |
| 214 |
<dt class="author">Author<span class="colon">:</span></dt> |
| 215 |
<dd class="author"><p>me</p></dd> |
| 216 |
</dl> |
| 217 |
""", |
| 218 |
'fragment': '<p>Some stuff</p>\n', |
| 219 |
'html_body': """\ |
| 220 |
<main id="title"> |
| 221 |
<h1 class="title">Title</h1> |
| 222 |
<dl class="docinfo simple"> |
| 223 |
<dt class="author">Author<span class="colon">:</span></dt> |
| 224 |
<dd class="author"><p>me</p></dd> |
| 225 |
</dl> |
| 226 |
<p>Some stuff</p> |
| 227 |
</main> |
| 228 |
""", |
| 229 |
'html_title': '<h1 class="title">Title</h1>\n', |
| 230 |
'meta': default_parts['meta'] + '<meta name="author" content="me" />\n', |
| 231 |
'title': 'Title' |
| 232 |
}], |
| 233 |
]) |
| 234 |
|
| 235 |
totest['no_title_promotion'] = ({'doctitle_xform': False, |
| 236 |
'initial_header_level': 'auto'}, [ |
| 237 |
["""\ |
| 238 |
+++++ |
| 239 |
Title |
| 240 |
+++++ |
| 241 |
|
| 242 |
Not A Subtitle |
| 243 |
============== |
| 244 |
|
| 245 |
Some stuff |
| 246 |
|
| 247 |
Section |
| 248 |
------- |
| 249 |
|
| 250 |
Some more stuff |
| 251 |
|
| 252 |
Another Section |
| 253 |
............... |
| 254 |
|
| 255 |
And even more stuff |
| 256 |
""", |
| 257 |
{'fragment': """\ |
| 258 |
<section id="title"> |
| 259 |
<h1>Title<a class="self-link" title="link to this section" href="#title"></a></h1> |
| 260 |
<section id="not-a-subtitle"> |
| 261 |
<h2>Not A Subtitle<a class="self-link" title="link to this section" href="#not-a-subtitle"></a></h2> |
| 262 |
<p>Some stuff</p> |
| 263 |
<section id="section"> |
| 264 |
<h3>Section<a class="self-link" title="link to this section" href="#section"></a></h3> |
| 265 |
<p>Some more stuff</p> |
| 266 |
<section id="another-section"> |
| 267 |
<h4>Another Section<a class="self-link" title="link to this section" href="#another-section"></a></h4> |
| 268 |
<p>And even more stuff</p> |
| 269 |
</section> |
| 270 |
</section> |
| 271 |
</section> |
| 272 |
</section> |
| 273 |
"""}, |
| 274 |
], |
| 275 |
]) |
| 276 |
|
| 277 |
totest['unknown-encoding'] = ({'output_encoding': 'unicode'}, [ |
| 278 |
['Simple String\n', |
| 279 |
{'encoding': 'unicode', |
| 280 |
'fragment': '<p>Simple String</p>\n', |
| 281 |
'html_head': f'{default_parts["meta"]}<title>{{metatitle}}</title>\n', |
| 282 |
'meta': default_parts['meta'].removeprefix('<meta charset="%s" />\n'), |
| 283 |
}], |
| 284 |
]) |
| 285 |
|
| 286 |
|
| 287 |
if __name__ == '__main__': |
| 288 |
unittest.main() |