| 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 |
| 22 |
from docutils.writers import html4css1 |
| 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 |
} |
| 43 |
result = bytes(core.publish_string( |
| 44 |
'EUR = \u20ac', writer=html4css1.Writer(), |
| 45 |
settings_overrides=settings_overrides)) |
| 46 |
|
| 47 |
|
| 48 |
self.assertIn(b'EUR = €', result) |
| 49 |
|
| 50 |
|
| 51 |
class MovingArgsTestCase(unittest.TestCase): |
| 52 |
|
| 53 |
mys = {'_disable_config': True} |
| 54 |
|
| 55 |
def test_definition_list_item_classes(self): |
| 56 |
|
| 57 |
|
| 58 |
data = """\ |
| 59 |
first term: |
| 60 |
fist def |
| 61 |
|
| 62 |
.. class:: for the second item |
| 63 |
|
| 64 |
second term: |
| 65 |
second def |
| 66 |
""" |
| 67 |
result = core.publish_string(data, writer=html4css1.Writer(), |
| 68 |
settings_overrides=self.mys) |
| 69 |
self.assertIn(b'<dt class="for the second item">second term:</dt>', |
| 70 |
result) |
| 71 |
|
| 72 |
def test_definition_list_item_name(self): |
| 73 |
|
| 74 |
|
| 75 |
data = """\ |
| 76 |
first term: |
| 77 |
first def |
| 78 |
|
| 79 |
.. _second item: |
| 80 |
|
| 81 |
second term: |
| 82 |
second def |
| 83 |
""" |
| 84 |
result = core.publish_string(data, writer=html4css1.Writer(), |
| 85 |
settings_overrides=self.mys) |
| 86 |
self.assertIn(b'<dt id="second-item">second term:</dt>', |
| 87 |
result) |
| 88 |
|
| 89 |
|
| 90 |
ham_css = relpath(DATA_ROOT/'ham.css') |
| 91 |
|
| 92 |
|
| 93 |
class SettingsTestCase(unittest.TestCase): |
| 94 |
data = 'test' |
| 95 |
|
| 96 |
def test_default_stylesheet(self): |
| 97 |
|
| 98 |
mys = {'_disable_config': True} |
| 99 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 100 |
settings_overrides=mys)['stylesheet'] |
| 101 |
self.assertIn('Default cascading style sheet ' |
| 102 |
'for the HTML output of Docutils.', styles) |
| 103 |
|
| 104 |
def test_default_stylesheet_linked(self): |
| 105 |
|
| 106 |
mys = {'_disable_config': True, |
| 107 |
'embed_stylesheet': False} |
| 108 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 109 |
settings_overrides=mys)['stylesheet'] |
| 110 |
self.assertIn('docutils/writers/html4css1/html4css1.css', styles) |
| 111 |
|
| 112 |
def test_math_stylesheet_linked(self): |
| 113 |
|
| 114 |
mys = {'_disable_config': True, |
| 115 |
'embed_stylesheet': False, |
| 116 |
'stylesheet_path': 'html4css1.css, math.css'} |
| 117 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 118 |
settings_overrides=mys)['stylesheet'] |
| 119 |
self.assertIn('docutils/writers/html4css1/html4css1.css', styles) |
| 120 |
self.assertIn('docutils/writers/html5_polyglot/math.css', styles) |
| 121 |
|
| 122 |
def test_custom_stylesheet_linked(self): |
| 123 |
|
| 124 |
mys = {'_disable_config': True, |
| 125 |
'embed_stylesheet': False, |
| 126 |
'stylesheet_path': 'html4css1.css, ' |
| 127 |
'data/ham.css'} |
| 128 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 129 |
settings_overrides=mys)['stylesheet'] |
| 130 |
self.assertIn('docutils/writers/html4css1/html4css1.css', styles) |
| 131 |
self.assertIn('href="data/ham.css"', styles) |
| 132 |
|
| 133 |
def test_custom_stylesheet_dir(self): |
| 134 |
mys = {'_disable_config': True, |
| 135 |
'embed_stylesheet': False, |
| 136 |
'stylesheet_dirs': ( |
| 137 |
TEST_ROOT / '../docutils/writers/html4css1/', |
| 138 |
DATA_ROOT), |
| 139 |
'stylesheet_path': 'html4css1.css, ham.css'} |
| 140 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 141 |
settings_overrides=mys)['stylesheet'] |
| 142 |
if (TEST_ROOT / '../docutils/writers/html4css1/').is_dir(): |
| 143 |
self.assertIn('docutils/writers/html4css1/html4css1.css', styles) |
| 144 |
self.assertIn(f'href="{ham_css}"', styles) |
| 145 |
|
| 146 |
def test_custom_stylesheet_dir_embedded(self): |
| 147 |
mys = {'_disable_config': True, |
| 148 |
'embed_stylesheet': True, |
| 149 |
'stylesheet_dirs': ( |
| 150 |
TEST_ROOT / '../docutils/writers/html4css1/', |
| 151 |
DATA_ROOT), |
| 152 |
'stylesheet_path': 'ham.css'} |
| 153 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 154 |
settings_overrides=mys)['stylesheet'] |
| 155 |
self.assertIn('dl.docutils dd {\n margin-bottom: 0.5em }', styles) |
| 156 |
|
| 157 |
|
| 158 |
html4css1_css = relpath(TEST_ROOT/'functional/input/data/html4css1.css') |
| 159 |
math_css = relpath(TEST_ROOT/'functional/input/data/math.css') |
| 160 |
|
| 161 |
|
| 162 |
class MathTestCase(unittest.TestCase): |
| 163 |
|
| 164 |
"""Attention: This class tests the current implementation of maths support |
| 165 |
which is open to change in future Docutils releases. """ |
| 166 |
|
| 167 |
mathjax_script = '<script type="text/javascript" src="%s">' |
| 168 |
default_mathjax_url = ('file:/usr/share/javascript/mathjax/MathJax.js' |
| 169 |
'?config=TeX-AMS_CHTML') |
| 170 |
custom_mathjax_url = '/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML' |
| 171 |
data = ':math:`42`' |
| 172 |
|
| 173 |
def test_math_output_default(self): |
| 174 |
|
| 175 |
mys = {'_disable_config': True} |
| 176 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 177 |
settings_overrides=mys)['stylesheet'] |
| 178 |
self.assertIn('convert LaTeX equations to HTML output.', styles) |
| 179 |
|
| 180 |
def test_math_output_mathjax(self): |
| 181 |
|
| 182 |
|
| 183 |
mys = {'_disable_config': True, |
| 184 |
'report_level': 3, |
| 185 |
'math_output': 'MathJax'} |
| 186 |
head = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 187 |
settings_overrides=mys)['head'] |
| 188 |
self.assertIn(self.mathjax_script % self.default_mathjax_url, head) |
| 189 |
|
| 190 |
def test_math_output_mathjax_custom(self): |
| 191 |
|
| 192 |
mys = {'_disable_config': True, |
| 193 |
'math_output': |
| 194 |
'mathjax %s' % self.custom_mathjax_url} |
| 195 |
head = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 196 |
settings_overrides=mys)['head'] |
| 197 |
self.assertIn(self.mathjax_script % self.custom_mathjax_url, head) |
| 198 |
|
| 199 |
def test_math_output_html(self): |
| 200 |
mys = {'_disable_config': True, |
| 201 |
'math_output': 'HTML'} |
| 202 |
head = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 203 |
settings_overrides=mys)['head'] |
| 204 |
|
| 205 |
self.assertNotIn('MathJax.js', head) |
| 206 |
|
| 207 |
def test_math_output_html_stylesheet(self): |
| 208 |
mys = {'_disable_config': True, |
| 209 |
'math_output': 'HTML math.css,custom/style.css', |
| 210 |
'stylesheet_dirs': ( |
| 211 |
TEST_ROOT, |
| 212 |
os.path.join(TEST_ROOT, 'functional/input/data')), |
| 213 |
'embed_stylesheet': False} |
| 214 |
styles = core.publish_parts(self.data, writer=html4css1.Writer(), |
| 215 |
settings_overrides=mys)['stylesheet'] |
| 216 |
self.assertEqual(f"""\ |
| 217 |
<link rel="stylesheet" href="{html4css1_css}" type="text/css" /> |
| 218 |
<link rel="stylesheet" href="{math_css}" type="text/css" /> |
| 219 |
<link rel="stylesheet" href="custom/style.css" type="text/css" /> |
| 220 |
""", styles) |
| 221 |
|
| 222 |
def test_math_output_mathjax_no_math(self): |
| 223 |
|
| 224 |
head = core.publish_parts('No math.', writer=html4css1.Writer())['head'] |
| 225 |
self.assertNotIn('MathJax', head) |
| 226 |
|
| 227 |
|
| 228 |
if __name__ == '__main__': |
| 229 |
unittest.main() |