Debian Codegraph @

test/test_writers/test_html4css1_misc.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_html4css1_misc.py 9914 2024-08-21 13:45:08Z milde $
3 # Authors: Lea Wiemann, Dmitry Shachnev, Günter Milde
4 # Maintainer: [email protected]
5 # Copyright: This module has been placed in the public domain.
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 # prepend the "docutils root" to the Python library path
18 # so we import the local `docutils` package.
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 # TEST_ROOT is ./test/ from the docutils root
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 # Test that xmlcharrefreplace is the default output encoding
37 # error handler.
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 # Encoding a euro sign with latin1 doesn't work, so the
47 # xmlcharrefreplace handler is used.
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 # Do not drop class arguments for the definition list item.
57 # Pass them to to the term node instead.
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 # Do not drop the "name" of the definition list item.
74 # Pass it to to the term node instead.
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 # default style sheet, embedded
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 # default style sheet, linked
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 # default + math style sheet, linked
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 # default + custom style sheet, linked
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 # HTML with math.css stylesheet (since 0.11)
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 # Explicitly specifying math_output=MathJax, case insensitively
182 # use default MathJax URL
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 # Customizing MathJax URL
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 # There should be no MathJax script when math_output is not MathJax
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 # There should be no math script when text does not contain math
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()