2 from __future__ import unicode_literals
4 from StringIO import StringIO
6 from librarian.parser import WLDocument
7 from librarian.html import extract_annotations
9 from nose.tools import eq_
12 def _test_annotation(expected, got, name):
13 assert got[0].startswith('anchor-'), "%s: Unexpected anchor: '%s', should begin with 'anchor-'" % (name, got[0])
14 eq_(expected[0], got[1], "%s: Unexpected type, expected '%s', got '%s'" % (name, expected[0], got[1]))
15 eq_(expected[1], got[2], "%s: Unexpected qualifier, expected '%s', got '%s'" % (name, expected[1], got[2]))
16 eq_(expected[2], got[3], "%s: Unexpected text representation, expected '%s', got '%s'" % (name, expected[2], got[3]))
17 exp_html = '<div class="fn-%s">%s</div>' % (expected[0], expected[3])
18 eq_(exp_html, got[4], "%s: Unexpected html representation, expected '%s', got '%s'" % (name, exp_html, got[4]))
21 def test_annotations():
33 '<pr>Definiendum --- definiens.</pr>', (
36 'Definiendum \u2014 definiens.',
37 '<p>Definiendum \u2014 definiens.</p>'
41 ('<pt><slowo_obce>Definiendum</slowo_obce> --- definiens.</pt>', (
44 'Definiendum \u2014 definiens.',
45 '<p><em class="foreign-word">Definiendum</em> \u2014 definiens.</p>'
47 'Standard footnote.'),
49 ('<pr>Definiendum (łac.) --- definiens.</pr>', (
52 'Definiendum (łac.) \u2014 definiens.',
53 '<p>Definiendum (łac.) \u2014 definiens.</p>'
55 'Plain footnote with qualifier'),
57 ('<pe><slowo_obce>Definiendum</slowo_obce> (łac.) --- definiens.</pe>', (
60 'Definiendum (łac.) \u2014 definiens.',
61 '<p><em class="foreign-word">Definiendum</em> (łac.) \u2014 definiens.</p>'
63 'Standard footnote with qualifier.'),
65 ('<pt> <slowo_obce>Definiendum</slowo_obce> (daw.) --- definiens.</pt>', (
68 'Definiendum (daw.) \u2014 definiens.',
69 '<p> <em class="foreign-word">Definiendum</em> (daw.) \u2014 definiens.</p>'
71 'Standard footnote with leading whitespace and qualifier.'),
73 ('<pr>Definiendum (łac.) --- <slowo_obce>definiens</slowo_obce>.</pr>', (
76 'Definiendum (łac.) \u2014 definiens.',
77 '<p>Definiendum (łac.) \u2014 <em class="foreign-word">definiens</em>.</p>'
79 'Plain footnote with qualifier and some emphasis.'),
81 ('<pe><slowo_obce>Definiendum</slowo_obce> (łac.) --- <slowo_obce>definiens</slowo_obce>.</pe>', (
84 'Definiendum (łac.) \u2014 definiens.',
85 '<p><em class="foreign-word">Definiendum</em> (łac.) \u2014 <em class="foreign-word">definiens</em>.</p>'
87 'Standard footnote with qualifier and some emphasis.'),
89 ('<pe>Definiendum (łac.) --- definens (some) --- more text.</pe>', (
92 'Definiendum (łac.) \u2014 definiens (some) \u2014 more text.',
93 '<p>Definiendum (łac.) \u2014 definiens (some) \u2014 more text.</p>',
95 'Footnote with a second parentheses and mdash.'),
99 xml_src = '''<utwor><akap> %s </akap></utwor>''' % "".join(
100 t[0] for t in annotations)
101 html = WLDocument.from_string(xml_src, parse_dublincore=False).as_html().get_file()
102 res_annotations = list(extract_annotations(html))
104 for i, (src, expected, name) in enumerate(annotations):
105 yield _test_annotation, expected, res_annotations[i], name