| 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 related to parser.py |
| 10 |
|
| 11 |
Org content parsed into runs and elements |
| 12 |
""" |
| 13 |
import unittest |
| 14 |
from hyperorg import textruns |
| 15 |
from hyperorg import elements |
| 16 |
from hyperorg import parser |
| 17 |
|
| 18 |
|
| 19 |
class Paragraphs(unittest.TestCase): |
| 20 |
"""Parse paragraphs""" |
| 21 |
|
| 22 |
def test_single(self): |
| 23 |
"""One paragraph with three lines.""" |
| 24 |
|
| 25 |
content = [ |
| 26 |
'lorem ipsum dolor', |
| 27 |
'sit amet,', |
| 28 |
'consetetur sadipscing elitr' |
| 29 |
] |
| 30 |
|
| 31 |
_, sut = parser.OrgContentParser(content).result() |
| 32 |
|
| 33 |
self.assertEqual(len(sut), 1) |
| 34 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 35 |
self.assertEqual( |
| 36 |
sut[0][:], |
| 37 |
['lorem ipsum dolor sit amet, consetetur sadipscing elitr'] |
| 38 |
) |
| 39 |
|
| 40 |
def test_three(self): |
| 41 |
"""Three paragraphs.""" |
| 42 |
|
| 43 |
content = [ |
| 44 |
'paragraph 1 lorem ipsum', |
| 45 |
'lorem ipsum dolor', |
| 46 |
'', |
| 47 |
'paragraph 2 dolor sit amet, consetetur', |
| 48 |
'sadipscing elitr, sed', |
| 49 |
'diam nonumy eirmod tempor', |
| 50 |
'', |
| 51 |
'paragraph 3 invidunt', |
| 52 |
'aliquyam erat,' |
| 53 |
] |
| 54 |
|
| 55 |
_, sut = parser.OrgContentParser(content).result() |
| 56 |
|
| 57 |
self.assertEqual(len(sut), 3) |
| 58 |
|
| 59 |
for idx in range(2): |
| 60 |
self.assertIsInstance(sut[idx], elements.Paragraph) |
| 61 |
|
| 62 |
expected_paragraphs = [ |
| 63 |
# paragraph 1 |
| 64 |
'paragraph 1 lorem ipsum lorem ipsum dolor', |
| 65 |
# paragraph 2 |
| 66 |
'paragraph 2 dolor sit amet, consetetur sadipscing elitr, sed ' |
| 67 |
'diam nonumy eirmod tempor', |
| 68 |
# paragraph 3 |
| 69 |
'paragraph 3 invidunt aliquyam erat,' |
| 70 |
] |
| 71 |
|
| 72 |
for idx in range(0): |
| 73 |
self.assertEqual(sut[idx][:], expected_paragraphs[idx]) |
| 74 |
|
| 75 |
def test_mixed_with_blocks(self): |
| 76 |
"""Blocks and paragraphs combined""" |
| 77 |
content = [ |
| 78 |
'Foo', |
| 79 |
'#+begin_src python', |
| 80 |
'import eins', |
| 81 |
'print("eins")', |
| 82 |
'#+end_src', |
| 83 |
'Bar', |
| 84 |
'#+begin_src Cpp', |
| 85 |
'import zwei', |
| 86 |
'print("zwei")', |
| 87 |
'#+end_src', |
| 88 |
'Letzte Zeile' |
| 89 |
] |
| 90 |
_, sut = parser.OrgContentParser(content).result() |
| 91 |
|
| 92 |
# five elements |
| 93 |
self.assertEqual(len(sut), 5) |
| 94 |
|
| 95 |
# the paragraphs |
| 96 |
self.assertEqual(sut[0], elements.Paragraph(['Foo'])) |
| 97 |
self.assertEqual(sut[2], elements.Paragraph(['Bar'])) |
| 98 |
self.assertEqual(sut[4], elements.Paragraph(['Letzte Zeile'])) |
| 99 |
|
| 100 |
self.assertEqual(sut[1], elements.Block( |
| 101 |
'src', ['import eins', 'print("eins")'], 'python')) |
| 102 |
|
| 103 |
self.assertEqual(sut[3], elements.Block( |
| 104 |
'src', ['import zwei', 'print("zwei")'], 'Cpp')) |
| 105 |
|
| 106 |
|
| 107 |
class Headings(unittest.TestCase): |
| 108 |
"""Test parsing Headings""" |
| 109 |
|
| 110 |
def test_simple(self): |
| 111 |
"""One heading""" |
| 112 |
content = ['* Heading'] |
| 113 |
_, sut = parser.OrgContentParser(content).result() |
| 114 |
|
| 115 |
self.assertEqual(len(sut), 1) |
| 116 |
self.assertIsInstance(sut[0], elements.Heading) |
| 117 |
self.assertEqual(sut[0][0], 'Heading') |
| 118 |
|
| 119 |
def test_multiple(self): |
| 120 |
"""Three headings""" |
| 121 |
content = [ |
| 122 |
'* Heading 1', |
| 123 |
'* Heading 2', |
| 124 |
'* Heading 3' |
| 125 |
] |
| 126 |
_, sut = parser.OrgContentParser(content).result() |
| 127 |
self.assertEqual(len(sut), 3) |
| 128 |
for idx in (1, 2, 3): |
| 129 |
self.assertEqual(sut[idx-1].runs, [f'Heading {idx}']) |
| 130 |
|
| 131 |
def test_heading_levels(self): |
| 132 |
"""Headings on multiple levels.""" |
| 133 |
|
| 134 |
content = [ |
| 135 |
'* Heading 1', |
| 136 |
'** Heading 2', |
| 137 |
'*** Heading 3', |
| 138 |
'**** Heading 4', |
| 139 |
'***** Heading 5', |
| 140 |
'****** Heading 6', |
| 141 |
'******* Heading 7', |
| 142 |
'******** Heading 8', |
| 143 |
'********* Heading 9' |
| 144 |
] |
| 145 |
_, sut = parser.OrgContentParser(content).result() |
| 146 |
|
| 147 |
self.assertEqual(len(sut), 9) |
| 148 |
|
| 149 |
for idx in range(9): |
| 150 |
self.assertTrue(isinstance(sut[idx], elements.Heading)) |
| 151 |
self.assertEqual(sut[idx].runs, [f'Heading {idx+1}']) |
| 152 |
|
| 153 |
def test_mix_with_paragraphs(self): |
| 154 |
"""Mixing headings and paragraphs.""" |
| 155 |
|
| 156 |
content = [ |
| 157 |
'* Heading 1', |
| 158 |
'lore ipsum', |
| 159 |
'and more', |
| 160 |
'** Heading 2', |
| 161 |
'one', |
| 162 |
'', |
| 163 |
'two', |
| 164 |
'*Heading 3' |
| 165 |
] |
| 166 |
_, sut = parser.OrgContentParser(content).result() |
| 167 |
|
| 168 |
self.assertEqual(sut[0], elements.Heading('* Heading 1')) |
| 169 |
self.assertEqual(sut[1][:], ['lore ipsum and more']) |
| 170 |
self.assertEqual(sut[2], elements.Heading('** Heading 2')) |
| 171 |
self.assertEqual(sut[3][:], ['one']) |
| 172 |
self.assertEqual(sut[4][:], ['two *Heading 3']) |
| 173 |
self.assertEqual(len(sut), 5) |
| 174 |
|
| 175 |
def test_paragraph_begin_with_markup(self): |
| 176 |
"""The paragraph start with markup""" |
| 177 |
content = [ |
| 178 |
'* Heading', |
| 179 |
'*bold*: Foo.' |
| 180 |
] |
| 181 |
_, sut = parser.OrgContentParser(content).result() |
| 182 |
|
| 183 |
self.assertEqual(len(sut), 2) |
| 184 |
self.assertEqual(sut[0], elements.Heading('* Heading')) |
| 185 |
self.assertEqual(len(sut[1]), 2) |
| 186 |
self.assertEqual( |
| 187 |
sut[1][0], |
| 188 |
textruns.Markup('bold', textruns.Markup.Type.IMPORTANT)) |
| 189 |
self.assertEqual(sut[1][1], ': Foo.') |
| 190 |
|
| 191 |
|
| 192 |
class Blocks(unittest.TestCase): |
| 193 |
"""Parse (code) blocks""" |
| 194 |
|
| 195 |
def test_one_liner(self): |
| 196 |
"""Codeblock with one line only""" |
| 197 |
content = [ |
| 198 |
'#+begin_src python', |
| 199 |
'import hyperorg', |
| 200 |
'#+end_src' |
| 201 |
] |
| 202 |
|
| 203 |
_, sut = parser.OrgContentParser(content).result() |
| 204 |
|
| 205 |
self.assertEqual(len(sut), 1) |
| 206 |
|
| 207 |
block = sut[0] |
| 208 |
self.assertIsInstance(block, elements.Block) |
| 209 |
|
| 210 |
# test block content |
| 211 |
self.assertEqual(block.name, 'src') |
| 212 |
self.assertEqual(block.language, 'python') |
| 213 |
self.assertEqual(block[0], 'import hyperorg') |
| 214 |
|
| 215 |
def test_multiple_lines(self): |
| 216 |
"""Block contain multiple lines""" |
| 217 |
content = [ |
| 218 |
'#+begin_src python', |
| 219 |
'import hyperorg', |
| 220 |
'', |
| 221 |
'', |
| 222 |
'', |
| 223 |
'if __name__ == "__main__":', |
| 224 |
' print("Hello World!")', |
| 225 |
'', |
| 226 |
' sys.exit(1)', |
| 227 |
'#+end_src' |
| 228 |
] |
| 229 |
_, sut = parser.OrgContentParser(content).result() |
| 230 |
|
| 231 |
# expected |
| 232 |
codeblock = elements.Block( |
| 233 |
name='src', |
| 234 |
content_lines=[ |
| 235 |
'import hyperorg', |
| 236 |
'', |
| 237 |
'', |
| 238 |
'', |
| 239 |
'if __name__ == "__main__":', |
| 240 |
' print("Hello World!")', |
| 241 |
'', |
| 242 |
' sys.exit(1)'], |
| 243 |
language='python') |
| 244 |
|
| 245 |
self.assertEqual(len(sut), 1) |
| 246 |
self.assertEqual(sut[0], codeblock) |
| 247 |
|
| 248 |
def test_two_blocks(self): |
| 249 |
"""One codeblock follows the other""" |
| 250 |
content = [ |
| 251 |
'#+begin_src python', |
| 252 |
'import eins', |
| 253 |
'print("eins")', |
| 254 |
'#+end_src', |
| 255 |
'#+begin_src Cpp', |
| 256 |
'import zwei', |
| 257 |
'print("zwei")', |
| 258 |
'foo bar', |
| 259 |
'#+end_src', |
| 260 |
] |
| 261 |
|
| 262 |
pars = parser.OrgContentParser(content) |
| 263 |
_, sut = pars.result() |
| 264 |
|
| 265 |
self.assertEqual(len(sut), 2) |
| 266 |
|
| 267 |
python = sut[0] |
| 268 |
cpp = sut[1] |
| 269 |
|
| 270 |
# test block content |
| 271 |
self.assertEqual(python.language, 'python') |
| 272 |
self.assertEqual(len(python), 2) |
| 273 |
|
| 274 |
self.assertEqual(cpp.language, 'Cpp') |
| 275 |
self.assertEqual(len(cpp), 3) |
| 276 |
|
| 277 |
def test_case_insensitive(self): |
| 278 |
"""The property code is case insensitive""" |
| 279 |
content = [ |
| 280 |
'#+bEgIn_Src python', |
| 281 |
'import hyperorg', |
| 282 |
'#+enD_sRc' |
| 283 |
] |
| 284 |
_, sut = parser.OrgContentParser(content).result() |
| 285 |
|
| 286 |
codeblock = elements.Block( |
| 287 |
name='Src', |
| 288 |
content_lines=['import hyperorg'], |
| 289 |
language='python') |
| 290 |
|
| 291 |
self.assertEqual(len(sut), 1) |
| 292 |
self.assertEqual(sut[0], codeblock) |
| 293 |
|
| 294 |
def test_no_language(self): |
| 295 |
"""Missing language specifier""" |
| 296 |
content = [ |
| 297 |
'#+begin_foobar', |
| 298 |
'import hyperorg', |
| 299 |
'#+end_foobar' |
| 300 |
] |
| 301 |
_, sut = parser.OrgContentParser(content).result() |
| 302 |
|
| 303 |
self.assertEqual(sut[0].language, '') |
| 304 |
|
| 305 |
|
| 306 |
class TableBlocks(unittest.TestCase): |
| 307 |
"""Test cases for quick n dirty solution to detect tables but render them |
| 308 |
as code blocks instead of HTML |
| 309 |
|
| 310 |
Syntax reference: https://orgmode.org/worg/org-syntax.html#Tables |
| 311 |
|
| 312 |
The pygments "text" lexer is used instead of "org" because the org lexer |
| 313 |
is known for not supporting tables. |
| 314 |
See: https://github.com/pygments/pygments/discussions/2688 |
| 315 |
""" |
| 316 |
|
| 317 |
def test_org_table(self): |
| 318 |
"""Org table""" |
| 319 |
content = [ |
| 320 |
'| Name | Phone | Age |', |
| 321 |
'|-------+-------+-----|', |
| 322 |
'| Peter | 1234 | 24 |', |
| 323 |
'| Anna | 4321 | 25 |' |
| 324 |
] |
| 325 |
|
| 326 |
_, sut = parser.OrgContentParser(content).result() |
| 327 |
|
| 328 |
self.assertEqual(len(sut), 1) |
| 329 |
|
| 330 |
block = sut[0] |
| 331 |
self.assertIsInstance(block, elements.TableBlock) |
| 332 |
|
| 333 |
# test block content |
| 334 |
self.assertEqual(block.name, 'src') |
| 335 |
self.assertEqual(block.language, 'text') |
| 336 |
for idx, content_line in enumerate(content): |
| 337 |
self.assertEqual(block[idx], content_line) |
| 338 |
|
| 339 |
def test_etable_table(self): |
| 340 |
"""Org table""" |
| 341 |
content = [ |
| 342 |
'+-------+-------+-----+', |
| 343 |
'| Name | Phone | Age |', |
| 344 |
'+-------+-------+-----+', |
| 345 |
'| Peter | 1234 | 24 |', |
| 346 |
'+-------+-------+-----+', |
| 347 |
'| Anna | 4321 | 25 |', |
| 348 |
'+-------+-------+-----+' |
| 349 |
] |
| 350 |
|
| 351 |
_, sut = parser.OrgContentParser(content).result() |
| 352 |
|
| 353 |
self.assertEqual(len(sut), 1) |
| 354 |
|
| 355 |
block = sut[0] |
| 356 |
self.assertIsInstance(block, elements.TableBlock) |
| 357 |
|
| 358 |
# test block content |
| 359 |
self.assertEqual(block.name, 'src') |
| 360 |
self.assertEqual(block.language, 'text') |
| 361 |
for idx, content_line in enumerate(content): |
| 362 |
self.assertEqual(block[idx], content_line) |
| 363 |
|
| 364 |
|
| 365 |
class Links(unittest.TestCase): |
| 366 |
"""Parse links""" |
| 367 |
|
| 368 |
def test_paragraph(self): |
| 369 |
"""Link contained in a paragraph""" |
| 370 |
content = ['ada [[https://foo.bar][mylink]] lovelance'] |
| 371 |
_, sut = parser.OrgContentParser(content).result() |
| 372 |
|
| 373 |
# one paragraph |
| 374 |
self.assertEqual(len(sut), 1) |
| 375 |
# with 3 runs |
| 376 |
self.assertEqual(len(sut[0]), 3) |
| 377 |
|
| 378 |
self.assertEqual(sut[0][0], 'ada ') |
| 379 |
self.assertEqual(sut[0][1].runs, ['mylink']) |
| 380 |
self.assertEqual(sut[0][1].target, 'foo.bar') |
| 381 |
self.assertEqual(sut[0][2], ' lovelance') |
| 382 |
|
| 383 |
def test_label_with_linebreak(self): |
| 384 |
"""Link contained in a paragraph""" |
| 385 |
content = [ |
| 386 |
'ada [[https://foo.bar][my', |
| 387 |
'link]] lovelance' |
| 388 |
] |
| 389 |
_, sut = parser.OrgContentParser(content).result() |
| 390 |
|
| 391 |
# one paragraph |
| 392 |
self.assertEqual(len(sut), 1) |
| 393 |
# with 3 runs |
| 394 |
self.assertEqual(len(sut[0]), 3) |
| 395 |
|
| 396 |
self.assertEqual(sut[0][0], 'ada ') |
| 397 |
self.assertEqual(sut[0][1].runs, ['my link']) |
| 398 |
self.assertEqual(sut[0][1].target, 'foo.bar') |
| 399 |
self.assertEqual(sut[0][2], ' lovelance') |
| 400 |
|
| 401 |
|
| 402 |
class Figures(unittest.TestCase): |
| 403 |
"""Parse figures""" |
| 404 |
|
| 405 |
def test_simple(self): |
| 406 |
"""Simple figure.""" |
| 407 |
content = [ |
| 408 |
'', |
| 409 |
'[[figure.bmp]]', |
| 410 |
] |
| 411 |
_, sut = parser.OrgContentParser(content).result() |
| 412 |
|
| 413 |
self.assertEqual(len(sut), 1) |
| 414 |
|
| 415 |
figure = sut[0] |
| 416 |
self.assertEqual(figure.attributes, {}) |
| 417 |
self.assertEqual(figure.caption, None) |
| 418 |
self.assertEqual(figure.image.target, 'figure.bmp') |
| 419 |
self.assertEqual(figure.link, None) |
| 420 |
|
| 421 |
def test_linked_to_locale_file(self): |
| 422 |
"""Figure with locale file linked image.""" |
| 423 |
content = ['[[link.bmp][file:figure.png]]'] |
| 424 |
_, sut = parser.OrgContentParser(content).result() |
| 425 |
|
| 426 |
self.assertEqual(len(sut), 1) |
| 427 |
figure = sut[0] |
| 428 |
self.assertEqual(figure.attributes, {}) |
| 429 |
self.assertEqual(figure.caption, None) |
| 430 |
|
| 431 |
self.assertEqual(figure.link.target, 'link.bmp') |
| 432 |
self.assertEqual(figure.image.target, 'figure.png') |
| 433 |
|
| 434 |
def test_figure_linked_to_https_file(self): |
| 435 |
"""Figure with https file linked image.""" |
| 436 |
content = ['[[link.bmp][https://world.out/figure.png]]'] |
| 437 |
_, sut = parser.OrgContentParser(content).result() |
| 438 |
|
| 439 |
self.assertEqual(len(sut), 1) |
| 440 |
figure = sut[0] |
| 441 |
self.assertEqual(figure.attributes, {}) |
| 442 |
self.assertEqual(figure.caption, None) |
| 443 |
self.assertEqual(figure.link.target, 'link.bmp') |
| 444 |
self.assertEqual(figure.image.target, 'https://world.out/figure.png') |
| 445 |
|
| 446 |
def test_not_a_figure(self): |
| 447 |
"""Missing "file:" prefix result in a paragraph with link""" |
| 448 |
content = ['[[link.bmp][figure.png]]'] |
| 449 |
_, sut = parser.OrgContentParser(content).result() |
| 450 |
|
| 451 |
self.assertEqual(len(sut), 1) |
| 452 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 453 |
self.assertEqual(sut[0][0].target, 'link.bmp') |
| 454 |
self.assertEqual(sut[0][0][0], 'figure.png') |
| 455 |
|
| 456 |
def test_two_figures(self): |
| 457 |
"""Two figures""" |
| 458 |
content = [ |
| 459 |
'foobar', |
| 460 |
'', |
| 461 |
'[[figure_one.png]]', |
| 462 |
'', |
| 463 |
'[[figure_two.png]]', |
| 464 |
] |
| 465 |
_, sut = parser.OrgContentParser(content).result() |
| 466 |
|
| 467 |
self.assertEqual(len(sut), 3) |
| 468 |
self.assertEqual(sut[1].image.target, 'figure_one.png') |
| 469 |
self.assertEqual(sut[2].image.target, 'figure_two.png') |
| 470 |
|
| 471 |
def test_attr_width(self): |
| 472 |
"""ATTR_HTML :width""" |
| 473 |
content = [ |
| 474 |
'foobar', |
| 475 |
'#+ATTR_HTML: :width 123', |
| 476 |
'[[figure_one.png]]', |
| 477 |
] |
| 478 |
_, sut = parser.OrgContentParser(content).result() |
| 479 |
|
| 480 |
self.assertEqual(len(sut), 2) |
| 481 |
sut = sut[1] |
| 482 |
self.assertEqual(sut.image.target, 'figure_one.png') |
| 483 |
self.assertEqual(sut.image.width, '123') |
| 484 |
|
| 485 |
def test_attr_alt(self): |
| 486 |
"""ATTR_HTML :alt""" |
| 487 |
content = [ |
| 488 |
'foobar', |
| 489 |
'#+ATTR_HTML: :alt foo bar', |
| 490 |
'[[figure_one.png]]', |
| 491 |
] |
| 492 |
_, sut = parser.OrgContentParser(content).result() |
| 493 |
|
| 494 |
self.assertEqual(len(sut), 2) |
| 495 |
sut = sut[1] |
| 496 |
self.assertEqual(sut.attributes, {}) |
| 497 |
self.assertEqual(sut.image.target, 'figure_one.png') |
| 498 |
self.assertEqual(sut.image.alternative, 'foo bar') |
| 499 |
|
| 500 |
def test_caption(self): |
| 501 |
"""CAPTION""" |
| 502 |
content = [ |
| 503 |
'foobar', |
| 504 |
'#+CAPTION: ätsch', |
| 505 |
'[[figure.png]]', |
| 506 |
] |
| 507 |
_, sut = parser.OrgContentParser(content).result() |
| 508 |
|
| 509 |
self.assertEqual(len(sut), 2) |
| 510 |
sut = sut[1] |
| 511 |
self.assertEqual(sut.attributes, {}) |
| 512 |
self.assertEqual(sut.caption, ['ätsch']) |
| 513 |
self.assertEqual(sut.image.target, 'figure.png') |
| 514 |
|
| 515 |
def test_attr_title(self): |
| 516 |
"""ATTR_HTML :title""" |
| 517 |
content = [ |
| 518 |
'foobar', |
| 519 |
'#+ATTR_HTML: :title This is mars', |
| 520 |
'[[figure_one.png]]' |
| 521 |
] |
| 522 |
|
| 523 |
_, sut = parser.OrgContentParser(content).result() |
| 524 |
|
| 525 |
self.assertEqual(len(sut), 2) |
| 526 |
sut = sut[1] |
| 527 |
self.assertEqual(sut.attributes, {}) |
| 528 |
self.assertEqual(sut.image.title, 'This is mars') |
| 529 |
|
| 530 |
def test_unknown_property(self): |
| 531 |
"""Unknown property""" |
| 532 |
content = [ |
| 533 |
'foobar', |
| 534 |
'#+fooBar: pink elephant', |
| 535 |
'[[figure_one.png]]', |
| 536 |
] |
| 537 |
_, sut = parser.OrgContentParser(content).result() |
| 538 |
|
| 539 |
self.assertEqual(len(sut), 2) |
| 540 |
sut = sut[1] |
| 541 |
self.assertEqual(sut.unknown, ['#+FOOBAR: pink elephant']) |
| 542 |
|
| 543 |
def test_multiple_properties(self): |
| 544 |
"""Figure with multiple propierties""" |
| 545 |
content = [ |
| 546 |
'foobar', |
| 547 |
'#+ATTR_HTML: :width 15%', |
| 548 |
'#+DOWNLOADED: https://foobar.world', |
| 549 |
'[[foobarfig.png]]', |
| 550 |
] |
| 551 |
_, sut = parser.OrgContentParser(content).result() |
| 552 |
|
| 553 |
self.assertEqual(len(sut), 2) |
| 554 |
sut = sut[1] |
| 555 |
self.assertEqual(sut.image.target, 'foobarfig.png') |
| 556 |
self.assertEqual(sut.image.width, '15%') |
| 557 |
self.assertEqual(sut.unknown, |
| 558 |
['#+DOWNLOADED: https://foobar.world']) |
| 559 |
|
| 560 |
def test_multiple_attr_html(self): |
| 561 |
"""Figure with multiple ATTR_HTML lines""" |
| 562 |
content = [ |
| 563 |
'foobar', |
| 564 |
'#+ATTR_HTML: :alt foo bar', |
| 565 |
'#+ATTR_HTML: :width 15%', |
| 566 |
'#+ATTR_HTML: :height 72%', |
| 567 |
'#+ATTR_HTML: :möp X', |
| 568 |
'#+ATTR_HTML: :title FOOBAR CITY', |
| 569 |
'[[foobarfig.png]]', |
| 570 |
] |
| 571 |
_, sut = parser.OrgContentParser(content).result() |
| 572 |
|
| 573 |
# Figures Image |
| 574 |
sut = sut[1] |
| 575 |
self.assertEqual(sut.image.target, 'foobarfig.png') |
| 576 |
self.assertEqual(sut.image.alternative, 'foo bar') |
| 577 |
self.assertEqual(sut.image.width, '15%') |
| 578 |
self.assertEqual(sut.image.height, '72%') |
| 579 |
self.assertEqual(sut.image.title, 'FOOBAR CITY') |
| 580 |
# Figure |
| 581 |
self.assertEqual(sut.attributes, {'möp': 'X'}) |
| 582 |
|
| 583 |
def test_duplicate_attr_html(self): |
| 584 |
"""Width overwrites the other.""" |
| 585 |
content = [ |
| 586 |
'foobar', |
| 587 |
'#+ATTR_HTML: :width 15%', |
| 588 |
'#+ATTR_HTML: :width 39%', |
| 589 |
'[[foobarfig.png]]', |
| 590 |
] |
| 591 |
_, sut = parser.OrgContentParser(content).result() |
| 592 |
|
| 593 |
self.assertEqual(len(sut), 2) |
| 594 |
sut = sut[1] |
| 595 |
self.assertEqual(sut.image.target, 'foobarfig.png') |
| 596 |
self.assertEqual(sut.image.width, '39%') |
| 597 |
|
| 598 |
|
| 599 |
class Math(unittest.TestCase): |
| 600 |
"""Parse inline and display/block (latex) math""" |
| 601 |
|
| 602 |
def test_display(self): |
| 603 |
"""Display (block) math marked by dollar sign or square brackets""" |
| 604 |
|
| 605 |
test_content = [ |
| 606 |
'$$e = mc^2$$', |
| 607 |
r'\[e = mc^2\]', |
| 608 |
] |
| 609 |
|
| 610 |
for content in test_content: |
| 611 |
_, sut = parser.OrgContentParser([content]).result() |
| 612 |
|
| 613 |
self.assertEqual(len(sut), 1) |
| 614 |
self.assertIsInstance(sut[0], elements.DisplayMath) |
| 615 |
self.assertEqual(sut[0][0], 'e = mc^2') |
| 616 |
|
| 617 |
def test_display_text_surrounded(self): |
| 618 |
"""Display (block) math with text as pre- and suffix""" |
| 619 |
|
| 620 |
test_content = [ |
| 621 |
'first paragraph $$e = mc^2$$ second one', |
| 622 |
r'first paragraph \[e = mc^2\] second one', |
| 623 |
] |
| 624 |
|
| 625 |
for content in test_content: |
| 626 |
_, sut = parser.OrgContentParser([content, '']).result() |
| 627 |
self.assertEqual(len(sut), 3) |
| 628 |
self.assertIsInstance(sut[1], elements.DisplayMath) |
| 629 |
self.assertEqual(sut[1][0], 'e = mc^2') |
| 630 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 631 |
self.assertIsInstance(sut[2], elements.Paragraph) |
| 632 |
|
| 633 |
def test_inline(self): |
| 634 |
"""Inline math marked marked by single dollar sign or round brackets |
| 635 |
""" |
| 636 |
|
| 637 |
test_content = [ |
| 638 |
'$e = mc^2$', |
| 639 |
r'\(e = mc^2\)', |
| 640 |
] |
| 641 |
|
| 642 |
for content in test_content: |
| 643 |
_, sut = parser.OrgContentParser([content]).result() |
| 644 |
|
| 645 |
# one element only |
| 646 |
self.assertEqual(len(sut), 1) |
| 647 |
# a paragraph |
| 648 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 649 |
# contain one run only |
| 650 |
self.assertEqual(len(sut[0]), 1) |
| 651 |
# a inlinemath |
| 652 |
self.assertIsInstance(sut[0][0], textruns.InlineMath) |
| 653 |
self.assertEqual(sut[0][0][0], 'e = mc^2') |
| 654 |
|
| 655 |
def test_inline_text_surrounded(self): |
| 656 |
"""Inline math marked with text as pre- and suffix""" |
| 657 |
|
| 658 |
test_content = [ |
| 659 |
'one paragraph $e = mc^2$ with formula.', |
| 660 |
r'one paragraph \(e = mc^2\) with formula.', |
| 661 |
] |
| 662 |
|
| 663 |
for content in test_content: |
| 664 |
_, sut = parser.OrgContentParser([content, '']).result() |
| 665 |
|
| 666 |
# one paragraph |
| 667 |
self.assertEqual(len(sut), 1) |
| 668 |
# with 3 runs |
| 669 |
self.assertEqual(len(sut[0]), 3) |
| 670 |
# second is inline math |
| 671 |
self.assertIsInstance(sut[0][1], textruns.InlineMath) |
| 672 |
self.assertEqual(sut[0][1][0], 'e = mc^2') |
| 673 |
# first and third are just strings |
| 674 |
self.assertEqual(sut[0][0], 'one paragraph ') |
| 675 |
self.assertEqual(sut[0][2], ' with formula.') |
| 676 |
|
| 677 |
def test_full(self): |
| 678 |
"""All math variants as one""" |
| 679 |
|
| 680 |
content = [ |
| 681 |
'first paragraph $$e = mc^2$$ second one', |
| 682 |
'', |
| 683 |
r'first paragraph \[e = mc^2\] second one', |
| 684 |
'', |
| 685 |
'$$e = mc^2$$', |
| 686 |
'', |
| 687 |
r'\[e = mc^2\]', |
| 688 |
'', |
| 689 |
'one paragraph $e = mc^2$ with formula.', |
| 690 |
'', |
| 691 |
r'one paragraph \(e = mc^2\) with formula.', |
| 692 |
'', |
| 693 |
'$e = mc^2$', |
| 694 |
'', |
| 695 |
r'\(e = mc^2\)', |
| 696 |
] |
| 697 |
|
| 698 |
_, sut = parser.OrgContentParser(content).result() |
| 699 |
|
| 700 |
# 12 elements |
| 701 |
self.assertEqual(len(sut), 12) |
| 702 |
# some Paragraphs |
| 703 |
for idx in [0, 2, 3, 5, 8, 9, 10, 11]: |
| 704 |
self.assertIsInstance(sut[idx], elements.Paragraph, f'{idx=}') |
| 705 |
# some DisplayMath |
| 706 |
for idx in [1, 4, 6, 7]: |
| 707 |
self.assertIsInstance(sut[idx], elements.DisplayMath, f'{idx=}') |
| 708 |
# 9th has inline math |
| 709 |
self.assertEqual(len(sut[8]), 3) |
| 710 |
self.assertIsInstance(sut[8][1], textruns.InlineMath) |
| 711 |
# 10th has inline math |
| 712 |
self.assertEqual(len(sut[9]), 3) |
| 713 |
self.assertIsInstance(sut[9][1], textruns.InlineMath) |
| 714 |
# 11th has inline math |
| 715 |
self.assertEqual(len(sut[10]), 1) |
| 716 |
self.assertIsInstance(sut[10][0], textruns.InlineMath) |
| 717 |
# 12th (last) has inline math |
| 718 |
self.assertEqual(len(sut[11]), 1) |
| 719 |
self.assertIsInstance(sut[11][0], textruns.InlineMath) |
| 720 |
|
| 721 |
def test_multiple_display_in_one_line(self): |
| 722 |
"""Special case of multiple display math in one line""" |
| 723 |
|
| 724 |
# One line with 5 display math of dollar and bracket kind |
| 725 |
content = [ |
| 726 |
r'first para $$A = mc^2$$ second pAra \[B = mc^2\] third Para ' |
| 727 |
r'$$c = mc^2$$ forth \[D = mc^2\] five \[e = mc^2\]', |
| 728 |
] |
| 729 |
|
| 730 |
_, sut = parser.OrgContentParser(content).result() |
| 731 |
|
| 732 |
self.assertEqual(len(sut), 10) |
| 733 |
# some Paragraphs |
| 734 |
for idx in [0, 2, 4, 6, 8]: |
| 735 |
self.assertIsInstance(sut[idx], elements.Paragraph, f'{idx=}') |
| 736 |
# some DisplayMath |
| 737 |
for idx in [1, 3, 5, 7, 9]: |
| 738 |
self.assertIsInstance(sut[idx], elements.DisplayMath, f'{idx=}') |
| 739 |
|
| 740 |
def test_display_with_newline(self): |
| 741 |
"""Display math with newline in it""" |
| 742 |
content = ['$$foo=\nbar$$'] |
| 743 |
|
| 744 |
_, sut = parser.OrgContentParser(content).result() |
| 745 |
self.assertEqual(len(sut), 1) |
| 746 |
self.assertIsInstance(sut[0], elements.DisplayMath) |
| 747 |
self.assertEqual(sut[0][0], 'foo=\nbar') |