Ilustr sizes in json for app
[wolnelektury.git] / src / catalogue / api / tojson.py
index b803e73..691ee3d 100644 (file)
@@ -3,6 +3,14 @@ import json
 import re
 from sys import argv
 from lxml import etree
+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),
@@ -123,7 +131,11 @@ tags = {
     'wiersz': ('tr', False, None, None, None),
     'kol': ('td', True, None, None, None),
 
-    'ilustr': ('img', False, None, {'src': 'src'}, False),
+    'ilustr': ('img', False, None, {
+        'src': lambda e, S: e.attrib['src'],
+        'sizes': ilustr_sizes_getter,
+        'alt': 'alt',
+    }, False),
     'tab': ('span', False, {'class': 'tab'}, {'szer': 'szer'}, False),
     
 }
@@ -192,6 +204,8 @@ def toj(elem, S):
             for k, v in attr_map.items():
                 if v == '.text':
                     val = elem.text
+                elif callable(v):
+                    val = v(elem, S)
                 else:
                     val = elem.attrib[v]
                 output['attr'][k] = val
@@ -245,14 +259,16 @@ def toj(elem, S):
         return []
     return output
 
-def conv(tree):
+def conv(tree, slug):
     S = {
         'index': 0,
         'vindex': 0,
+        'ilustr': 0,
         'id': defaultdict(lambda: 0),
         'stack': [],
         'front1': [],
         'front2': [],
+        'slug': slug,
     }
     output = toj(tree.getroot(), S)
     if not len(output): return {}