Add source set handling.

This commit is contained in:
Sergio Martínez Portela 2021-12-17 09:54:09 +01:00
parent 1c609454ed
commit 99274cf6d7

View File

@ -25,6 +25,13 @@ OBJECT_TYPE_DESCRIPTORS = (
'styles',
'href',
),
(
{
'name': 'source'
},
'sets',
'srcset',
),
(
{
'name': 'img'
@ -94,8 +101,21 @@ def archive(content, base_url, selector, directory, attribute, progbar):
if part[attribute].startswith('data:'):
continue
href = urllib.parse.urljoin(base_url, part[attribute],
allow_fragments=False)
selectors = []
if attribute == "srcset":
# Dirty hack to support srcset syntax
hrefs = []
for option in part[attribute].split(','):
val, selector = option.split()
hrefs.append(urllib.parse.urljoin(base_url, val,
allow_fragments=False))
selectors.append(selector)
else:
hrefs = [urllib.parse.urljoin(base_url, part[attribute],
allow_fragments=False)]
resolveds = []
for i, href in enumerate(hrefs):
progbar.next_iter(href)
name = (hashlib.sha1(href.encode()).hexdigest()
@ -114,7 +134,11 @@ def archive(content, base_url, selector, directory, attribute, progbar):
with open(path, 'wb') as f:
f.write(content)
part[attribute] = path
resolveds.append(path)
if i < len(selectors):
resolveds[-1] += ' ' + selectors[i]
part[attribute] = ', '.join(resolveds)
def relink_links(content, base_url):