def make_nsfw_safe(text):
"""Make NSFW safer by adding click-to-show class to images."""
soup = BeautifulSoup(text, "lxml")
images = soup.find_all("img")
for image in images:
if image.get("class"):
image["class"] = "%s nsfw" % " ".join(image.get("class"))
else:
image["class"] = "nsfw"
image.replace_with(image)
result = str(soup)
# We don't want html/body, which BeautifulSoup kindly wraps our new HTML in
if result.startswith("<html><body>") and result.endswith("</body></html>"):
result = result[len("<html><body>"):-len("</body></html>")]
return result
评论列表
文章目录