collective.exportimport icon indicating copy to clipboard operation
collective.exportimport copied to clipboard

fix_html_in_portlets should also support additional fixers

Open pbauer opened this issue 3 years ago • 0 comments

Same as fix_html_in_content_fields is should be possible to pass additional fixers.

An example I used recently is to scale all unscaled images:

def scale_unscaled_images(text, obj=None):
    """Scale unscaled image"""
    if not text:
        return text
    fallback_scale = "huge"

    soup = BeautifulSoup(text, "html.parser")
    for tag in soup.find_all("img"):
        if "data-val" not in tag.attrs:
            # maybe external image
            continue

        scale = tag["data-scale"]
        # Prevent unscaled images!
        if not scale:
            scale = fallback_scale
            tag["data-scale"] = fallback_scale
        if not tag["src"].endswith(scale):
            tag["src"] = tag["src"] + "/" + scale

    return soup.decode()

pbauer avatar Apr 13 '23 10:04 pbauer