2020-01-21 06:35:58 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from django.templatetags.static import static
|
|
|
|
from lxml import html
|
|
|
|
|
|
|
|
|
|
|
|
def lazy_load(tree):
|
2022-05-14 17:57:27 +00:00
|
|
|
blank = static("blank.gif")
|
|
|
|
for img in tree.xpath(".//img"):
|
|
|
|
src = img.get("src", "")
|
|
|
|
if src.startswith("data") or "-math" in img.get("class", ""):
|
2020-01-21 06:35:58 +00:00
|
|
|
continue
|
2022-05-14 17:57:27 +00:00
|
|
|
noscript = html.Element("noscript")
|
2020-01-21 06:35:58 +00:00
|
|
|
copy = deepcopy(img)
|
2022-05-14 17:57:27 +00:00
|
|
|
copy.tail = ""
|
2020-01-21 06:35:58 +00:00
|
|
|
noscript.append(copy)
|
|
|
|
img.addprevious(noscript)
|
2022-05-14 17:57:27 +00:00
|
|
|
img.set("data-src", src)
|
|
|
|
img.set("src", blank)
|
|
|
|
img.set("class", img.get("class") + " unveil" if img.get("class") else "unveil")
|