Add SVG compression via npx svgo
Add vector extension support for .svg and route SVG files through npx svgo before raster compression. Keep behavior fail-safe: missing npx/svgo or non-zero svgo exit returns None and preserves existing flow. Extend tests for SVG discovery, SVG routing priority, and missing npx handling. Co-Authored-By: Abacus.AI CLI <agent@abacus.ai>
This commit is contained in:
+45
-1
@@ -44,7 +44,9 @@ from typing import Callable, List, Optional
|
||||
|
||||
__version__ = "1.1.7"
|
||||
|
||||
ALLOWED_EXT = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
|
||||
RASTER_EXT = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
|
||||
VECTOR_EXT = {".svg"}
|
||||
ALLOWED_EXT = RASTER_EXT | VECTOR_EXT
|
||||
PROGRESS_BAR_LEN = 40
|
||||
TEMP_PREFIX = "pptx_compress_"
|
||||
DEFAULT_MIN_SAVINGS = "2%"
|
||||
@@ -236,6 +238,45 @@ def compress_raster_image(
|
||||
)
|
||||
|
||||
|
||||
def compress_svg_with_svgo(
|
||||
original: Path,
|
||||
out_dir: Path,
|
||||
) -> Path | None:
|
||||
if original.suffix.lower() not in VECTOR_EXT:
|
||||
return None
|
||||
npx_exe = which("npx")
|
||||
if not npx_exe:
|
||||
return None
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
out_file = out_dir / original.name
|
||||
cmd = [
|
||||
npx_exe,
|
||||
"--yes",
|
||||
"svgo",
|
||||
str(original),
|
||||
"-o",
|
||||
str(out_file),
|
||||
]
|
||||
try:
|
||||
r = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if r.returncode != 0:
|
||||
sys.stderr.write(f"[svgo] Fehler bei {original.name}:{r.stderr}")
|
||||
return None
|
||||
return out_file if out_file.exists() else None
|
||||
except Exception as ex:
|
||||
sys.stderr.write(f"[svgo] Ausnahme bei {original.name}: {ex}")
|
||||
return None
|
||||
|
||||
|
||||
def compress_vector_image(
|
||||
original: Path,
|
||||
out_dir: Path,
|
||||
) -> Path | None:
|
||||
if original.suffix.lower() == ".svg":
|
||||
return compress_svg_with_svgo(original=original, out_dir=out_dir)
|
||||
return None
|
||||
|
||||
|
||||
def compress_image_with_routing(
|
||||
compressor: Callable[..., Path | None],
|
||||
original: Path,
|
||||
@@ -244,6 +285,9 @@ def compress_image_with_routing(
|
||||
quality: int,
|
||||
min_savings: str,
|
||||
) -> Path | None:
|
||||
vector_out = compress_vector_image(original=original, out_dir=out_dir)
|
||||
if vector_out is not None:
|
||||
return vector_out
|
||||
return compress_raster_image(
|
||||
compressor=compressor,
|
||||
original=original,
|
||||
|
||||
Reference in New Issue
Block a user