| 1 |
|
| 2 |
|
| 3 |
e |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Miscellaneous HTML writer tests. |
| 9 |
""" |
| 10 |
|
| 11 |
import os |
| 12 |
from pathlib import Path |
| 13 |
import sys |
| 14 |
import unittest |
| 15 |
|
| 16 |
if __name__ == '__main__': |
| 17 |
|
| 18 |
|
| 19 |
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 20 |
|
| 21 |
from docutils import core, frontend, nodes, utils |
| 22 |
from docutils.writers import html5_polyglot, _html_base |
| 23 |
|
| 24 |
|
| 25 |
TEST_ROOT = Path(__file__).parents[1] |
| 26 |
DATA_ROOT = TEST_ROOT / 'data' |
| 27 |
|
| 28 |
|
| 29 |
def relpath(path): |
| 30 |
return os.path.relpath(path, os.getcwd()).replace('\\', '/') |
| 31 |
|
| 32 |
|
| 33 |
class EncodingTestCase(unittest.TestCase): |
| 34 |
|
| 35 |
def test_xmlcharrefreplace(self): |
| 36 |
|
| 37 |
|
| 38 |
settings_overrides = { |
| 39 |
'output_encoding': 'latin1', |
| 40 |
'stylesheet_path': '', |
| 41 |
'_disable_config': True} |
| 42 |
result = core.publish_string( |
| 43 |
'EUR = \u20ac', writer=html5_polyglot.Writer(), |
| 44 |
settings_overrides=settings_overrides) |
| 45 |
|
| 46 |
|
| 47 |
self.assertIn(b'EUR = €', result) |
| 48 |
|
| 49 |
|
| 50 |
class MovingArgsTestCase(unittest.TestCase): |
| 51 |
|
| 52 |
mys = {'stylesheet_path': '', |
| 53 |
'_disable_config': True, |
| 54 |
} |
| 55 |
|
| 56 |
def test_definition_list_item_classes(self): |
| 57 |
|
| 58 |
|
| 59 |
data = """\ |
| 60 |
first term: |
| 61 |
fist def |
| 62 |
|
| 63 |
.. class:: for the second item |
| 64 |
|
| 65 |
second term: |
| 66 |
second def |
| 67 |
""" |
| 68 |
result = core.publish_string(data, writer=html5_polyglot.Writer(), |
| 69 |
settings_overrides=self.mys).decode() |
| 70 |
self.assertIn('<dt class="for the second item">second term:</dt>', |
| 71 |
result) |
| 72 |
|
| 73 |
def test_definition_list_item_name(self): |
| 74 |
|
| 75 |
|
| 76 |
data = """\ |
| 77 |
first term: |
| 78 |
first def |
| 79 |
|
| 80 |
.. _second item: |
| 81 |
|
| 82 |
second term: |
| 83 |
second def |
| 84 |
""" |
| 85 |
result = core.publish_string(data, writer=html5_polyglot.Writer(), |
| 86 |
settings_overrides=self.mys).decode() |
| 87 |
self.assertIn('<dt id="second-item">second term:</dt>', |
| 88 |
result) |
| 89 |
|
| 90 |
|
| 91 |
ham_css = relpath(DATA_ROOT / 'ham.css') |
| 92 |
|
| 93 |
|
| 94 |
class SettingsTestCase(unittest.TestCase): |
| 95 |
data = 'test' |
| 96 |
|
| 97 |
def test_default_stylesheet(self): |
| 98 |
|
| 99 |
mys = {'_disable_config': True} |
| 100 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 101 |
settings_overrides=mys)['stylesheet'] |
| 102 |
self.assertIn('Minimal style sheet for the HTML output of Docutils.', |
| 103 |
styles) |
| 104 |
|
| 105 |
def test_default_stylesheet_linked(self): |
| 106 |
|
| 107 |
mys = {'_disable_config': True, |
| 108 |
'embed_stylesheet': False} |
| 109 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 110 |
settings_overrides=mys)['stylesheet'] |
| 111 |
self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles) |
| 112 |
|
| 113 |
def test_math_stylesheet_linked(self): |
| 114 |
|
| 115 |
mys = {'_disable_config': True, |
| 116 |
'embed_stylesheet': False, |
| 117 |
'stylesheet_path': 'minimal.css, math.css'} |
| 118 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 119 |
settings_overrides=mys)['stylesheet'] |
| 120 |
self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles) |
| 121 |
self.assertIn('docutils/writers/html5_polyglot/math.css', styles) |
| 122 |
|
| 123 |
def test_custom_stylesheet_linked(self): |
| 124 |
|
| 125 |
mys = {'_disable_config': True, |
| 126 |
'embed_stylesheet': False, |
| 127 |
'stylesheet_path': 'minimal.css, ' |
| 128 |
'data/ham.css'} |
| 129 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 130 |
settings_overrides=mys)['stylesheet'] |
| 131 |
self.assertIn('docutils/writers/html5_polyglot/minimal.css', styles) |
| 132 |
self.assertIn('href="data/ham.css"', styles) |
| 133 |
|
| 134 |
def test_custom_stylesheet_dir(self): |
| 135 |
mys = {'_disable_config': True, |
| 136 |
'embed_stylesheet': False, |
| 137 |
'stylesheet_dirs': ( |
| 138 |
TEST_ROOT / '../docutils/writers/html5_polyglot/', |
| 139 |
DATA_ROOT), |
| 140 |
'stylesheet_path': 'minimal.css, ham.css'} |
| 141 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 142 |
settings_overrides=mys)['stylesheet'] |
| 143 |
if (TEST_ROOT / '../docutils/writers/html5_polyglot/').is_dir(): |
| 144 |
self.assertIn('docutils/writers/html5_polyglot/minimal.css', |
| 145 |
styles) |
| 146 |
self.assertIn(f'href="{ham_css}"', styles) |
| 147 |
|
| 148 |
def test_custom_stylesheet_dir_embedded(self): |
| 149 |
mys = {'_disable_config': True, |
| 150 |
'embed_stylesheet': True, |
| 151 |
'stylesheet_dirs': ( |
| 152 |
TEST_ROOT / '../docutils/writers/html5_polyglot/', |
| 153 |
DATA_ROOT), |
| 154 |
'stylesheet_path': 'ham.css'} |
| 155 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 156 |
settings_overrides=mys)['stylesheet'] |
| 157 |
self.assertIn('dl.docutils dd {\n margin-bottom: 0.5em }', styles) |
| 158 |
|
| 159 |
def test_future_warnings(self): |
| 160 |
"""Warn about deprecated setting name.""" |
| 161 |
my_settings = {'_disable_config': True, |
| 162 |
'embed_images': False, |
| 163 |
'output_encoding': 'unicode', |
| 164 |
} |
| 165 |
with self.assertWarnsRegex(FutureWarning, |
| 166 |
'"embed_images"\n will be removed'): |
| 167 |
core.publish_string('warnings test', |
| 168 |
writer=html5_polyglot.Writer(), |
| 169 |
settings_overrides=my_settings) |
| 170 |
|
| 171 |
|
| 172 |
minimal_css = relpath(TEST_ROOT / 'functional/input/data/minimal.css') |
| 173 |
plain_css = relpath(TEST_ROOT / 'functional/input/data/plain.css') |
| 174 |
math_css = relpath(TEST_ROOT / 'functional/input/data/math.css') |
| 175 |
|
| 176 |
|
| 177 |
class MathTestCase(unittest.TestCase): |
| 178 |
|
| 179 |
"""Provisional: This class tests the current implementation of maths |
| 180 |
support which is open to change in future Docutils releases. """ |
| 181 |
|
| 182 |
mathjax_script = '<script type="text/javascript" src="%s">' |
| 183 |
default_mathjax_url = ('file:/usr/share/javascript/mathjax/MathJax.js' |
| 184 |
'?config=TeX-AMS_CHTML') |
| 185 |
custom_mathjax_url = '/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML' |
| 186 |
data = ':math:`42`' |
| 187 |
|
| 188 |
def test_math_output_default(self): |
| 189 |
|
| 190 |
mys = {'_disable_config': True} |
| 191 |
fragment = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 192 |
settings_overrides=mys)['fragment'] |
| 193 |
self.assertIn('<mn>42</mn>', fragment) |
| 194 |
|
| 195 |
def test_math_output_mathjax(self): |
| 196 |
|
| 197 |
|
| 198 |
mys = {'_disable_config': True, |
| 199 |
'report_level': 3, |
| 200 |
'math_output': 'MathJax'} |
| 201 |
head = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 202 |
settings_overrides=mys)['head'] |
| 203 |
self.assertIn(self.mathjax_script % self.default_mathjax_url, head) |
| 204 |
|
| 205 |
def test_math_output_mathjax_custom(self): |
| 206 |
|
| 207 |
mys = {'_disable_config': True, |
| 208 |
'math_output': |
| 209 |
'mathjax %s' % self.custom_mathjax_url} |
| 210 |
head = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 211 |
settings_overrides=mys)['head'] |
| 212 |
self.assertIn(self.mathjax_script % self.custom_mathjax_url, head) |
| 213 |
|
| 214 |
def test_math_output_html(self): |
| 215 |
mys = {'_disable_config': True, |
| 216 |
'math_output': 'HTML'} |
| 217 |
head = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 218 |
settings_overrides=mys)['head'] |
| 219 |
|
| 220 |
self.assertNotIn('MathJax.js', head) |
| 221 |
|
| 222 |
def test_math_output_html_stylesheet(self): |
| 223 |
mys = {'_disable_config': True, |
| 224 |
'math_output': 'HTML math.css,custom/style.css', |
| 225 |
'stylesheet_dirs': ( |
| 226 |
TEST_ROOT, |
| 227 |
TEST_ROOT / 'functional/input/data'), |
| 228 |
'embed_stylesheet': False} |
| 229 |
styles = core.publish_parts(self.data, writer=html5_polyglot.Writer(), |
| 230 |
settings_overrides=mys)['stylesheet'] |
| 231 |
self.assertEqual(f"""\ |
| 232 |
<link rel="stylesheet" href="{minimal_css}" type="text/css" /> |
| 233 |
<link rel="stylesheet" href="{plain_css}" type="text/css" /> |
| 234 |
<link rel="stylesheet" href="{math_css}" type="text/css" /> |
| 235 |
<link rel="stylesheet" href="custom/style.css" type="text/css" /> |
| 236 |
""", styles) |
| 237 |
|
| 238 |
def test_math_output_mathjax_no_math(self): |
| 239 |
|
| 240 |
head = core.publish_parts('No math.', writer=html5_polyglot.Writer())['head'] |
| 241 |
self.assertNotIn('MathJax', head) |
| 242 |
|
| 243 |
|
| 244 |
class ImagesTestCase(unittest.TestCase): |
| 245 |
"""Test image handling routines.""" |
| 246 |
maxDiff = None |
| 247 |
settings = frontend.get_default_settings(_html_base.Writer) |
| 248 |
document = utils.new_document('test data', settings) |
| 249 |
translator = _html_base.HTMLTranslator(document) |
| 250 |
svg_sample = """\ |
| 251 |
<?xml version="1.0" encoding="UTF-8"?> |
| 252 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 253 |
style="background: blue; width: 5ex; height: 3ex" viewBox="0 0 10 10"> |
| 254 |
<circle cx="5" cy="5" r="4" fill="lightblue" /> |
| 255 |
<!-- comments are ignored --> |
| 256 |
</svg> |
| 257 |
""" |
| 258 |
expected = """\ |
| 259 |
<svg xmlns="http://www.w3.org/2000/svg" style="background: blue; width: 4em; height: 3ex;" viewBox="0 0 10 10" height="32" class="test me"> |
| 260 |
<title>blue circle</title><circle cx="5" cy="5" r="4" fill="lightblue" /> |
| 261 |
\n\ |
| 262 |
</svg>""" |
| 263 |
|
| 264 |
def test_image_size(self): |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
image = nodes.image(width='4em', height='3', scale=200) |
| 269 |
self.assertEqual(self.translator.image_size(image), |
| 270 |
{'height': '6', 'style': 'width: 8em;'}) |
| 271 |
|
| 272 |
image = nodes.image(width='4em', height='3', scale=50) |
| 273 |
self.assertEqual(self.translator.image_size(image), |
| 274 |
{'style': 'width: 2em;', 'height': '2'}) |
| 275 |
|
| 276 |
def test_prepare_svg(self): |
| 277 |
|
| 278 |
|
| 279 |
image = nodes.image(height='32', width='4em', alt='blue circle', |
| 280 |
align='left', classes=['test', 'me']) |
| 281 |
atts = self.translator.image_size(image) |
| 282 |
rv = self.translator.prepare_svg(self.svg_sample, image, atts) |
| 283 |
self.assertEqual(rv, self.expected) |
| 284 |
|
| 285 |
def test_prepare_svg_syntax_variants(self): |
| 286 |
|
| 287 |
svg_sample = '<svg style="width:3em"></svg>' |
| 288 |
|
| 289 |
atts = {'style': 'colon:in:value'} |
| 290 |
rv = self.translator.prepare_svg(svg_sample, nodes.image(), atts) |
| 291 |
self.assertEqual(rv, '<svg style="width: 3em; colon: in:value;" />') |
| 292 |
|
| 293 |
atts = {'style': 'no-colon;'} |
| 294 |
rv = self.translator.prepare_svg(svg_sample, nodes.image(), atts) |
| 295 |
self.assertEqual(rv, '<svg style="width: 3em; no-colon: ;" />') |
| 296 |
|
| 297 |
|
| 298 |
if __name__ == '__main__': |
| 299 |
unittest.main() |