| 1 |
# SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]> |
| 2 |
# SPDX-FileCopyrightText: © 2025 Michael Olie <[email protected]> |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: GPL-3.0-only |
| 5 |
# |
| 6 |
# This file is part of the program "Hyperorg" which is released under GNU |
| 7 |
# General Public License v3 (GPLv3). |
| 8 |
# See folder LICENSES or go to <https://www.gnu.org/licenses/#GPL>. |
| 9 |
"""Tests about generating HTML from Runs using `exporter._html_from_runs()`.""" |
| 10 |
# pylint: disable=protected-access,missing-function-docstring |
| 11 |
import unittest |
| 12 |
from hyperorg import exporter |
| 13 |
from hyperorg import textruns |
| 14 |
from hyperorg.content import Node |
| 15 |
|
| 16 |
|
| 17 |
def _reset_node_class(): |
| 18 |
Node.nodes = {} |
| 19 |
Node.heading_ids = {} |
| 20 |
Node.input_dir = 'input_dir' |
| 21 |
exporter.Exporter.output_dir = 'output_dir' |
| 22 |
|
| 23 |
|
| 24 |
class HtmlFromRuns_Markup(unittest.TestCase): # pylint: disable=C0103 |
| 25 |
"""Test HTML out of markup runs""" |
| 26 |
|
| 27 |
def test_no_markup_text(self): |
| 28 |
runs = ['foobar'] |
| 29 |
|
| 30 |
self.assertEqual(exporter._html_from_runs(runs), 'foobar') |
| 31 |
|
| 32 |
def test_markup(self): |
| 33 |
runs = [ |
| 34 |
textruns.Markup('foobar', textruns.Markup.Type.IMPORTANT) |
| 35 |
] |
| 36 |
|
| 37 |
self.assertEqual( |
| 38 |
exporter._html_from_runs(runs), |
| 39 |
'<strong>foobar</strong>') |
| 40 |
|
| 41 |
|
| 42 |
class HtmlFromRuns_Links(unittest.TestCase): # pylint: disable=C0103 |
| 43 |
"""Test HTML out of link runs""" |
| 44 |
|
| 45 |
# pylint: disable=protected-access |
| 46 |
# pylint: disable=missing-function-docstring |
| 47 |
# pylint: disable=too-many-public-methods |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def setUpClass(cls): |
| 51 |
# pylint: disable=duplicate-code |
| 52 |
def _mock_me(*args, **kwargs): # pylint: disable=unused-argument |
| 53 |
pass |
| 54 |
cls._establish_file_link = exporter._establish_file_link |
| 55 |
exporter._establish_file_link = _mock_me |
| 56 |
|
| 57 |
@classmethod |
| 58 |
def tearDownClass(cls): |
| 59 |
# pylint: disable=duplicate-code |
| 60 |
exporter._establish_file_link = cls._establish_file_link |
| 61 |
|
| 62 |
def setUp(self): |
| 63 |
_reset_node_class() |
| 64 |
|
| 65 |
def test_orgid(self): |
| 66 |
Node.nodes['123'] = Node([], '123_foo.org') |
| 67 |
runs = [ |
| 68 |
textruns.Link.from_org_string('id:123][normal'), |
| 69 |
' and ', |
| 70 |
textruns.Link.from_org_string('id:123][two words'), |
| 71 |
' and ', |
| 72 |
textruns.Link.from_org_string('id:123][with\nlinebreak'), |
| 73 |
] |
| 74 |
|
| 75 |
expect_html = '<a href="123_foo.html">normal</a> and ' \ |
| 76 |
'<a href="123_foo.html">two words</a> and ' \ |
| 77 |
'<a href="123_foo.html">with\nlinebreak</a>' |
| 78 |
|
| 79 |
self.assertEqual(exporter._html_from_runs(runs), expect_html) |
| 80 |
|
| 81 |
def test_orgid_without_label(self): |
| 82 |
Node.nodes['123'] = Node([], '123_foo.org') |
| 83 |
runs = [textruns.Link.from_org_string('id:123')] |
| 84 |
|
| 85 |
expect_html = '<a href="123_foo.html">123</a>' |
| 86 |
|
| 87 |
self.assertEqual(exporter._html_from_runs(runs), expect_html) |
| 88 |
|
| 89 |
def test_orgid_target_unknown(self): |
| 90 |
"""Org-roam-ID unknown with label""" |
| 91 |
runs = [textruns.Link.from_org_string('id:123][normal')] |
| 92 |
|
| 93 |
self.assertRegex( |
| 94 |
exporter._html_from_runs(runs), |
| 95 |
# title attribute, "Unknown", OrgID, label ... |
| 96 |
r'.*title=.*Unknown.*OrgID.*id\:123.*normal.*' |
| 97 |
# ... style class, label |
| 98 |
r'class=\"invalid-link\".*normal.*' |
| 99 |
) |
| 100 |
|
| 101 |
def test_orgid_target_unknown_without_label(self): |
| 102 |
"""Org-roam-ID unknown without a label""" |
| 103 |
runs = [textruns.Link.from_org_string('id:123')] |
| 104 |
|
| 105 |
self.assertRegex( |
| 106 |
exporter._html_from_runs(runs), |
| 107 |
# title attribute, "Unknown", OrgID, label ... |
| 108 |
r'.*title=.*Unknown*.OrgID.*id\:123.*' |
| 109 |
# ... style class, label |
| 110 |
r'class=\"invalid-link\".*123.*' |
| 111 |
) |
| 112 |
|
| 113 |
def test_roam_unknown(self): |
| 114 |
"""Roam link (is always unknown)""" |
| 115 |
# textruns.Link('roam:target'), |
| 116 |
runs = [textruns.Link.from_org_string('roam:word][label')] |
| 117 |
|
| 118 |
self.assertRegex( |
| 119 |
exporter._html_from_runs(runs), |
| 120 |
# title attribute, "Unknown", ROAM, label ... |
| 121 |
r'.*title=.*Unknown.*ROAM.*roam\:word.*' |
| 122 |
# ... style class, label |
| 123 |
r'class=\"invalid-link\".*label.*' |
| 124 |
) |
| 125 |
|
| 126 |
def test_roam_unknown_without_label(self): |
| 127 |
"""Roam link (is always unknown) without label""" |
| 128 |
# textruns.Link('roam:target'), |
| 129 |
runs = [textruns.Link.from_org_string('roam:word')] |
| 130 |
|
| 131 |
self.assertRegex( |
| 132 |
exporter._html_from_runs(runs), |
| 133 |
# title attribute, "Unknown", ROAM ... |
| 134 |
r'.*title=.*Unknown.*ROAM.*roam\:word.*' |
| 135 |
# ... style class, target |
| 136 |
r'class=\"invalid-link\".*word.*' |
| 137 |
) |
| 138 |
|
| 139 |
def test_https(self): |
| 140 |
"""Hyperlinks with and without labels to href.""" |
| 141 |
test_data = [ |
| 142 |
# Link with label |
| 143 |
( |
| 144 |
[textruns.Link.from_org_string('https://foo.bar][foobar')], |
| 145 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 146 |
'href="https://foo.bar">foobar</a>' |
| 147 |
), |
| 148 |
# Link without label |
| 149 |
( |
| 150 |
[textruns.Link.from_org_string('https://foo.bar')], |
| 151 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 152 |
'href="https://foo.bar">https://foo.bar</a>' |
| 153 |
), |
| 154 |
# two links with(out) label |
| 155 |
( |
| 156 |
[ |
| 157 |
textruns.Link.from_org_string('https://foo.bar][foobar'), |
| 158 |
' and ', |
| 159 |
textruns.Link.from_org_string('https://foo.bar') |
| 160 |
], |
| 161 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 162 |
'href="https://foo.bar">foobar</a> and ' |
| 163 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 164 |
'href="https://foo.bar">https://foo.bar</a>' |
| 165 |
), |
| 166 |
# two links other way around |
| 167 |
( |
| 168 |
[ |
| 169 |
textruns.Link.from_org_string('https://foo.bar'), |
| 170 |
' and ', |
| 171 |
textruns.Link.from_org_string('https://foo.bar][foobar') |
| 172 |
], |
| 173 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 174 |
'href="https://foo.bar">https://foo.bar</a> and ' |
| 175 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 176 |
'href="https://foo.bar">foobar</a>' |
| 177 |
), |
| 178 |
] |
| 179 |
|
| 180 |
for runs, expect in test_data: |
| 181 |
self.assertEqual(exporter._html_from_runs(runs), expect) |
| 182 |
|
| 183 |
def test_https_linebreaks(self): |
| 184 |
"""Hyperlinks with linebreaks.""" |
| 185 |
runs = [textruns.Link.from_org_string('https://foo.bar][foo\nbar')] |
| 186 |
expect = '<a target="_blank" rel="noopener noreferrer" ' \ |
| 187 |
'href="https://foo.bar">foo\nbar</a>' |
| 188 |
|
| 189 |
self.assertEqual(exporter._html_from_runs(runs), expect) |
| 190 |
|
| 191 |
def test_doi(self): |
| 192 |
self.assertEqual( |
| 193 |
exporter._html_from_runs( |
| 194 |
[textruns.Link.from_org_string('doi:10.1055/a-1961-1145')] |
| 195 |
), |
| 196 |
'DOI: <a target="_blank" rel="noopener noreferrer" ' |
| 197 |
'href="https://doi.org/10.1055/a-1961-1145">' |
| 198 |
'10.1055/a-1961-1145</a>' |
| 199 |
) |
| 200 |
|
| 201 |
def test_doi_label(self): |
| 202 |
self.assertEqual( |
| 203 |
exporter._html_from_runs( |
| 204 |
[textruns.Link.from_org_string( |
| 205 |
'doi:10.1055/a-1961-1145][Inanspruchnahme')] |
| 206 |
), |
| 207 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 208 |
'href="https://doi.org/10.1055/a-1961-1145">' |
| 209 |
'Inanspruchnahme</a>' |
| 210 |
) |
| 211 |
|
| 212 |
def test_mailto(self): |
| 213 |
self.assertEqual( |
| 214 |
exporter._html_from_runs( |
| 215 |
[textruns.Link.from_org_string('mailto:[email protected]')] |
| 216 |
), |
| 217 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 218 |
'href="mailto:[email protected]">[email protected]</a>' |
| 219 |
) |
| 220 |
|
| 221 |
def test_mailto_label(self): |
| 222 |
self.assertEqual( |
| 223 |
exporter._html_from_runs([ |
| 224 |
textruns.Link.from_org_string( |
| 225 |
'mailto:[email protected]][Maintainer' |
| 226 |
) |
| 227 |
]), |
| 228 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 229 |
'href="mailto:[email protected]">Maintainer</a>' |
| 230 |
) |
| 231 |
|
| 232 |
def test_file(self): |
| 233 |
self.assertEqual( |
| 234 |
exporter._html_from_runs( |
| 235 |
[textruns.Link.from_org_string('file:foobar.dat')] |
| 236 |
), |
| 237 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 238 |
'href="foobar.dat">foobar.dat</a>' |
| 239 |
) |
| 240 |
|
| 241 |
def test_file_label(self): |
| 242 |
self.assertEqual( |
| 243 |
exporter._html_from_runs( |
| 244 |
[textruns.Link.from_org_string('file:foobar.dat][FOOBAR.DAT')] |
| 245 |
), |
| 246 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 247 |
'href="foobar.dat">FOOBAR.DAT</a>' |
| 248 |
) |
| 249 |
|
| 250 |
def test_no_type(self): |
| 251 |
self.assertEqual( |
| 252 |
exporter._html_from_runs( |
| 253 |
[textruns.Link.from_org_string('foobar.dat')] |
| 254 |
), |
| 255 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 256 |
'href="foobar.dat">foobar.dat</a>' |
| 257 |
) |
| 258 |
|
| 259 |
def test_no_type_label(self): |
| 260 |
self.assertEqual( |
| 261 |
exporter._html_from_runs( |
| 262 |
[textruns.Link.from_org_string('foobar.dat][LaBeL')] |
| 263 |
), |
| 264 |
'<a target="_blank" rel="noopener noreferrer" ' |
| 265 |
'href="foobar.dat">LaBeL</a>' |
| 266 |
) |
| 267 |
|
| 268 |
def test_unsecure_http(self): |
| 269 |
"""HTTP links are not secure but will be parsed. |
| 270 |
|
| 271 |
See #29 about an improvement of that behavior |
| 272 |
unsafe (because of HTTP-only) hyperlinks not parsed |
| 273 |
""" |
| 274 |
runs = [textruns.Link.from_org_string('http://foo.bar][foobar')] |
| 275 |
|
| 276 |
self.assertRegex( |
| 277 |
exporter._html_from_runs(runs), |
| 278 |
# title attribute, "Unknown", ROAM, label ... |
| 279 |
r'.*title=.*security.*HTTP.*alternative.*http\://foo.bar.*' |
| 280 |
# ... style class, label |
| 281 |
r'class=\"insecure-link\".*foobar.*' |
| 282 |
) |
| 283 |
|
| 284 |
def test_unsecure_http_without_label(self): |
| 285 |
"""HTTP links are not secure but will be parsed. |
| 286 |
|
| 287 |
See #29 about an improvement of that behavior |
| 288 |
unsafe (because of HTTP-only) hyperlinks not parsed |
| 289 |
""" |
| 290 |
runs = [textruns.Link.from_org_string('http://foo.bar')] |
| 291 |
|
| 292 |
self.assertRegex( |
| 293 |
exporter._html_from_runs(runs), |
| 294 |
# title attribute, "Unknown", ROAM, label ... |
| 295 |
r'.*title=.*security.*HTTP.*alternative.*http\://foo.bar.*' |
| 296 |
# ... style class, label |
| 297 |
r'class=\"insecure-link\".*foo.bar.*' |
| 298 |
) |
| 299 |
|
| 300 |
def test_unknown_type(self): |
| 301 |
"""Link with unknown type (protocol).""" |
| 302 |
runs = [textruns.Link.from_org_string('fancytype:rainbow][label')] |
| 303 |
|
| 304 |
self.assertRegex( |
| 305 |
exporter._html_from_runs(runs), |
| 306 |
r'.*title=.*link type.*fancytype.*unknown.*rainbow.*label.*' |
| 307 |
r'class=\"invalid-link\".*fancytype.*label.*' |
| 308 |
) |
| 309 |
|
| 310 |
def test_unknown_type_without_label(self): |
| 311 |
"""Link with unknown type (protocol) without a label.""" |
| 312 |
runs = [textruns.Link.from_org_string('fancytype:rainbow')] |
| 313 |
|
| 314 |
self.assertRegex( |
| 315 |
exporter._html_from_runs(runs), |
| 316 |
r'.*title=.*link type.*fancytype.*unknown.*rainbow.*' |
| 317 |
r'class=\"invalid-link\".*fancytype.*rainbow.*' |
| 318 |
) |
| 319 |
|
| 320 |
|
| 321 |
class HtmlFromRuns_InlineImages(unittest.TestCase): # pylint: disable=C0103 |
| 322 |
"""Test HTML out of inline images""" |
| 323 |
# pylint: disable=protected-access |
| 324 |
@classmethod |
| 325 |
def setUpClass(cls): |
| 326 |
def _mock_me(*args, **kwargs): # pylint: disable=unused-argument |
| 327 |
pass |
| 328 |
cls._establish_file_link = exporter._establish_file_link |
| 329 |
exporter._establish_file_link = _mock_me |
| 330 |
|
| 331 |
@classmethod |
| 332 |
def tearDownClass(cls): |
| 333 |
exporter._establish_file_link = cls._establish_file_link |
| 334 |
|
| 335 |
def test_inline_image(self): |
| 336 |
"""Inline image""" |
| 337 |
sut = [ |
| 338 |
'foo', |
| 339 |
textruns.Image('target.png'), |
| 340 |
'bar' |
| 341 |
] |
| 342 |
|
| 343 |
self.assertEqual( |
| 344 |
exporter._html_from_runs(sut), |
| 345 |
'foo<img src="target.png" alt="target.png" />bar' |
| 346 |
) |
| 347 |
|
| 348 |
def test_inline_image_as_link(self): |
| 349 |
"""Linked inline image""" |
| 350 |
sut = [ |
| 351 |
'foo', |
| 352 |
textruns.Link.from_org_string('target.png][file:image.png'), |
| 353 |
'bar' |
| 354 |
] |
| 355 |
|
| 356 |
self.assertEqual( |
| 357 |
exporter._html_from_runs(sut), |
| 358 |
'foo' |
| 359 |
'<a target="_blank" rel="noopener noreferrer" href="target.png">' |
| 360 |
'<img src="image.png" alt="image.png" />' |
| 361 |
'</a>bar' |
| 362 |
) |
| 363 |
|
| 364 |
|
| 365 |
class HtmlFromRuns_Math(unittest.TestCase): # pylint: disable=C0103 |
| 366 |
"""Test HTML out of Math runs""" |
| 367 |
|
| 368 |
def test_inline(self): |
| 369 |
sut = [textruns.InlineMath('e = mc^{2}')] |
| 370 |
|
| 371 |
expected_html = r'<span class="math">\(e = mc^{2}\)</span>' |
| 372 |
|
| 373 |
self.assertEqual(exporter._html_from_runs(sut), expected_html) |