Ilustr sizes in json for app
authorRadek Czajka <radekczajka@wolnelektury.pl>
Tue, 1 Dec 2026 13:44:01 +0000 (14:44 +0100)
committerRadek Czajka <radekczajka@wolnelektury.pl>
Tue, 1 Dec 2026 13:44:01 +0000 (14:44 +0100)
src/catalogue/api/tojson.py
src/catalogue/utils.py

index d3b7ab5..691ee3d 100644 (file)
@@ -3,7 +3,14 @@ import json
 import re
 from sys import argv
 from lxml import etree
 import re
 from sys import argv
 from lxml import etree
-from catalogue.utils import absolute_url, gallery_url
+from catalogue.utils import get_ilustr_sizes
+
+
+def ilustr_sizes_getter(e, S):
+    sizes = get_ilustr_sizes(e.attrib['src'], S['slug'], S['ilustr'])
+    S['ilustr'] += 1
+    return sizes
+
 
 tags = {
     'utwor': ('_pass', False, None, None, None),
 
 tags = {
     'utwor': ('_pass', False, None, None, None),
@@ -125,7 +132,9 @@ tags = {
     'kol': ('td', True, None, None, None),
 
     'ilustr': ('img', False, None, {
     'kol': ('td', True, None, None, None),
 
     'ilustr': ('img', False, None, {
-        'src': lambda e, S: absolute_url(gallery_url(S['slug'])) + e.attrib['src'].rsplit('/', 1)[-1],
+        'src': lambda e, S: e.attrib['src'],
+        'sizes': ilustr_sizes_getter,
+        'alt': 'alt',
     }, False),
     'tab': ('span', False, {'class': 'tab'}, {'szer': 'szer'}, False),
     
     }, False),
     'tab': ('span', False, {'class': 'tab'}, {'szer': 'szer'}, False),
     
@@ -254,6 +263,7 @@ def conv(tree, slug):
     S = {
         'index': 0,
         'vindex': 0,
     S = {
         'index': 0,
         'vindex': 0,
+        'ilustr': 0,
         'id': defaultdict(lambda: 0),
         'stack': [],
         'front1': [],
         'id': defaultdict(lambda: 0),
         'stack': [],
         'front1': [],
index 2469d6c..60de555 100644 (file)
@@ -332,3 +332,14 @@ def iter_file(field_file, chunk_size=CHUNK_SIZE):
     else:
         with field_file.open("rb") as f:
             yield from f.chunks(chunk_size)
     else:
         with field_file.open("rb") as f:
             yield from f.chunks(chunk_size)
+
+
+# TODO: this should be recorded when generated.
+ILUSTR_SIZES = [360, 600, 1200, 1800, 2400]
+def get_ilustr_sizes(src, slug, index):
+    ext = src.rsplit('.', 1)[-1]
+    gallery = absolute_url(gallery_url(slug))
+    return {
+        str(size): f"{gallery}{index}.W{size}.{ext}"
+        for size in ILUSTR_SIZES
+    }