Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SVG->PNG conversion #4

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 46 additions & 2 deletions lektor_thumbnail_generator.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import shutil

from lektor.build_programs import AttachmentBuildProgram, buildprogram
Expand All @@ -8,9 +9,41 @@
get_image_info, get_quality)
from lektor.pluginsystem import Plugin
from lektor.reporter import reporter
from lektor.utils import portable_popen
from lektor.utils import portable_popen, locate_executable
from werkzeug.utils import cached_property

def process_svg_image(
ctx,
source_image,
dst_filename,
width=None,
height=None,
mode=None,
):
if width is None and height is None:
raise ValueError("Must specify at least one of width or height.")

# Because Lektor doesn't have a generic find program (only imagemagick),
# only *nix is supported for simplicity. Imagemagick will be used as the
# fallback on Windows, but it won't work well...
if os.name == 'nt':
return process_image(ctx, source_image, dst_filename, width, height,
mode, 85)

inkscape = locate_executable('inkscape')

cmdline = [inkscape, source_image]

if width is not None:
cmdline += ["-w", str(width)]
if height is not None:
cmdline += ["-h", str(height)]

# FIXME: This will only work with inkscape 1.0+
cmdline += ["--export-filename", dst_filename]

reporter.report_debug_info("inkscape cmd line", cmdline)
portable_popen(cmdline).wait()

# We override process_image here because Lektor does not support adding extra
# parameters yet, but it will soon, and this can be removed when it does.
Expand Down Expand Up @@ -82,7 +115,10 @@ def build_artifact(self, artifact):

df = artifact.source_obj.url_path
ext_pos = df.rfind(".")
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])
if df[ext_pos + 1 :] == 'svg':
dst_filename = "%s-%s.png" % (df[:ext_pos], item)
else:
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])

def closure(dst_filename, source_img, width, height, resize_image=True):
# We need this closure, otherwise variables get updated and this
Expand All @@ -92,6 +128,14 @@ def build_thumbnail_artifact(artifact):
artifact.ensure_dir()
if not resize_image:
shutil.copy2(source_img, artifact.dst_filename)
elif df[ext_pos + 1 :] == 'svg':
process_svg_image(
ctx,
source_img,
artifact.dst_filename,
width,
height,
)
else:
process_image(
ctx,
Expand Down