From 89fa5e34164bed75efb46804c15b96b9964fb684 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sun, 23 Jul 2023 19:27:07 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99id=C3=A1na=20podpora=20pro=20asymptote.?= =?UTF-8?q?=20Stejn=C4=9B=20jako=20PDF=20se=20z=20nich=20v=20HTML=20v?= =?UTF-8?q?=C3=BDstupu=20nyn=C3=AD=20generuj=C3=AD=20SVG.=20(Resolved=20#1?= =?UTF-8?q?2)=20#5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/formatitko/html_generator.py | 9 +++++---- src/formatitko/images.py | 33 ++++++++++++++++++++++++++++++-- test/test-files/circle.asy | 2 ++ test/test-files/test-partial.md | 1 + 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 test/test-files/circle.asy diff --git a/src/formatitko/html_generator.py b/src/formatitko/html_generator.py index 18f13e4..7380a1b 100644 --- a/src/formatitko/html_generator.py +++ b/src/formatitko/html_generator.py @@ -138,10 +138,11 @@ class HTMLGenerator(OutputGenerator): # processing contains finding and moving them to the output # directory. url = self.imageProcessor.process_image(url, ext, source_dir, **additional_args) - elif ext in ["pdf", "epdf"]: - if not "dpi" in additional_args: - additional_args["dpi"] = 300 - url = self.imageProcessor.process_image(url, "png", source_dir, **additional_args) + elif ext in ["pdf", "epdf","asy"]: + # Only relevant for when these were PNGs, leaving this here for future reference. + # if not "dpi" in additional_args: + # additional_args["dpi"] = 300 + url = self.imageProcessor.process_image(url, "svg", source_dir, **additional_args) elif ext in ["jpg"]: url = self.imageProcessor.process_image(url, "jpeg", source_dir, **additional_args) else: diff --git a/src/formatitko/images.py b/src/formatitko/images.py index 7623315..3f31b00 100644 --- a/src/formatitko/images.py +++ b/src/formatitko/images.py @@ -7,12 +7,20 @@ from PIL import Image class FileInWrongDirError(Exception): pass -class InkscapeError(Exception): +class ConversionProgramError(Exception): pass -class ImageMagickError(Exception): +class InkscapeError(ConversionProgramError): pass +class ImageMagickError(ConversionProgramError): + pass + +class AsyError(ConversionProgramError): + pass + + + class ImageProcessor: def __init__(self, public_dir: str, web_path: str, cache_dir: str, *lookup_dirs: list[str]): self.public_dir = public_dir @@ -32,6 +40,9 @@ class ImageProcessor: if full_path is None: raise FileNotFoundError(f'Image {input_filename} not found in {self.lookup_dirs} or {source_dir}.') + if format == "jpg": + format = "jpeg" + # Locate all dependencies deps_full = [full_path] for dep in deps: @@ -67,6 +78,21 @@ class ImageProcessor: elif self.find_image(target_name, [source_dir]) is not None and not self.is_outdated(self.find_image(target_name, [source_dir]), deps): shutil.copyfile(self.find_image(target_name, [source_dir]), target_path) + # Process asymptote + elif ext == "asy": + # Collect dependencies + deps_dir = self.cache_dir + "/" + name + "_deps" + if not os.path.isdir(deps_dir): + os.mkdir(deps_dir) + for dep_full in deps_full: + dep = os.path.basename(dep_full) + if not os.path.isfile(deps_dir + "/" + dep) or os.path.getmtime(deps_dir + "/" + dep) < os.path.getmtime(dep_full): + shutil.copyfile(dep_full, deps_dir + "/" + dep) + dpi_arg = ['-render', str(dpi/72)] if dpi is not None else [] + if subprocess.run(['asy', name, '-o', target_name, '-f', format, *dpi_arg], cwd=deps_dir).returncode != 0: + raise AsyError(f"Could not convert '{full_path}' to '{format}'") + shutil.move(deps_dir + "/" + target_name, self.cache_dir + "/" + target_name) + # Convert SVGs using inkscape elif ext == "svg": width_arg = ['--export-width', str(width)] if width is not None else [] @@ -125,3 +151,6 @@ class ImageProcessor: for dir in [*self.lookup_dirs, *additional_dirs]: if os.path.isfile(dir + "/" + input_filename): return dir + "/" + input_filename + + + diff --git a/test/test-files/circle.asy b/test/test-files/circle.asy new file mode 100644 index 0000000..fe099f8 --- /dev/null +++ b/test/test-files/circle.asy @@ -0,0 +1,2 @@ +import graph; +draw(Circle((0,0),20)); // graph - Circle diff --git a/test/test-files/test-partial.md b/test/test-files/test-partial.md index f50fa23..9e63376 100644 --- a/test/test-files/test-partial.md +++ b/test/test-files/test-partial.md @@ -48,6 +48,7 @@ I am a duck. This should be only shown to included cats. ::: +![A circle diagram](circle.asy){width=50px} $$ \def\eqalign#1{NO, just, nooooo}