1 from collections import defaultdict
6 from catalogue.utils import get_ilustr_sizes
9 def ilustr_sizes_getter(e, S):
10 sizes = get_ilustr_sizes(e.attrib['src'], S['slug'], S['ilustr'])
16 'utwor': ('_pass', False, None, None, None),
17 '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF': ('_ignore', False, None, None, None),
18 'abstrakt': ('_ignore', False, None, None, None),
19 'uwaga': ('_ignore', False, None, None, None),
20 'extra': ('_ignore', False, None, None, None),
21 'nota_red': ('_ignore', False, None, None, None),
22 'numeracja': ('_ignore', False, None, None, None),
24 'powiesc': ('master', False, None, None, None),
25 'opowiadanie': ('master', False, None, None, None),
26 'liryka_lp': ('master', False, None, None, None),
27 'liryka_l': ('master', False, None, None, None),
28 'dramat_wspolczesny': ('master', False, None, None, None),
29 'dramat_wierszowany_lp': ('master', False, None, None, None),
30 'dramat_wierszowany_l': ('master', False, None, None, None),
32 'dlugi_cytat': ('blockquote', False, None, None, None),
33 'poezja_cyt': ('blockquote', False, None, None, None),
34 'dlugi_cyt': ('blockquote', False, None, None, None),
35 'ramka': ('blockquote', False, {'class': 'ramka'}, None, None),
37 'blok': ('div', False, None, None, None),
39 'strofa': ('div', True, {'class': 'stanza'}, None, None),
40 'wers': ('div', True, {'class': 'verse'}, None, None),
41 'wers_wciety': ('div', True, {'class': 'wers_wciety'}, None, None),
42 'wers_cd': ('div', True, {'class': 'wers_cd'}, None, None),
43 'wers_akap': ('div', True, {'class': 'wers_akap'}, None, None),
44 'zastepnik_wersu': ('div', True, {'class': 'zastepnik_wersu'}, None, None),
45 'wers_do_prawej': ('div', True, {'class': 'wers_do_prawej'}, None, None),
46 'wers_srodek': ('div', True, {'class': 'wers_srodek'}, None, None),
48 'autor_utworu': ('div', True, {'class': 'author'}, None, None),
49 'dzielo_nadrzedne': ('div', True, {'class': 'dzielo_nadrzedne'}, None, None),
50 'nazwa_utworu': ('div', True, {'class': 'title'}, None, None),
51 'podtytul': ('div', True, {'class': 'podtytul'}, None, None),
53 'motto': ('div', False, {'class': 'motto'}, None, None),
54 'motto_podpis': ('div', True, {'class': 'motto_podpis'}, None, None),
55 'dedykacja': ('div', True, {'class': 'dedykacja'}, None, None),
56 'miejsce_czas': ('div', True, {'class': 'miejsce_czas'}, None, None),
58 'lista_osob': ('div', False, {'class': 'lista_osob'}, None, None),
59 'naglowek_listy': ('div', True, {'class': 'naglowek_listy'}, None, None),
60 'lista_osoba': ('div', True, {'class': 'lista_osoba'}, None, None),
61 'naglowek_osoba': ('div', True, {'class': 'naglowek_osoba'}, None, None),
62 'osoba': ('em', True, {'class': 'osoba'}, None, None),
63 'didaskalia': ('div', True, {'class': 'didaskalia'}, None, None),
64 'kwestia': ('div', False, {'class': 'kwestia'}, None, None),
65 'didask_tekst': ('em', True, {'class': 'didask_tekst'}, None, None),
67 'naglowek_czesc': ('h2', True, None, None, None),
68 'naglowek_akt': ('h2', True, None, None, None),
69 'naglowek_scena': ('h3', True, None, None, None),
70 'naglowek_rozdzial': ('h3', True, None, None, None),
71 'naglowek_podrozdzial': ('h4', True, None, None, None),
72 'srodtytul': ('h5', True, None, None, None),
74 'nota': ('div', True, {'class': 'note'}, None, False),
76 'akap': ('p', True, {'class': 'paragraph'}, None, True),
77 'akap_dialog': ('p', True, {'class': 'paragraph'}, None, True),
78 'akap_cd': ('p', True, {'class': 'paragraph'}, None, True),
80 'sekcja_asterysk': ('p', True, {'class': 'spacer-asterisk'}, None, True),
81 'sekcja_swiatlo': ('p', True, {'class': 'sekcja_swiatlo'}, None, True),
82 'separator_linia': ('p', True, {'class': 'separator_linia'}, None, True),
84 'tytul_dziela': ('em', True, {'class': 'book-title'}, None, False),
85 'slowo_obce': ('em', True, {'class': 'foreign-word'}, None, False),
86 'wyroznienie': ('em', True, {'class': 'author-emphasis'}, None, False),
87 'wieksze_odstepy': ('em', True, {'class': 'wieksze_odstepy'}, None, False),
89 'ref': ('a', True, {'class': 'reference'}, {'data-uri': 'href'}, False),
91 'begin': ('_ignore', True, {'class': 'reference'}, {'data-uri': 'href'}, False),
92 'end': ('_ignore', True, {'class': 'reference'}, {'data-uri': 'href'}, False),
93 'motyw': ('_ignore', True, {'class': 'theme'}, None, False),
95 'pa': ('a', True, {'class': 'footnote footnote-pa'}, None, False),
96 'pe': ('a', True, {'class': 'footnote footnote-pe'}, None, False),
97 'pr': ('a', True, {'class': 'footnote footnote-pr'}, None, False),
98 'pt': ('a', True, {'class': 'footnote footnote-pt'}, None, False),
99 'ptrad': ('a', True, {'class': 'footnote footnote-ptrad'}, None, False),
101 'werset': ('p', True, {'class': 'werset'}, None, True),
102 'br': ('br', False, None, None, None),
103 'indeks_dolny': ('em', True, {'class': 'indeks_dolny'}, None, False),
104 'mat': ('span', True, {'class': 'mat'}, None, False),
106 'mfenced': ('math_mfenced', True, None, None, False),
107 'mfrac': ('math_mfrac', True, None, None, False),
108 'mrow': ('math_mrow', True, None, None, False),
109 'mi': ('math_mi', True, None, None, False),
110 'mn': ('math_mn', True, None, None, False),
111 'mo': ('math_mo', True, None, None, False),
112 'msup': ('math_msup', True, None, None, False),
114 'list': ('blockquote', False, {'class': 'list'}, None, None),
115 'wywiad_pyt': ('blockquote', False, {'class': 'wywiad_pyt'}, None, None),
116 'wywiad_odp': ('blockquote', False, {'class': 'wywiad_odp'}, None, None),
117 'rownolegle': ('blockquote', False, {'class': 'rownolegle'}, None, None),
118 'animacja': ('div', False, {'class': 'animacja'}, None, None),
119 'data': ('div', True, {'class': 'data'}, None, None),
120 'podpis': ('div', True, {'class': 'podpis'}, None, None),
121 'naglowek_listu': ('div', True, {'class': 'naglowek_listu'}, None, None),
122 'pozdrowienie': ('div', True, {'class': 'pozdrowienie'}, None, None),
123 'adresat': ('div', True, {'class': 'adresat'}, None, None),
124 'tytul_oryg': ('div', True, {'class': 'tytul_oryg'}, None, None),
125 'miejsce_data': ('div', True, {'class': 'miejsce_data'}, None, None),
126 'audio': ('_ignore', False, None, None, None),
127 'www': ('a', True, {'class': 'www'}, {'href': '.text'}, False),
129 'tabela': ('table', False, None, None, None),
130 'tabelka': ('table', False, None, None, None),
131 'wiersz': ('tr', False, None, None, None),
132 'kol': ('td', True, None, None, None),
134 'ilustr': ('img', False, None, {
135 'src': lambda e, S: e.attrib['src'],
136 'sizes': ilustr_sizes_getter,
139 'tab': ('span', False, {'class': 'tab'}, {'szer': 'szer'}, False),
153 'zastepnik_wersu': 'f',
154 'wers_do_prawej': 'f',
162 #tree = etree.parse(argv[1])
169 front2 = set(['autor_utworu'])
173 text = text.replace('---', '—').replace('--', '–').replace('...', '…').replace(',,', '„').replace('"', '”').replace('\n', ' ')
178 if elem.tag is etree.Comment: return []
179 tag, hastext, attrs, attr_map, num = tags[elem.tag]
183 elif tag == '_ignore':
191 output['paragraphIndex'] = S['index']
192 if 'dlugi_cytat' not in S['stack'] and 'poezja_cyt' not in S['stack']:
194 output['visibleNumber'] = S['vindex']
195 id_prefix = id_prefixes.get(elem.tag, 'i')
196 S['id'][id_prefix] += 1
197 output['id'] = id_prefix + str(S['id'][id_prefix])
198 if elem.attrib.get('id'):
199 output['id'] = 'wl-' + elem.attrib.get('id')
201 output['attr'] = attrs.copy()
203 output.setdefault('attr', {})
204 for k, v in attr_map.items():
211 output['attr'][k] = val
212 output['contents'] = contents
214 if elem.tag == 'strofa':
215 verses = [etree.Element('wers')]
217 vparts = re.split(r'/\s+', elem.text)
218 for i, v in enumerate(vparts):
220 verses.append(etree.Element('wers'))
221 verses[-1].text = (verses[-1].text or '') + v
223 vparts = re.split(r'/\s+', child.tail or '')
224 child.tail = vparts[0]
225 verses[-1].append(child)
227 verses.append(etree.Element('wers'))
230 if not(len(verses[-1]) or (verses[-1].text or '').strip()):
233 elem.clear(keep_tail=True)
235 if len(verse) == 1 and (verse[0].tag.startswith('wers') or verse[0].tag == 'zastepnik_wersu') and not (verse[0].tail or '').strip():
236 elem.append(verse[0])
241 # for v in re.split(r'/\s+', elem.text):
242 # etree.SubElement(elem, 'wers').text = v
245 if hastext and elem.text:
246 contents.append(norm(elem.text))
248 S['stack'].append(elem.tag)
249 contents += toj(c, S)
250 if hastext and c.tail:
251 contents.append(norm(c.tail))
254 if elem.tag in front1:
255 S['front1'] += output
257 if elem.tag in front2:
258 S['front2'] += output
262 def conv(tree, slug):
267 'id': defaultdict(lambda: 0),
273 output = toj(tree.getroot(), S)
274 if not len(output): return {}
276 jt['front1'] = S['front1']
277 jt['front2'] = S['front2']
280 #print(json.dumps(jt, indent=2, ensure_ascii=False))