From: Radek Czajka Date: Wed, 27 Jan 2021 12:25:06 +0000 (+0100) Subject: Response images. X-Git-Tag: 1.13 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/73858688886ec363048373f7e8253a33497fe3cd?hp=6641219fd962fc1791fa8d94dec26db4c3546931 Response images. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 98b62e4..6f17bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ This document records all notable changes to Librarian. +## 1.13 (2021-01-27) + +### Changed +- Responsive images in HTML. This changes the html.transform API. + + ## 1.12 (2021-01-27) ### Added diff --git a/setup.py b/setup.py index 055ec93..c1af4e3 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def whole_tree(prefix, path): setup( name='librarian', - version='1.12', + version='1.13', description='Converter from WolneLektury.pl XML-based language to XHTML, TXT and other formats', author="Marek Stępniowski", author_email='marek@stepniowski.com', diff --git a/src/librarian/html.py b/src/librarian/html.py index 9ec6583..d262198 100644 --- a/src/librarian/html.py +++ b/src/librarian/html.py @@ -12,6 +12,7 @@ import copy from lxml import etree from librarian import XHTMLNS, ParseError, OutputFile from librarian import functions +from PIL import Image from lxml.etree import XMLSyntaxError, XSLTApplyError import six @@ -50,7 +51,32 @@ def transform_abstrakt(abstrakt_element): return re.sub(']*>', '', html) -def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None): +def add_image_sizes(tree, gallery_path, gallery_url): + widths = [360, 600, 1200, 1800] + for ilustr in tree.findall('//ilustr'): + rel_path = ilustr.attrib['src'] + img = Image.open(gallery_path + rel_path) + srcset = [] + for w in widths: + if w < img.size[0]: + height = round(img.size[1] * w / img.size[0]) + th = img.resize((w, height)) + + fname = ('.W%d.' % w).join(rel_path.rsplit('.', 1)) + th.save(gallery_path + fname) + srcset.append(" ".join(( + gallery_url + fname, + '%dw' % w + ))) + srcset.append(" ".join(( + gallery_url + rel_path, + '%dw' % img.size[0] + ))) + ilustr.attrib['srcset'] = ", ".join(srcset) + ilustr.attrib['src'] = gallery_url + rel_path + + +def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None, gallery_path='img/', gallery_url='img/'): """Transforms the WL document to XHTML. If output_filename is None, returns an XML, @@ -75,7 +101,8 @@ def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None): if not options: options = {} - options.setdefault('gallery', "''") + + add_image_sizes(document.edoc, gallery_path, gallery_url) css = ( css diff --git a/src/librarian/xslt/book2html.xslt b/src/librarian/xslt/book2html.xslt index 7576413..04e2658 100644 --- a/src/librarian/xslt/book2html.xslt +++ b/src/librarian/xslt/book2html.xslt @@ -135,7 +135,17 @@ - + + + + + + + (min-width: 718px) 600px, + (min-width: 600px) calc(100vw - 118px), + (min-width: 320px) calc(100vw - 75px), + (min-width: 15em) calc(100wv - 60px), + calc(100wv - 40px)