-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermark_image_table.py
193 lines (170 loc) · 7.02 KB
/
watermark_image_table.py
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
#!/usr/bin/env python3
from collections import defaultdict
from pathlib import Path
from typing import Union
from importlib_metadata import metadata
from lxml import etree
import re
from sympy import pretty_print
from find_sigil_refs import encode_sigil
import sys
import os
import logging
logger = logging.getLogger(__name__)
this_script = Path(sys.argv[0])
project_root = this_script.parent.parent
NS={'f': 'http://www.faustedition.net/ns'}
# GSA_25-W_1362_wm_hand_drawn_150.jpg
IMG_FN_PATTERN = re.compile(r'GSA_25-W_(\d+)_wm_([a-z_]+)_(.*)')
CAT_LABELS = {'all': 'Blatt', 'detail': 'Detail', 'hand_drawn': 'Zeichnung'}
def etree_by_id(root: Path, idno_type: str):
result = {}
for mdfile in root.glob('**/*.xml'):
tree = etree.parse(str(mdfile))
id = tree.xpath(f'//f:idno[@type="{idno_type}"]/text()', namespaces=NS)
if id:
result[id[0]] = tree
return result
def collect_wm_imgs(imgfolder: Path):
by_sigpart = defaultdict(list)
for image in sorted(imgfolder.glob('*')):
match = IMG_FN_PATTERN.match(image.stem)
if match:
sigpart = match.group(1)
by_sigpart[sigpart].append(dict(
path = image,
category = CAT_LABELS.get(match.group(2), match.group(2)),
resolution = match.group(3)
))
else:
logger.warning('Ignoring unmatched file %s', image)
return by_sigpart
class WMLabels(dict):
def __init__(self, watermark_map:Path=None):
if watermark_map is None:
watermark_map = project_root / "src/main/xproc/xslt/watermark-labels.xml"
self.path = watermark_map
wm_tree = etree.parse(os.fspath(watermark_map))
self.tree = wm_tree
for el in wm_tree.xpath('//f:watermark', namespaces=NS):
id = el.get('id')
if id != "none":
self[id] = el.text
def register_image(self, wm_id, row_ref):
els = self.tree.xpath(f"//f:watermark[@id='{wm_id}']", namespaces=NS)
if els:
for el in els:
el.set('imgref', row_ref)
else:
logger.warning('WMLabels: id %s not found in the label table, cannot link to %s', wm_id, row_ref)
def save(self, path: Path = None):
if path is None:
path = self.path
self.tree.write(os.fspath(path), pretty_print=True, encoding='utf-8')
def image_cell(image: dict, signature, td=True):
url='/img/watermarks/' + image['path'].name
thumb='/img/watermarks/thumb/' + image['path'].name
caption=f"{signature} ({image['category']}, {image['resolution']} dpi)"
link = f"""<a href="{url}" class="chocolat-image" title="{caption}"><img src="{thumb}" /></a>"""
if td:
return '<td>' + link + '</td>'
else:
return link
def reorder_images(images: list[dict]) -> dict[str, Union[list, dict]]:
result = {'rest': []}
for image in images:
if image['category'] in result:
result['rest'].append(image)
else:
result[image['category']] = image
return result
def normalize_whitespace(text: str) -> str:
return re.sub(r'\s+', ' ', text).strip()
def generate_table(by_sigpart, idmap, wmmap: WMLabels):
rows = []
for sigpart, images in by_sigpart.items():
signature = f'GSA 25/W {sigpart}'
metadata = idmap.get(signature)
cols = []
# Image columns
img_by_cat = reorder_images(images)
for cat in ['Blatt', 'Detail', 'Zeichnung']:
img = img_by_cat.get(cat)
if img:
cols.append(image_cell(img, signature))
else:
cols.append('<td></td>')
cols.append('<td>' + ''.join(image_cell(img, signature, td=False) for img in img_by_cat['rest']) + '</td>')
if metadata:
sigil = metadata.xpath('//f:idno[@type="faustedition"]/text()', namespaces=NS)[0]
sigil_t = encode_sigil(sigil)
wmids = metadata.xpath('//f:watermarkID/text() | //f:countermarkID/text()', namespaces=NS)
if wmids:
wm_raw = normalize_whitespace(wmids[0])
wm_normalized = wmmap.get(wm_raw)
if wm_normalized:
wm_id = re.sub(r'\W', '_', wm_normalized)
wm_link = f'<a href="/watermark-table#{wm_id}">{wm_normalized}</a>'
wmmap.register_image(wm_raw, 'wm_' + sigil_t)
else:
wm_link = ''
else:
wm_link = ''
row = f"""<tr id="wm_{sigil_t}">
<td><a href="/document?sigil={sigil_t}&view=structure">{sigil}</a></td>
<td><a href="/document?sigil={sigil_t}&view=structure">{signature}</a></td>
{''.join(cols)}
<td>{wm_link}</td>
</tr>"""
else:
logger.warning('Signature %s not found', sigpart)
row = f"""<tr><td>{signature}</td>{''.join(cols)}</tr>"""
rows.append(row)
return ''.join(rows)
HEAD = """
<?php include "includes/header.php"?>
<!-- WARNING: This file can be re-generated using faust-gen/utils/watermark_image_table.py. Do not edit directly. -->
<section class="main"
data-breadcrumbs="Archiv@archive|Materialien@archive_materials"
data-title="Wasserzeichenaufnahmen">
<article class="pure-u-1">
<p>Auf dieser Seite werden die 2013 im Goethe- und Schiller-Archiv angefertigten
Wasserzeichenaufnahmen verfügbar gemacht.</p>
<p>Vgl. den <a href="watermarks">ausführlichen Bericht über die Anfertigung der
Aufnahmen</a> sowie die <a href="watermark-table">Liste der in der Faustedition
vorkommenden Wasserzeichen</a>.</p>
</article>
<table class="pure-table" data-sortable="true">
<thead>
<th data-sortable-type="sigil">Sigle</th>
<th data-sortable-type="sigil">Signatur</th>
<th>Blatt</th>
<th>Detail</th>
<th>Zeichnung</th>
<th>weitere</th>
<th data-sortable-type="alpha">Beschreibung (Link: Vorkommen)</th>
</thead>
<tbody>
"""
FOOT = """
</tbody>
</table>
</section>
<script type="text/javascript">
requirejs(['faust_common', 'svg-pan-zoom', 'sortable', 'jquery', 'jquery.table'],
function(Faust, svgPanZoom, Sortable, $, $table) {
// document.getElementById('breadcrumbs').appendChild(Faust.createBreadcrumbs([{"caption": "Über die Ausgabe", "link": "/intro"}, {"caption": "Wasserzeichen"}]));
Sortable.init();
$("table[data-sortable]").fixedtableheader();
});
</script>
<?php include "includes/footer.php"?>
"""
if __name__ == '__main__':
images = collect_wm_imgs(project_root / 'src/main/web/img/watermarks')
metadata = etree_by_id(project_root / 'data/xml/document', 'gsa_2')
wm_map = WMLabels()
content = generate_table(images, metadata, wm_map)
output_file = project_root / 'src/main/web/archive_watermarks.php'
output_file.write_text(HEAD + content + FOOT, encoding='utf-8')
wm_map.save()