| 1 |
# SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: GPL-3.0-only |
| 4 |
# |
| 5 |
# This file is part of the program "Hyperorg" which is released under GNU |
| 6 |
# General Public License v3 (GPLv3). |
| 7 |
# See folder LICENSES or go to <https://www.gnu.org/licenses/#GPL>. |
| 8 |
""" |
| 9 |
Tests about generating HTML from elements, e.g. via using |
| 10 |
_paragraph_to_html(). |
| 11 |
""" |
| 12 |
import unittest |
| 13 |
import html |
| 14 |
from hyperorg import elements, exporter, textruns |
| 15 |
from hyperorg.content import Node |
| 16 |
from tests.helper import unify_html |
| 17 |
|
| 18 |
|
| 19 |
def _reset_node_class(): |
| 20 |
Node.nodes = {} |
| 21 |
Node.heading_ids = {} |
| 22 |
Node.input_dir = 'input_dir' |
| 23 |
exporter.Exporter.output_dir = 'output_dir' |
| 24 |
|
| 25 |
|
| 26 |
class ParagraphToHtml(unittest.TestCase): |
| 27 |
"""Test HTML from paragraphs""" |
| 28 |
# pylint: disable=protected-access |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
_reset_node_class() |
| 32 |
|
| 33 |
def test_links(self): |
| 34 |
"""Paragraph with links""" |
| 35 |
self.maxDiff = None |
| 36 |
Node.nodes['123'] = Node([], 'bar_123.org') |
| 37 |
Node.nodes['456'] = Node([], 'foo_456.org') |
| 38 |
|
| 39 |
paragraph = elements.Paragraph([ |
| 40 |
'Governments of the [[roam:Industrial World][IW]], you', |
| 41 |
'[[roam:weary giants]] of [[id:123][flesh]] and [[id:456]]', |
| 42 |
'steel, [[https://icke.world][I]] come', |
| 43 |
'from [[https://Cyberspace.net]], the new home of Mind.']) |
| 44 |
|
| 45 |
expect = '<p>Governments of the <span title="The link destination ' \ |
| 46 |
'could not be found.
Unknown ROAM "Industrial ' \ |
| 47 |
'World".
Original link: [[roam:Industrial World][IW]">' \ |
| 48 |
'👾<span class="invalid-link">IW</span></span>, you ' \ |
| 49 |
'<span title="The link destination could not be found.' \ |
| 50 |
'
Unknown ROAM "weary giants".
Original ' \ |
| 51 |
'link: [[roam:weary giants]]">👾<span class="invalid-link">' \ |
| 52 |
'weary giants</span></span> of ' \ |
| 53 |
'<a href="bar_123.html">flesh</a> and ' \ |
| 54 |
'<a href="foo_456.html">456</a> steel, ' \ |
| 55 |
'<a target="_blank" rel="noopener noreferrer" ' \ |
| 56 |
'href="https://icke.world">I</a> come ' \ |
| 57 |
'from <a target="_blank" rel="noopener noreferrer" ' \ |
| 58 |
'href="https://Cyberspace.net">https://Cyberspace.net</a>' \ |
| 59 |
', the new home of Mind.</p>' |
| 60 |
|
| 61 |
self.assertEqual( |
| 62 |
unify_html(exporter.Exporter._paragraph_to_html(paragraph)), |
| 63 |
unify_html(expect)) |
| 64 |
|
| 65 |
def test_multiline(self): |
| 66 |
"""Paragraph with inline markup.""" |
| 67 |
|
| 68 |
self.maxDiff = None |
| 69 |
|
| 70 |
paragraph = elements.Paragraph([ |
| 71 |
'Multiline verbatim =start here and will go to the next line', |
| 72 |
'after 79 chars and we still weit=.' |
| 73 |
]) |
| 74 |
expect = '<p>Multiline verbatim <code class="verbatim">start here ' \ |
| 75 |
'and will go to the ' \ |
| 76 |
'next line after 79 chars and we still weit</code>.</p>' |
| 77 |
|
| 78 |
self.assertEqual(exporter.Exporter._paragraph_to_html(paragraph), |
| 79 |
expect) |
| 80 |
|
| 81 |
def test_simple(self): |
| 82 |
"""Paragraph to html""" |
| 83 |
|
| 84 |
test_data = [ |
| 85 |
(['eins'], '<p>eins</p>'), |
| 86 |
(['eins', 'zwei'], '<p>eins zwei</p>'), |
| 87 |
] |
| 88 |
|
| 89 |
for lines, target_html in test_data: |
| 90 |
paragraph = elements.Paragraph(lines) |
| 91 |
|
| 92 |
generated_html = exporter.Exporter._paragraph_to_html(paragraph) |
| 93 |
self.assertEqual(generated_html, target_html) |
| 94 |
|
| 95 |
def test_verbatim_first_marker(self): |
| 96 |
"""Verbatim beginning marker.""" |
| 97 |
|
| 98 |
test_data = [ |
| 99 |
# no char before first marker |
| 100 |
('=example=', '<code class="verbatim">example</code>'), |
| 101 |
# space before first marker |
| 102 |
('lore =foobar= ipsum', |
| 103 |
'lore <code class="verbatim">foobar</code> ipsum'), |
| 104 |
|
| 105 |
] |
| 106 |
|
| 107 |
for markup, expect in test_data: |
| 108 |
paragraph = elements.Paragraph([markup]) |
| 109 |
self.assertEqual( |
| 110 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 111 |
'<p>' + expect + '</p>') |
| 112 |
|
| 113 |
def test_verbatim_end_marker(self): |
| 114 |
"""Verbatim end marker.""" |
| 115 |
|
| 116 |
test_data = [ |
| 117 |
# space and word at the end |
| 118 |
('=foobar= ipsum', '<code class="verbatim">foobar</code> ipsum'), |
| 119 |
# nothing at the end |
| 120 |
('lore =foobar=', 'lore <code class="verbatim">foobar</code>'), |
| 121 |
# not allowed char at the end |
| 122 |
('lore =foobar=x', 'lore =foobar=x'), |
| 123 |
] |
| 124 |
|
| 125 |
for markup, expect in test_data: |
| 126 |
paragraph = elements.Paragraph([markup]) |
| 127 |
self.assertEqual( |
| 128 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 129 |
'<p>' + expect + '</p>') |
| 130 |
|
| 131 |
def test_verbatim_multiple_markers(self): |
| 132 |
"""Verbatim with = in it.""" |
| 133 |
markup = '=foo X=1 Y=abc punkt.=' |
| 134 |
expect = '<p><code class="verbatim">foo X=1 Y=abc punkt.</code></p>' |
| 135 |
|
| 136 |
paragraph = elements.Paragraph([markup]) |
| 137 |
self.assertEqual(exporter.Exporter._paragraph_to_html(paragraph), |
| 138 |
expect) |
| 139 |
|
| 140 |
def test_multiple_markups(self): |
| 141 |
"""Multiple markup types""" |
| 142 |
test_data = [ |
| 143 |
(' _example_ ', '<p> <u>example</u> </p>'), |
| 144 |
('Underline _example_.', '<p>Underline <u>example</u>.</p>'), |
| 145 |
(' *example* ', '<p> <strong>example</strong> </p>'), |
| 146 |
('Bold *example*.', '<p>Bold <strong>example</strong>.</p>'), |
| 147 |
(' /example/ ', '<p> <em>example</em> </p>'), |
| 148 |
('Italic /example/.', '<p>Italic <em>example</em>.</p>'), |
| 149 |
(' +example+ ', '<p> <del>example</del> </p>'), |
| 150 |
('Code +example+.', '<p>Code <del>example</del>.</p>'), |
| 151 |
(' ~example~ ', '<p> <code>example</code> </p>'), |
| 152 |
('Code ~example~.', '<p>Code <code>example</code>.</p>'), |
| 153 |
('Verbatim =example=.', |
| 154 |
'<p>Verbatim <code class="verbatim">example</code>.</p>'), |
| 155 |
(' =example= ', |
| 156 |
'<p> <code class="verbatim">example</code> </p>') |
| 157 |
] |
| 158 |
|
| 159 |
for markup, expect in test_data: |
| 160 |
paragraph = elements.Paragraph([markup]) |
| 161 |
self.assertEqual( |
| 162 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 163 |
expect) |
| 164 |
|
| 165 |
def test_three_markers(self): |
| 166 |
"""Line with three markers.""" |
| 167 |
markup = 'One line with three =markers= and there= .' |
| 168 |
expect = '<p>One line with three <code class="verbatim">markers' \ |
| 169 |
'</code> and there= .</p>' |
| 170 |
|
| 171 |
paragraph = elements.Paragraph([markup]) |
| 172 |
self.assertEqual(exporter.Exporter._paragraph_to_html(paragraph), |
| 173 |
expect) |
| 174 |
|
| 175 |
def test_non_markers(self): |
| 176 |
"""Marker chars not interpreted.""" |
| 177 |
markup = 'Line with two but no-marsker: n =7 and x=9 .' |
| 178 |
expect = '<p>Line with two but no-marsker: n =7 and x=9 .</p>' |
| 179 |
|
| 180 |
paragraph = elements.Paragraph([markup]) |
| 181 |
self.assertEqual(exporter.Exporter._paragraph_to_html(paragraph), |
| 182 |
expect) |
| 183 |
|
| 184 |
def test_endings(self): |
| 185 |
"""Chars allowed after marker. |
| 186 |
|
| 187 |
To find a pair of markers the first marker need to have a blank space |
| 188 |
as prefix. But the second marker can have other chars instead of a |
| 189 |
space; e.g. a period to finish a sentence. |
| 190 |
""" |
| 191 |
|
| 192 |
self.maxDiff = None |
| 193 |
|
| 194 |
line = 'Allowed is =space= and =period=. and =question=? and ' \ |
| 195 |
'=exclamation=! and =round=) and =curley=} and =double ' \ |
| 196 |
'quotation=" and =single quotation=\' and ' \ |
| 197 |
'=double point=: and =semicolon=; and =comma=, ' \ |
| 198 |
'and =minus=- that is how it is.' |
| 199 |
|
| 200 |
expect = '<p>Allowed is <code class="verbatim">space</code> ' \ |
| 201 |
'and <code class="verbatim">period</code>. and ' \ |
| 202 |
'<code class="verbatim">question</code>? ' \ |
| 203 |
'and <code class="verbatim">exclamation</code>! and ' \ |
| 204 |
'<code class="verbatim">round</code>) ' \ |
| 205 |
'and <code class="verbatim">curley</code>} ' \ |
| 206 |
'and <code class="verbatim">double quotation</code>" ' \ |
| 207 |
'and <code class="verbatim">single quotation</code>' ' \ |
| 208 |
'and <code class="verbatim">double point</code>: ' \ |
| 209 |
'and <code class="verbatim">semicolon</code>; ' \ |
| 210 |
'and <code class="verbatim">comma</code>, ' \ |
| 211 |
'and <code class="verbatim">minus</code>- ' \ |
| 212 |
'that is how it is.</p>' |
| 213 |
|
| 214 |
paragraph = elements.Paragraph([line]) |
| 215 |
self.assertEqual(exporter.Exporter._paragraph_to_html(paragraph), |
| 216 |
expect) |
| 217 |
|
| 218 |
def test_endings_for_all(self): |
| 219 |
"""Allowed Endings for all inline markers.""" |
| 220 |
|
| 221 |
allowed_endings = ' .?!)}"\':;,-' |
| 222 |
inline_and_html = [ |
| 223 |
('=', 'code class="verbatim"'), |
| 224 |
('~', 'code'), |
| 225 |
('/', 'em'), |
| 226 |
('*', 'strong'), |
| 227 |
('_', 'u'), |
| 228 |
('+', 'del') |
| 229 |
] |
| 230 |
|
| 231 |
for marker, tag in inline_and_html: |
| 232 |
|
| 233 |
for ending in allowed_endings: |
| 234 |
|
| 235 |
orgraw = f'{marker}foo bar{marker}{ending}' |
| 236 |
ending = html.escape(ending) |
| 237 |
expect = f'<{tag}>foo bar</{tag.split()[0]}>{ending}' |
| 238 |
|
| 239 |
# no prefixed/trailing whitespace |
| 240 |
paragraph = elements.Paragraph([orgraw]) |
| 241 |
self.assertEqual( |
| 242 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 243 |
'<p>' + expect + '</p>') |
| 244 |
|
| 245 |
# prefixed whitespace |
| 246 |
paragraph = elements.Paragraph([f' {orgraw}']) |
| 247 |
self.assertEqual( |
| 248 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 249 |
f'<p> {expect}</p>') |
| 250 |
|
| 251 |
# trailing whitespace |
| 252 |
paragraph = elements.Paragraph([f'{orgraw} ']) |
| 253 |
self.assertEqual( |
| 254 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 255 |
f'<p>{expect} </p>') |
| 256 |
|
| 257 |
# prefixed & trailing whitespace |
| 258 |
paragraph = elements.Paragraph([f' {orgraw} ']) |
| 259 |
self.assertEqual( |
| 260 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 261 |
f'<p> {expect} </p>') |
| 262 |
|
| 263 |
def test_beginnings_for_all(self): |
| 264 |
"""Allowed Beginnings for all inline markers. |
| 265 |
|
| 266 |
Dev note: Orgmode itself seems to make a difference between allowed |
| 267 |
characters in the beginning and at the end of an inline marker. Not |
| 268 |
sure why. The dot is allowed at the end but not in the beginning for |
| 269 |
example. But I don't care about this and allow the same characters at |
| 270 |
beginning and end |
| 271 |
""" |
| 272 |
|
| 273 |
allowed_begins = ' .?!({"\':;,-' |
| 274 |
inline_and_html = [ |
| 275 |
('=', 'code class="verbatim"'), |
| 276 |
('~', 'code'), |
| 277 |
('/', 'em'), |
| 278 |
('*', 'strong'), |
| 279 |
('_', 'u'), |
| 280 |
('+', 'del') |
| 281 |
] |
| 282 |
|
| 283 |
for marker, tag in inline_and_html: |
| 284 |
|
| 285 |
for begin in allowed_begins: |
| 286 |
|
| 287 |
orgraw = f'{begin}{marker}foo bar{marker}' |
| 288 |
begin = html.escape(begin) |
| 289 |
expect = f'{begin}<{tag}>foo bar</{tag.split()[0]}>' |
| 290 |
|
| 291 |
# no prefixed/trailing whitespace |
| 292 |
paragraph = elements.Paragraph([orgraw]) |
| 293 |
self.assertEqual( |
| 294 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 295 |
'<p>' + expect + '</p>') |
| 296 |
|
| 297 |
# prefixed whitespace |
| 298 |
paragraph = elements.Paragraph([f' {orgraw}']) |
| 299 |
self.assertEqual( |
| 300 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 301 |
f'<p> {expect}</p>') |
| 302 |
|
| 303 |
# trailing whitespace |
| 304 |
paragraph = elements.Paragraph([f'{orgraw} ']) |
| 305 |
self.assertEqual( |
| 306 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 307 |
f'<p>{expect} </p>') |
| 308 |
|
| 309 |
# prefixed & trailing whitespace |
| 310 |
paragraph = elements.Paragraph([f' {orgraw} ']) |
| 311 |
self.assertEqual( |
| 312 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 313 |
f'<p> {expect} </p>') |
| 314 |
|
| 315 |
def test_verbatim_in_brakets(self): |
| 316 |
"""Verbatim enclosded by brackets""" |
| 317 |
verbatim = 'Foo (=C-c n l=) bar' |
| 318 |
expect = '<p>Foo (<code class="verbatim">C-c n l</code>) bar</p>' |
| 319 |
|
| 320 |
paragraph = elements.Paragraph([verbatim]) |
| 321 |
self.assertEqual( |
| 322 |
exporter.Exporter._paragraph_to_html(paragraph), expect) |
| 323 |
|
| 324 |
|
| 325 |
class InlineImagesToHtml(unittest.TestCase): |
| 326 |
"""More InlineImage stuff.""" |
| 327 |
# pylint: disable=protected-access |
| 328 |
@classmethod |
| 329 |
def setUpClass(cls): |
| 330 |
def _mock_me(*args, **kwargs): # pylint: disable=unused-argument |
| 331 |
pass |
| 332 |
cls._establish_file_link = exporter._establish_file_link |
| 333 |
exporter._establish_file_link = _mock_me |
| 334 |
|
| 335 |
@classmethod |
| 336 |
def tearDownClass(cls): |
| 337 |
exporter._establish_file_link = cls._establish_file_link |
| 338 |
|
| 339 |
def setUp(self): |
| 340 |
_reset_node_class() |
| 341 |
|
| 342 |
def test_simple(self): |
| 343 |
"""Simple inline image.""" |
| 344 |
sut = elements.Paragraph(['foo[[image.png]]bar']) |
| 345 |
|
| 346 |
self.assertEqual( |
| 347 |
exporter.Exporter._paragraph_to_html(sut), |
| 348 |
'<p>foo' |
| 349 |
'<img src="image.png" alt="image.png" />' |
| 350 |
'bar</p>' |
| 351 |
) |
| 352 |
|
| 353 |
def test_as_link_with_file_prefix(self): |
| 354 |
"""Images as labels of Links are recognized with file: prefix""" |
| 355 |
sut = elements.Paragraph(['foo[[target.png][file:image.png]]bar']) |
| 356 |
|
| 357 |
self.assertEqual( |
| 358 |
exporter.Exporter._paragraph_to_html(sut), |
| 359 |
'<p>foo' |
| 360 |
'<a target="_blank" rel="noopener noreferrer" href="target.png">' |
| 361 |
'<img src="image.png" alt="image.png" />' |
| 362 |
'</a>bar</p>' |
| 363 |
) |
| 364 |
|
| 365 |
def test_as_link_with_https_prefix(self): |
| 366 |
"""Images as labels of Links are recognized with https: prefix""" |
| 367 |
sut = elements.Paragraph(['foo[[target.png][https://image.png]]bar']) |
| 368 |
|
| 369 |
self.assertEqual( |
| 370 |
exporter.Exporter._paragraph_to_html(sut), |
| 371 |
'<p>foo' |
| 372 |
'<a target="_blank" rel="noopener noreferrer" href="target.png">' |
| 373 |
'<img src="https://image.png" alt="image.png" />' |
| 374 |
'</a>bar</p>' |
| 375 |
) |
| 376 |
|
| 377 |
def test_as_link_ignore_unsecure_http_prefix(self): |
| 378 |
"""Images as link labels are ignored when from insecure HTTP source""" |
| 379 |
sut = elements.Paragraph(['foo[[target.png][http://image.png]]bar']) |
| 380 |
|
| 381 |
self.assertEqual( |
| 382 |
exporter.Exporter._paragraph_to_html(sut), |
| 383 |
'<p>foo' |
| 384 |
'<a target="_blank" rel="noopener noreferrer" href="target.png">' |
| 385 |
'http://image.png' |
| 386 |
'</a>bar</p>' |
| 387 |
) |
| 388 |
|
| 389 |
def test_as_link_ignore_without_prefix(self): |
| 390 |
"""Images as link labels are ignored when missing "file:" or "https:" |
| 391 |
prefix""" |
| 392 |
sut = elements.Paragraph(['foo[[target.png][image.png]]bar']) |
| 393 |
|
| 394 |
self.assertEqual( |
| 395 |
exporter.Exporter._paragraph_to_html(sut), |
| 396 |
'<p>foo' |
| 397 |
'<a target="_blank" rel="noopener noreferrer" href="target.png">' |
| 398 |
'image.png' |
| 399 |
'</a>bar</p>' |
| 400 |
) |
| 401 |
|
| 402 |
|
| 403 |
class ListElementToHtml(unittest.TestCase): |
| 404 |
"""List Element to HTML""" |
| 405 |
# pylint: disable=protected-access |
| 406 |
|
| 407 |
def setUp(self): |
| 408 |
_reset_node_class() |
| 409 |
|
| 410 |
def test_simple_nested(self): |
| 411 |
"""One list with a nested list""" |
| 412 |
listelem = elements.ListElement(item='- one', ordered=False) |
| 413 |
listelem.add_item('- two') |
| 414 |
listelem.add_item(' - blue') |
| 415 |
listelem.add_item(' - red') |
| 416 |
listelem.add_item('- three') |
| 417 |
|
| 418 |
expect = ''' |
| 419 |
<ul> |
| 420 |
<li>one</li> |
| 421 |
<li>two |
| 422 |
<ul> |
| 423 |
<li>blue</li> |
| 424 |
<li>red</li> |
| 425 |
</ul></li> |
| 426 |
<li>three</li> |
| 427 |
</ul>''' |
| 428 |
|
| 429 |
self.assertEqual( |
| 430 |
unify_html(exporter.Exporter._list_to_html(listelem)), |
| 431 |
unify_html(expect)) |
| 432 |
|
| 433 |
def test_complex(self): |
| 434 |
"""Complex list (markup, links, markuped link-labels)""" |
| 435 |
self.maxDiff = None |
| 436 |
listelem = elements.ListElement(item='- one', ordered=False) |
| 437 |
listelem.add_item('- foo /two/') |
| 438 |
listelem.add_item('- blue [[https://bar.world][Bar]] link') |
| 439 |
listelem.add_item('- red [[https://bar.world][Big *Bar*]]') |
| 440 |
|
| 441 |
expect = ''' |
| 442 |
<ul> |
| 443 |
<li>one</li> |
| 444 |
<li>foo <em>two</em></li> |
| 445 |
<li>blue <a target="_blank" rel="noopener noreferrer" |
| 446 |
href="https://bar.world">Bar</a> link</li> |
| 447 |
<li>red <a target="_blank" rel="noopener noreferrer" |
| 448 |
href="https://bar.world">Big <strong>Bar</strong></a></li> |
| 449 |
</ul>''' |
| 450 |
|
| 451 |
self.assertEqual( |
| 452 |
unify_html(exporter.Exporter._list_to_html(listelem)), |
| 453 |
unify_html(expect)) |
| 454 |
|
| 455 |
def test_multiple_nested(self): |
| 456 |
"""Some nestes lists.""" |
| 457 |
test_data = [ |
| 458 |
([ |
| 459 |
'- foo', |
| 460 |
' - bar'], |
| 461 |
''' |
| 462 |
<ul> |
| 463 |
<li>foo |
| 464 |
<ul> |
| 465 |
<li>bar</li> |
| 466 |
</ul> |
| 467 |
</li> |
| 468 |
</ul>'''), |
| 469 |
(['- eins', |
| 470 |
'- zwei', |
| 471 |
' - drei'], |
| 472 |
''' |
| 473 |
<ul> |
| 474 |
<li>eins</li> |
| 475 |
<li>zwei |
| 476 |
<ul> |
| 477 |
<li>drei</li> |
| 478 |
</ul></li> |
| 479 |
</ul>'''), |
| 480 |
(['- eins', |
| 481 |
' - zwei'], |
| 482 |
''' |
| 483 |
<ul> |
| 484 |
<li>eins |
| 485 |
<ul> |
| 486 |
<li>zwei</li> |
| 487 |
</ul></li> |
| 488 |
</ul>'''), |
| 489 |
(['- one', |
| 490 |
'- eins', |
| 491 |
' - two', |
| 492 |
' - zwei', |
| 493 |
' - three', |
| 494 |
' - foo'], |
| 495 |
''' |
| 496 |
<ul> |
| 497 |
<li>one</li> |
| 498 |
<li>eins |
| 499 |
<ul> |
| 500 |
<li>two</li> |
| 501 |
<li>zwei |
| 502 |
<ul> |
| 503 |
<li>three</li> |
| 504 |
</ul></li> |
| 505 |
<li>foo</li> |
| 506 |
</ul> |
| 507 |
</li> |
| 508 |
</ul> |
| 509 |
''') |
| 510 |
] |
| 511 |
|
| 512 |
for content, target_html in test_data: |
| 513 |
listelem = elements.ListElement(content[0], ordered=False) |
| 514 |
for item in content[1:]: |
| 515 |
listelem.add_item(item) |
| 516 |
|
| 517 |
generated_html = exporter.Exporter._list_to_html(listelem) |
| 518 |
|
| 519 |
self.assertEqual( |
| 520 |
unify_html(generated_html), |
| 521 |
unify_html(target_html)) |
| 522 |
|
| 523 |
def test_inline_markup(self): |
| 524 |
"""Inline markup.""" |
| 525 |
|
| 526 |
self.maxDiff = None |
| 527 |
|
| 528 |
content = [ |
| 529 |
'- example *bold*', |
| 530 |
'- =verbatim=', |
| 531 |
'- and ~code~ or', |
| 532 |
'- _underline_', |
| 533 |
'- +killme+', |
| 534 |
'- /emphasized/'] |
| 535 |
listelem = elements.ListElement(content[0], ordered=False) |
| 536 |
for item in content[1:]: |
| 537 |
listelem.add_item(item) |
| 538 |
|
| 539 |
expected = '<ul>\n' \ |
| 540 |
'<li>example <strong>bold</strong></li>\n' \ |
| 541 |
'<li><code class="verbatim">verbatim</code></li>\n' \ |
| 542 |
'<li>and <code>code</code> or</li>\n' \ |
| 543 |
'<li><u>underline</u></li>\n' \ |
| 544 |
'<li><del>killme</del></li>\n' \ |
| 545 |
'<li><em>emphasized</em></li>\n' \ |
| 546 |
'</ul>' |
| 547 |
|
| 548 |
generated_html = exporter.Exporter._list_to_html(listelem) |
| 549 |
self.assertEqual(generated_html, expected) |
| 550 |
|
| 551 |
def test_nested_inline_markup(self): |
| 552 |
"""Nested list Inline markup.""" |
| 553 |
|
| 554 |
self.maxDiff = None |
| 555 |
|
| 556 |
content = [ |
| 557 |
'- example *bold*', |
| 558 |
'- =verbatim=', |
| 559 |
' - and ~code~ or', |
| 560 |
' - _underline_', |
| 561 |
' - /emphasized/'] |
| 562 |
listelem = elements.ListElement(content[0], ordered=False) |
| 563 |
for item in content[1:]: |
| 564 |
listelem.add_item(item) |
| 565 |
|
| 566 |
expected = ''' |
| 567 |
<ul> |
| 568 |
<li>example <strong>bold</strong></li> |
| 569 |
<li><code class="verbatim">verbatim</code> |
| 570 |
<ul> |
| 571 |
<li>and <code>code</code> or</li> |
| 572 |
<li><u>underline</u></li> |
| 573 |
<li><em>emphasized</em></li> |
| 574 |
</ul></li> |
| 575 |
</ul>''' |
| 576 |
|
| 577 |
generated_html = exporter.Exporter._list_to_html(listelem) |
| 578 |
self.assertEqual(unify_html(generated_html), unify_html(expected)) |
| 579 |
|
| 580 |
def test_with_codeblock(self): |
| 581 |
"""List with codeblock""" |
| 582 |
self.maxDiff = None |
| 583 |
|
| 584 |
# List with a code block in second item |
| 585 |
block = elements.Block( |
| 586 |
name='src', content_lines=['foo'], language='Language') |
| 587 |
listelem = elements.ListElement(item='- one', ordered=False) |
| 588 |
listelem.add_item('- two') |
| 589 |
listelem.append_to_last_item(block) |
| 590 |
|
| 591 |
expect = ''' |
| 592 |
<ul> |
| 593 |
<li>one</li> |
| 594 |
<li>two |
| 595 |
<pre title="src Language"> |
| 596 |
<span></span> |
| 597 |
foo |
| 598 |
</pre> |
| 599 |
</li> |
| 600 |
</ul>''' |
| 601 |
|
| 602 |
self.assertEqual( |
| 603 |
unify_html(exporter.Exporter._list_to_html(listelem), ''), |
| 604 |
unify_html(expect, '')) |
| 605 |
|
| 606 |
def test_with_two_codeblock(self): |
| 607 |
"""List with two codeblock""" |
| 608 |
self.maxDiff = None |
| 609 |
|
| 610 |
# List with a code block in second item |
| 611 |
blocka = elements.Block( |
| 612 |
name='src', content_lines=['foo'], language='Language') |
| 613 |
blockb = elements.Block( |
| 614 |
name='src', content_lines=['bar'], language='Language') |
| 615 |
listelem = elements.ListElement(item='- one', ordered=False) |
| 616 |
listelem.add_item('- two') |
| 617 |
listelem.append_to_last_item(blocka) |
| 618 |
listelem.append_to_last_item('three') |
| 619 |
listelem.append_to_last_item(blockb) |
| 620 |
listelem.add_item('- four') |
| 621 |
|
| 622 |
expect = ''' |
| 623 |
<ul> |
| 624 |
<li>one</li> |
| 625 |
<li>two |
| 626 |
<pre title="src Language"> |
| 627 |
<span></span> |
| 628 |
foo |
| 629 |
</pre> |
| 630 |
three |
| 631 |
<pre title="src Language"> |
| 632 |
<span></span> |
| 633 |
bar |
| 634 |
</pre> |
| 635 |
</li> |
| 636 |
<li>four</li> |
| 637 |
</ul>''' |
| 638 |
|
| 639 |
self.assertEqual( |
| 640 |
unify_html(exporter.Exporter._list_to_html(listelem), ''), |
| 641 |
unify_html(expect, '')) |
| 642 |
|
| 643 |
|
| 644 |
class HeadingToHtml(unittest.TestCase): |
| 645 |
"""Heading elements to HTML""" |
| 646 |
|
| 647 |
# pylint: disable=protected-access,missing-function-docstring |
| 648 |
|
| 649 |
def setUp(self): |
| 650 |
_reset_node_class() |
| 651 |
|
| 652 |
def test_simple(self): |
| 653 |
heading = elements.Heading('* Heading') |
| 654 |
|
| 655 |
expect = '<h2>Heading</h2>' |
| 656 |
|
| 657 |
self.assertEqual( |
| 658 |
unify_html(exporter.Exporter._heading_to_html(heading)), |
| 659 |
unify_html(expect)) |
| 660 |
|
| 661 |
def test_complex(self): |
| 662 |
heading = elements.Heading('* Heading /italic/ and +strikethrough+ ' |
| 663 |
'extra [[https://target.net][link]]') |
| 664 |
|
| 665 |
expect = ('<h2>Heading <em>italic</em> and <del>strikethrough</del>' |
| 666 |
' extra <a target="_blank" rel="noopener noreferrer" ' |
| 667 |
'href="https://target.net">link</a></h2>') |
| 668 |
|
| 669 |
self.assertEqual( |
| 670 |
unify_html(exporter.Exporter._heading_to_html(heading)), |
| 671 |
unify_html(expect)) |
| 672 |
|
| 673 |
def test_levels(self): |
| 674 |
# pylint: disable=consider-using-f-string |
| 675 |
test_data = [ |
| 676 |
('* eins', '<h2>eins</h2>'), |
| 677 |
('** eins', '<h3>eins</h3>'), |
| 678 |
('*** eins', '<h4>eins</h4>'), |
| 679 |
('**** eins', '<h5>eins</h5>'), |
| 680 |
('{} eins'.format('*' * 10), '<h11>eins</h11>'), |
| 681 |
('{} eins'.format('*' * 50), '<h51>eins</h51>'), |
| 682 |
('{} eins'.format('*' * 100), '<h101>eins</h101>'), |
| 683 |
] |
| 684 |
|
| 685 |
for content, target_html in test_data: |
| 686 |
sut = elements.Heading(content) |
| 687 |
|
| 688 |
generated_html = exporter.Exporter._heading_to_html(sut) |
| 689 |
self.assertEqual(generated_html, target_html) |
| 690 |
|
| 691 |
def test_with_markdown_verbatim(self): |
| 692 |
sut = elements.Heading('* =--squash=') |
| 693 |
expect = '<h2><code class="verbatim">--squash</code></h2>' |
| 694 |
self.assertEqual(exporter.Exporter._heading_to_html(sut), expect) |
| 695 |
|
| 696 |
def test_with_markdown_important(self): |
| 697 |
sut = elements.Heading('* *--squash*') |
| 698 |
expect = '<h2><strong>--squash</strong></h2>' |
| 699 |
self.assertEqual(exporter.Exporter._heading_to_html(sut), expect) |
| 700 |
|
| 701 |
|
| 702 |
class BlockToHtml(unittest.TestCase): |
| 703 |
"""Block elements to HTML""" |
| 704 |
|
| 705 |
# pylint: disable=protected-access,missing-function-docstring |
| 706 |
|
| 707 |
def setUp(self): |
| 708 |
_reset_node_class() |
| 709 |
|
| 710 |
def test_simple(self): |
| 711 |
sut = elements.Block('src', ['foo', 'bar'], 'Python') |
| 712 |
|
| 713 |
# syntax highlights via Pygments |
| 714 |
expect = '<pre title="src Python">\n' \ |
| 715 |
'<span></span><span class="n">foo</span>\n' \ |
| 716 |
'<span class="n">bar</span>\n' \ |
| 717 |
'</pre>' |
| 718 |
|
| 719 |
self.assertEqual( |
| 720 |
unify_html(exporter.Exporter._block_to_html(sut)), |
| 721 |
unify_html(expect)) |
| 722 |
|
| 723 |
# @mock.patch('hyperorg.exporter.pygments', return_value=None) |
| 724 |
def test_without_pygments(self): |
| 725 |
hide_pygments = exporter.pygments |
| 726 |
exporter.pygments = None |
| 727 |
|
| 728 |
sut = elements.Block('src', ['foo', 'bar'], 'Python') |
| 729 |
|
| 730 |
# syntax highlights via Pygments |
| 731 |
expect = '<pre title="src Python">\n' \ |
| 732 |
'foo\n' \ |
| 733 |
'bar' \ |
| 734 |
'</pre>' |
| 735 |
|
| 736 |
self.assertEqual( |
| 737 |
unify_html(exporter.Exporter._block_to_html(sut)), |
| 738 |
unify_html(expect)) |
| 739 |
|
| 740 |
exporter.pygments = hide_pygments |
| 741 |
|
| 742 |
def test_html_block_without_pygments(self): |
| 743 |
hide_pygments = exporter.pygments |
| 744 |
exporter.pygments = None |
| 745 |
|
| 746 |
sut = elements.Block( |
| 747 |
'src', |
| 748 |
['<p>Worf & Diana</p>'], |
| 749 |
'doesntmatter' |
| 750 |
) |
| 751 |
|
| 752 |
# Html like escaped: < |
| 753 |
expect = '<pre title="src doesntmatter">\n' \ |
| 754 |
'<p>Worf & Diana</p>' \ |
| 755 |
'</pre>' |
| 756 |
|
| 757 |
self.assertEqual( |
| 758 |
unify_html(exporter.Exporter._block_to_html(sut)), |
| 759 |
unify_html(expect)) |
| 760 |
|
| 761 |
exporter.pygments = hide_pygments |
| 762 |
|
| 763 |
|
| 764 |
class EscapeHTMLEntities(unittest.TestCase): |
| 765 |
"""Escaping of HTML entities. |
| 766 |
|
| 767 |
Such an entity is a special character. For example "<" is converted into |
| 768 |
"<" in HTML code. Or "&" into "&". |
| 769 |
|
| 770 |
Development note: This escaping is part of HTML and shouldn't be in the |
| 771 |
content layer which is format independent by design. Rework that! |
| 772 |
""" |
| 773 |
# pylint: disable=protected-access,missing-function-docstring |
| 774 |
org_string = "< > & \" '" |
| 775 |
html_string = "< > & " '" |
| 776 |
|
| 777 |
def test_angle_brackets(self): |
| 778 |
"""Angle brackets in paragraph.""" |
| 779 |
|
| 780 |
paragraph = elements.Paragraph(['Entry <[email protected]>']) |
| 781 |
result = exporter.Exporter._paragraph_to_html(paragraph) |
| 782 |
|
| 783 |
self.assertEqual(result, '<p>Entry <[email protected]></p>') |
| 784 |
|
| 785 |
def test_paragraph(self): |
| 786 |
paragraph = elements.Paragraph( |
| 787 |
['foo', EscapeHTMLEntities.org_string, 'bar']) |
| 788 |
|
| 789 |
self.assertEqual( |
| 790 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 791 |
f'<p>foo {EscapeHTMLEntities.html_string} bar</p>') |
| 792 |
|
| 793 |
def test_link(self): |
| 794 |
paragraph = elements.Paragraph([ |
| 795 |
f'[[https://123.net][{EscapeHTMLEntities.org_string}]]' |
| 796 |
]) |
| 797 |
|
| 798 |
self.assertEqual( |
| 799 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 800 |
'<p><a target="_blank" rel="noopener noreferrer" ' |
| 801 |
f'href="https://123.net">{EscapeHTMLEntities.html_string}' |
| 802 |
'</a></p>') |
| 803 |
|
| 804 |
def test_paragraph_bold(self): |
| 805 |
paragraph = elements.Paragraph( |
| 806 |
[f'Bold *{EscapeHTMLEntities.org_string}*']) |
| 807 |
|
| 808 |
self.assertEqual( |
| 809 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 810 |
f'<p>Bold <strong>{EscapeHTMLEntities.html_string}</strong></p>') |
| 811 |
|
| 812 |
def test_paragraph_italic(self): |
| 813 |
paragraph = elements.Paragraph( |
| 814 |
[f'Italic /{EscapeHTMLEntities.org_string}/']) |
| 815 |
|
| 816 |
self.assertEqual( |
| 817 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 818 |
f'<p>Italic <em>{EscapeHTMLEntities.html_string}</em></p>') |
| 819 |
|
| 820 |
def test_paragraph_underline(self): |
| 821 |
paragraph = elements.Paragraph( |
| 822 |
[f'Underline _{EscapeHTMLEntities.org_string}_']) |
| 823 |
|
| 824 |
self.assertEqual( |
| 825 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 826 |
f'<p>Underline <u>{EscapeHTMLEntities.html_string}</u></p>') |
| 827 |
|
| 828 |
def test_paragraph_code_verbatim(self): |
| 829 |
paragraph = elements.Paragraph([ |
| 830 |
f'Code ~{EscapeHTMLEntities.org_string}~', |
| 831 |
f'Verbatim ={EscapeHTMLEntities.org_string}=']) |
| 832 |
|
| 833 |
self.assertEqual( |
| 834 |
exporter.Exporter._paragraph_to_html(paragraph), |
| 835 |
f'<p>Code <code>{EscapeHTMLEntities.html_string}</code> ' |
| 836 |
'Verbatim <code class="verbatim">' |
| 837 |
f'{EscapeHTMLEntities.html_string}</code></p>' |
| 838 |
) |
| 839 |
|
| 840 |
def test_list(self): |
| 841 |
listelem = elements.ListElement(f'- {EscapeHTMLEntities.org_string}', |
| 842 |
False) |
| 843 |
|
| 844 |
self.assertEqual( |
| 845 |
unify_html(exporter.Exporter._list_to_html(listelem)), |
| 846 |
f'<ul><li>{EscapeHTMLEntities.html_string}</li></ul>' |
| 847 |
) |
| 848 |
|
| 849 |
def test_heading(self): |
| 850 |
heading = elements.Heading(f'* {EscapeHTMLEntities.org_string}') |
| 851 |
|
| 852 |
self.assertEqual( |
| 853 |
exporter.Exporter._heading_to_html(heading), |
| 854 |
f'<h2>{EscapeHTMLEntities.html_string}</h2>') |
| 855 |
|
| 856 |
|
| 857 |
class DisplayMathToHtml(unittest.TestCase): |
| 858 |
"""Test HTML out of Math blocks""" |
| 859 |
# pylint: disable=protected-access,missing-function-docstring |
| 860 |
|
| 861 |
def test_display(self): |
| 862 |
# self.maxDiff = None |
| 863 |
sut = elements.DisplayMath('e = mc^{2}') |
| 864 |
|
| 865 |
expected_html = r'<div class="math">\[e = mc^{2}\]</div>' |
| 866 |
|
| 867 |
self.assertEqual( |
| 868 |
exporter.Exporter._displaymath_to_html(sut), |
| 869 |
expected_html |
| 870 |
) |
| 871 |
|
| 872 |
|
| 873 |
class BacklinksToHtml(unittest.TestCase): |
| 874 |
"""Backlinks as HTML""" |
| 875 |
# pylint: disable=protected-access |
| 876 |
|
| 877 |
def setUp(self): |
| 878 |
_reset_node_class() |
| 879 |
|
| 880 |
def test_one_not_found(self): |
| 881 |
"""One backlink with unknown orgid""" |
| 882 |
|
| 883 |
sut = elements.Backlinks() |
| 884 |
sut.add_link(textruns.Link( |
| 885 |
target='rainbow', |
| 886 |
link_type=textruns.Link.Type.ORGID, |
| 887 |
label_runs=['Over the rainbow'] |
| 888 |
)) |
| 889 |
|
| 890 |
expect = ''.join([ |
| 891 |
'</section>', |
| 892 |
'<section>', |
| 893 |
'<hr />', |
| 894 |
'<h2>Backlinks</h2>', |
| 895 |
'<ul>', |
| 896 |
'<li>', |
| 897 |
'<span title="The link destination could not be found.
' |
| 898 |
'Unknown OrgID "rainbow".
' |
| 899 |
'Original link: [[id:rainbow][Over the rainbow]">', |
| 900 |
'⚠<span class="invalid-link">Over the rainbow</span>', |
| 901 |
'</span>', |
| 902 |
'</li>', |
| 903 |
'</ul>' |
| 904 |
]) |
| 905 |
self.maxDiff = None |
| 906 |
self.assertEqual( |
| 907 |
unify_html(exporter.Exporter._backlinks_to_html(sut)), |
| 908 |
unify_html(expect) |
| 909 |
) |
| 910 |
|
| 911 |
def test_one_valid(self): |
| 912 |
"""One backlink with valid orgid""" |
| 913 |
|
| 914 |
target = 'rainbow' |
| 915 |
Node.nodes[target] = Node([], f'{target}.org') |
| 916 |
|
| 917 |
sut = elements.Backlinks() |
| 918 |
sut.add_link(textruns.Link( |
| 919 |
target=target, |
| 920 |
link_type=textruns.Link.Type.ORGID, |
| 921 |
label_runs=['Over the rainbow'] |
| 922 |
)) |
| 923 |
|
| 924 |
expect = ''.join([ |
| 925 |
'</section>', |
| 926 |
'<section>', |
| 927 |
'<hr />', |
| 928 |
'<h2>Backlinks</h2>', |
| 929 |
'<ul>', |
| 930 |
'<li>', |
| 931 |
f'<a href="{target}.html">Over the rainbow</a>', |
| 932 |
'</li>', |
| 933 |
'</ul>' |
| 934 |
]) |
| 935 |
self.maxDiff = None |
| 936 |
self.assertEqual( |
| 937 |
unify_html(exporter.Exporter._backlinks_to_html(sut)), |
| 938 |
unify_html(expect) |
| 939 |
) |