Fixes
[librarian.git] / src / librarian / html.py
index 363286c..fddeb2f 100644 (file)
@@ -58,9 +58,8 @@ def add_image_sizes(tree, gallery_path, gallery_url, base_url):
         rel_path = ilustr.attrib['src']
         img_url = six.moves.urllib.parse.urljoin(base_url, rel_path)
 
         rel_path = ilustr.attrib['src']
         img_url = six.moves.urllib.parse.urljoin(base_url, rel_path)
 
-        with six.moves.urllib.request.urlopen(img_url) as f:
-            img = Image.open(f)
-
+        f = six.moves.urllib.request.urlopen(img_url)
+        img = Image.open(f)
         ext = {'GIF': 'gif', 'PNG': 'png'}.get(img.format, 'jpg')
 
         srcset = []
         ext = {'GIF': 'gif', 'PNG': 'png'}.get(img.format, 'jpg')
 
         srcset = []
@@ -75,10 +74,12 @@ def add_image_sizes(tree, gallery_path, gallery_url, base_url):
         ]
         largest = None
         for w in widths:
         ]
         largest = None
         for w in widths:
-            height = round(img.size[1] * w / img.size[0])
-            th = img.resize((w, height))
             fname = '%d.W%d.%s' % (i, w, ext)
             fname = '%d.W%d.%s' % (i, w, ext)
-            th.save(gallery_path + fname)
+            fpath = gallery_path + fname
+            if not os.path.exists(fpath):
+                height = round(img.size[1] * w / img.size[0])
+                th = img.resize((w, height))
+                th.save(fpath)
             th_url = gallery_url + fname
             srcset.append(" ".join((
                 th_url,
             th_url = gallery_url + fname
             srcset.append(" ".join((
                 th_url,
@@ -88,6 +89,8 @@ def add_image_sizes(tree, gallery_path, gallery_url, base_url):
         ilustr.attrib['srcset'] = ", ".join(srcset)
         ilustr.attrib['src'] = largest_url
 
         ilustr.attrib['srcset'] = ", ".join(srcset)
         ilustr.attrib['src'] = largest_url
 
+        f.close()
+
 
 def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None, gallery_path='img/', gallery_url='img/', base_url='file://./'):
     """Transforms the WL document to XHTML.
 
 def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None, gallery_path='img/', gallery_url='img/', base_url='file://./'):
     """Transforms the WL document to XHTML.
@@ -111,7 +114,8 @@ def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None, ga
 
         document.clean_ed_note()
         document.clean_ed_note('abstrakt')
 
         document.clean_ed_note()
         document.clean_ed_note('abstrakt')
-
+        document.fix_pa_akap()
+        
         if not options:
             options = {}
 
         if not options:
             options = {}
 
@@ -308,6 +312,7 @@ def any_ancestor(element, test):
 
 def add_anchors(root):
     counter = 1
 
 def add_anchors(root):
     counter = 1
+    visible_counter = 1
     for element in root.iterdescendants():
         def f(e):
             return (
     for element in root.iterdescendants():
         def f(e):
             return (
@@ -316,17 +321,27 @@ def add_anchors(root):
                 )
                 or e.get('id') == 'nota_red'
                 or e.tag == 'blockquote'
                 )
                 or e.get('id') == 'nota_red'
                 or e.tag == 'blockquote'
+                or e.get('id') == 'footnotes'
             )
             )
+
+        if element.get('class') == 'numeracja':
+            try:
+                visible_counter = int(element.get('data-start'))
+            except ValueError:
+                visible_counter = 1
+
         if any_ancestor(element, f):
             continue
 
         if element.tag == 'div' and 'verse' in element.get('class', ''):
         if any_ancestor(element, f):
             continue
 
         if element.tag == 'div' and 'verse' in element.get('class', ''):
-            if counter == 1 or counter % 5 == 0:
-                add_anchor(element, "f%d" % counter, link_text=counter)
+            if visible_counter == 1 or visible_counter % 5 == 0:
+                add_anchor(element, "f%d" % counter, link_text=visible_counter)
             counter += 1
             counter += 1
+            visible_counter += 1
         elif 'paragraph' in element.get('class', ''):
         elif 'paragraph' in element.get('class', ''):
-            add_anchor(element, "f%d" % counter, link_text=counter)
+            add_anchor(element, "f%d" % counter, link_text=visible_counter)
             counter += 1
             counter += 1
+            visible_counter += 1
 
 
 def raw_printable_text(element):
 
 
 def raw_printable_text(element):