-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add blurry_image Jinja extension
Adds blurry_image extension to insert an <img> tag with width & height into a Jinja template. Optionally inserts an image of a specific size. Usage: ``` {% blurry_image page.image, width=250 %} ```
- Loading branch information
1 parent
73154f2
commit 5ebe4b6
Showing
7 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import mimetypes | ||
from pathlib import Path | ||
from urllib.parse import urlparse | ||
|
||
from jinja2_simple_tags import StandaloneTag | ||
from rich.console import Console | ||
from wand.exceptions import BlobError | ||
from wand.image import Image | ||
|
||
from blurry.images import add_image_width_to_path | ||
from blurry.settings import get_build_directory | ||
from blurry.utils import build_path_to_url | ||
|
||
warning_console = Console(stderr=True, style="bold yellow") | ||
|
||
|
||
class BlurryImage(StandaloneTag): | ||
safe_output = True | ||
tags = {"blurry_image"} | ||
|
||
def render(self, *args, **kwargs): | ||
(image_url,) = args | ||
width = kwargs.get("width") | ||
image_content_path: str = "." + urlparse(image_url).path | ||
image_path = get_build_directory() / image_content_path | ||
|
||
try: | ||
with Image(filename=str(image_path)) as image: | ||
image_width = image.width | ||
image_height = image.height | ||
image_mimetype = image.mimetype | ||
except BlobError: | ||
warning_console.print(f"Could not find image: {image_path}") | ||
return "" | ||
|
||
if width: | ||
image_path = add_image_width_to_path(image_path, width) | ||
|
||
if image_mimetype in [ | ||
mimetypes.types_map[".jpg"], | ||
mimetypes.types_map[".png"], | ||
]: | ||
image_path = Path(str(image_path).replace(image_path.suffix, ".avif")) | ||
|
||
if not image_path.exists(): | ||
warning_console.print( | ||
f"blurry_image: Could not find {image_path}. Skipping." | ||
) | ||
return "" | ||
|
||
src = build_path_to_url(image_path) | ||
|
||
return f'<img src="{src}" width="{image_width}" height="{image_height}" />' |
Oops, something went wrong.