- style_filename = get_stylesheet(stylesheet)
- style = etree.parse(style_filename)
-
- document = copy.deepcopy(wldoc)
- del wldoc
- document.swap_endlines()
-
- if flags:
- for flag in flags:
- document.edoc.getroot().set(flag, 'yes')
-
- document.clean_ed_note()
- document.clean_ed_note('abstrakt')
-
- if not options:
- options = {}
- options.setdefault('gallery', "''")
-
- css = (
- css
- or 'https://static.wolnelektury.pl/css/compressed/book_text.css'
- )
- css = "'%s'" % css
- result = document.transform(style, css=css, **options)
- del document # no longer needed large object :)
-
- if html_has_content(result):
- add_anchors(result.getroot())
- add_table_of_themes(result.getroot())
- add_table_of_contents(result.getroot())
-
- return OutputFile.from_bytes(etree.tostring(
- result, method='html', xml_declaration=False,
- pretty_print=True, encoding='utf-8'
- ))
- else:
- return None
- except KeyError:
- raise ValueError("'%s' is not a valid stylesheet.")
- except (XMLSyntaxError, XSLTApplyError) as e:
- raise ParseError(e)
-
-
-@six.python_2_unicode_compatible
-class Fragment(object):
+ os.makedirs(gallery_path)
+ except:
+ pass
+
+ for i, ilustr in enumerate(tree.findall('//ilustr')):
+ rel_path = ilustr.attrib['src']
+ img_url = urllib.parse.urljoin(base_url, rel_path)
+
+ f = urllib.request.urlopen(img_url)
+ img = Image.open(f)
+ ext = {'GIF': 'gif', 'PNG': 'png'}.get(img.format, 'jpg')
+
+ srcset = []
+ # Needed widths: predefined and original, limited by
+ # whichever is smaller.
+ img_widths = [
+ w for w in
+ sorted(
+ set(widths + [img.size[0]])
+ )
+ if w <= min(widths[-1], img.size[0])
+ ]
+ largest = None
+ for w in widths:
+ fname = '%d.W%d.%s' % (i, w, ext)
+ 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,
+ '%dw' % w
+ )))
+ largest_url = th_url
+ ilustr.attrib['srcset'] = ", ".join(srcset)
+ ilustr.attrib['src'] = largest_url
+
+ f.close()
+
+
+class Fragment: