-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtei2html.py
More file actions
274 lines (247 loc) · 10.4 KB
/
tei2html.py
File metadata and controls
274 lines (247 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# !/usr/bin/env python3
# -*- coding: utf-8, vim: expandtab:ts=4 -*-
import glob
from bs4 import BeautifulSoup
from pathlib import Path
from tei_to_html_utils import file_gen_from_zip
HTML = '<html><head itemscope itemtype="http://schema.org/NewsArticle"><style></style></script></head></html> '
HI_DICT = {
'bold': 'b',
'italic': 'i',
'underline': 'u',
'strikeout': 'strike',
'superscript': 'sup',
'subscript': 'sub'
}
FREE = {
'cell': 'td',
'row': 'tr',
'ref': 'a',
'facs': 'a',
'list': 'ul',
'item': 'li',
'floatingText': 'div',
'div': 'div',
'table': 'table',
'figure': 'figure',
'p': 'p',
'quote': 'blockquote',
'body': 'to_unwrap'
}
TAG_TO_UNWRAP = {'body'}
FREE_ATTRS = {
'target': 'href',
'facs': 'src',
'type': 'type',
'rend': 'class'}
META_KEYS = {'sch:datePublished': 'Közzététel dátuma:', 'sch:dateModified': 'Módosítás dátuma:',
'sch:author': 'Szerző:', 'sch:source': 'Forrás:', 'sch:articleSection': 'Rovat:',
'sch:keywords': 'Kulcsszavak:', 'sch:url': 'URL:'}
def get_article_data(bsxml):
meta_d = {}
for m_key in META_KEYS.keys():
meta_val_list = [meta_val.text.strip() for meta_val in bsxml.find_all(m_key)]
if len(meta_val_list) > 0:
if m_key == 'sch:datePublished':
meta_d[META_KEYS[m_key]] = meta_val_list[0].replace('T', ' ')
else:
meta_d[META_KEYS[m_key]] = ', '.join(meta_val_list)
if bsxml.find('div', {'type': 'page'}) is not None:
urls = [u.attrs['source'] for u in bsxml.find_all('div', {'type': 'page'})]
meta_d['URL:'] = urls
return meta_d
def get_article_data_trafi(bsxml):
meta_d = {}
for m_key in META_KEYS.keys():
meta_val_list = [meta_val.text.strip() for meta_val in bsxml.find_all(m_key)]
if len(meta_val_list) > 0:
if m_key == 'sch:datePublished':
meta_d[META_KEYS[m_key]] = meta_val_list[0].replace('T', ' ')
else:
meta_d[META_KEYS[m_key]] = ', '.join(meta_val_list)
if bsxml.find('div', {'type': 'page'}) is not None:
urls = [u.attrs['source'] for u in bsxml.find_all('div', {'type': 'page'})]
meta_d['URL:'] = urls
sourcedesc = bsxml.find('sourceDesc')
authors = [au.text.strip() for au in sourcedesc.find_all('author')]
if len(authors) > 0:
meta_d['Szerző'] = ', '.join(authors)
date_ = sourcedesc.find('date')
if date_ is not None:
meta_d['Közzététel dátuma:'] = date_.text.strip()
url_ = sourcedesc.find('ptr', {'type': 'URL'})
if url_ is not None:
meta_d['URL:'] = url_.attrs['target']
# <ptr type="URL" target="https://www.mirror.co.uk/news/politics/keir-starmer-says-new-lockdown-22938036"/
keywords_cont = bsxml.find('keywords')
if keywords_cont is not None:
keywords = keywords_cont.find('term', {'type': 'tags'})
if keywords is not None:
meta_d['Kulcsszavak:'] = keywords.text.strip()
section = keywords_cont.find('term', {'type': 'categories'})
if section is not None:
meta_d['Rovat:'] = section.text.strip()
return meta_d
def fill_meta_block(input_bs, out_html, tei_type='eltedh'):
if tei_type == 'eltedh':
xenodata = input_bs.find('xenoData').find('rdf:Description')
meta_dict = get_article_data(input_bs)
else:
meta_dict = get_article_data_trafi(input_bs)
print(meta_dict)
head = out_html.head
meta_for_human = out_html.new_tag('div', attrs={'class': 'meta'})
# meta_dict = get_article_data(input_bs)
for mkey, mval in meta_dict.items():
a_meta = out_html.new_tag('p')
if mkey == 'URL:':
if not isinstance(mval, list):
mval = [mval]
a_meta.append('URL:')
for url in mval:
one_page_href = out_html.new_tag('a', attrs={'href': url})
one_page_href.string = url
a_meta.extend([out_html.new_tag('br'), one_page_href])
else:
a_meta.string = f'{mkey} {mval}'
meta_for_human.append(a_meta)
print(meta_for_human)
if tei_type == 'eltedh':
for x in xenodata.find_all():
schema_org_meta = out_html.new_tag('meta', attrs={'itemprop': x.name, 'content': x.text.strip()})
head.append(schema_org_meta)
return meta_for_human
def validate_html(html_obj):
for fig in html_obj.find_all('figure'):
if 'src' in fig.attrs.keys():
# embedded_content
button = html_obj.new_tag('button', {'style': 'font-size:40px'})
new_img = html_obj.new_tag('a', attrs={'href': fig['src']})
new_img.string = fig['src']
button.string = 'A linken található erőforrás nem része az archívumnak, ezért nem kerül megjelenítésre.'
button.append(new_img)
if fig.find() is not None:
fig.find().insert_before(button)
else:
fig.append(button)
del fig['src']
for page in html_obj.find_all('div', {'class': 'page'}):
page.attrs['data-href'] = page.attrs['source']
del page.attrs['source']
# <figure class="media_content" resp="script" type="corrected">
# <figure class="media_content" src="https://www.mosthallottam.hu/wp-content/uploads/2020/11/ev-rovara-2021.jpg">
def change_body_tags(bs_html):
html_body = bs_html.body
for tag in html_body.find_all():
if tag.name == 'hi':
if 'rend' in tag.attrs.keys():
tag.name = HI_DICT[tag.attrs['rend']]
else:
tag.name = 'em'
tag.attrs = {}
elif tag.name == 'p' and tag.attrs.get('rend', None) == 'head':
tag.name = 'h3'
tag.attrs = {}
elif tag.name == 'head' and tag.attrs.get('type', None) is not None:
# TODO: title_main = sourcedesc.find('title', {'type': 'main'})
rend = tag.attrs.get('type')
if rend == 'title':
tag.name = 'h1'
tag.attrs = {}
elif rend == 'subtitle':
tag.name = 'h2'
tag.attrs = {}
else:
tag_name = tag.name
tag_attrs = tag.attrs
isname = FREE.get(tag_name)
if isname is not None:
tag.name = isname
if tag_attrs != {}:
# <figure facs="https://ad.adverticum.net/t/?z=6653496&g=6653497&b=665349900&h
# =%5BLOCATION%5D&p=2" rend="embedded_content" resp="script" type="corrected">
new_attrs = {}
for k, v in tag_attrs.items():
if k in FREE_ATTRS:
new_attrs[FREE_ATTRS[k]] = v
else:
new_attrs[k] = v
tag.attrs = new_attrs
if tag.name == 'div':
if 'type' in tag.attrs.keys():
tag.attrs['class'] = tag.attrs['type']
del tag.attrs['type']
for tag in html_body.find_all('to_unwrap'):
tag.unwrap()
validate_html(bs_html)
def tei_to_html(xml_file, u):
bs_xml = BeautifulSoup(xml_file, features='xml')
bs_html = BeautifulSoup(HTML, features='html.parser')
css = open('tei2html.css', 'r')
bs_html.style.string = css.read()
if bs_xml.find('xenoData') is not None:
meta_for_human_block = fill_meta_block(bs_xml, bs_html, 'eltedh')
else:
meta_for_human_block = fill_meta_block(bs_xml, bs_html, 'trafi')
title_main = bs_xml.find('title', {'type': 'main'})
# TODO: title_sub = sourcedesc.find('title', {'type': '???'})
title_main.name = 'head'
title_main.attrs = {'type': 'title'}
print(title_main)
bs_xml.find('div', {'type': 'entry'}).insert_before(title_main)
print(bs_xml)
# <div type="entry"> trafilatura cikktörzs
bs_html.head.insert_after(bs_xml.body)
change_body_tags(bs_html)
bs_html.h1.insert_after(meta_for_human_block)
return bs_html
def process_portal_zip_to_htmls(archive_path_fold, out_folder, selected):
print(archive_path_fold)
out_folder_path = Path(out_folder)
for archive in glob.iglob(f'{archive_path_fold}/*.zip'):
zipname = archive[archive.rfind('/') + 1:]
if zipname in selected or selected == 'all':
portal_name = zipname[:zipname.find('.')]
for z_obj, filepath in file_gen_from_zip(archive):
with z_obj.open(filepath) as text_f:
uuid = filepath[filepath.find('/') + 1:filepath.find('.')]
filename_uuid = f'{uuid}.html'
article_as_html = tei_to_html(text_f, uuid)
html_filename = out_folder_path / portal_name / filename_uuid
html_filename.parent.mkdir(parents=True, exist_ok=True)
with open(html_filename, 'w', encoding='UTF-8') as output_html:
print(article_as_html.prettify(), file=output_html)
if __name__ == '__main__':
""" parser = argparse.ArgumentParser() # Add an argument
parser.add_argument('--inp_zip_dir', type=str, required=True)
parser.add_argument('--out_xml_dir', type=str, required=True)
parser.add_argument('--selected_zips', type=str, required=False, default='all')
args = parser.parse_args()
process_portal_zip_to_htmls(args.inp_zip_dir, args.out_xml_dir, args.selected_zips)"""
inp_zip_dir = '/home/dh/PycharmProjects/TEI2HTML' #
# inp_zip_dir = '/media/eltedh/6EAB565C0EA732DB/TEI_zips'
out_xml_dir = 'HTMLs'
selected_zips = ['mirror_politics.zip'] # 'p444_pagetest.zip'] # # # 'mosthallottam.zip'] #
#selected_zips = ['tei2html_test.zip'] # 'telex.zip']
process_portal_zip_to_htmls(inp_zip_dir, out_xml_dir, selected_zips)
# archive_path = '/media/eltedh/6EAB565C0EA732DB/TEI_zips'
# archive_path = '/home/sarkozizsofia/TEI_zips'
selected_archives = [
# 'magyaridok.zip',
# 'valasz.zip',
# 'vs.zip',
'vadhajtasok.zip'
# 'mosthallottam.zip',
# 'epiteszforum.zip',
# 'maszol.zip',
# 'utazomajom.zip',
# 'hataratkelo.zip',
# 'nnk.zip',
# 'p888.zip',
# 'telex.zip'
# 'szekelyhon.zip',
# 'transindex.zip'
# 'abcug.zip',
##'budapestbeacon.zip'
# 'termvil.zip'
]