From 0b5eef9eadced5428d072dd8efc1df9d71c52227 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 23 Mar 2012 19:11:42 -0400 Subject: [PATCH] Move JPEGOptim and OptiPNG plugins into the images module. --- hyde/ext/plugins/images.py | 119 ++++++++++++++++++++++++++++++++- hyde/tests/ext/test_optipng.py | 2 +- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/hyde/ext/plugins/images.py b/hyde/ext/plugins/images.py index 85b8b6c..4d7505b 100644 --- a/hyde/ext/plugins/images.py +++ b/hyde/ext/plugins/images.py @@ -5,12 +5,15 @@ Contains classes to handle images related things # Requires PIL or pillow """ -from hyde.plugin import Plugin +from hyde.plugin import CLTransformer, Plugin + import re import glob import os +from fswrap import File + try: from PIL import Image except ImportError: @@ -333,3 +336,117 @@ class ImageThumbnailsPlugin(Plugin): for resource in node.resources: if match_includes(resource.path): self.thumb(resource, dim1, dim2, prefix, crop_type, preserve_orientation) + +class JPEGOptimPlugin(CLTransformer): + """ + The plugin class for JPEGOptim + """ + + def __init__(self, site): + super(JPEGOptimPlugin, self).__init__(site) + + @property + def plugin_name(self): + """ + The name of the plugin. + """ + return "jpegoptim" + + def binary_resource_complete(self, resource): + """ + If the site is in development mode, just return. + Otherwise, run jpegoptim to compress the jpg file. + """ + + try: + mode = self.site.config.mode + except AttributeError: + mode = "production" + + if not resource.source_file.kind == 'jpg': + return + + if mode.startswith('dev'): + self.logger.debug("Skipping jpegoptim in development mode.") + return + + supported = [ + "force", + "max=", + "strip-all", + "strip-com", + "strip-exif", + "strip-iptc", + "strip-icc", + ] + target = File(self.site.config.deploy_root_path.child( + resource.relative_deploy_path)) + jpegoptim = self.app + args = [unicode(jpegoptim)] + args.extend(self.process_args(supported)) + args.extend(["-q", unicode(target)]) + self.call_app(args) + + +class OptiPNGPlugin(CLTransformer): + """ + The plugin class for OptiPNG + """ + + def __init__(self, site): + super(OptiPNGPlugin, self).__init__(site) + + @property + def plugin_name(self): + """ + The name of the plugin. + """ + return "optipng" + + def option_prefix(self, option): + return "-" + + def binary_resource_complete(self, resource): + """ + If the site is in development mode, just return. + Otherwise, run optipng to compress the png file. + """ + + try: + mode = self.site.config.mode + except AttributeError: + mode = "production" + + if not resource.source_file.kind == 'png': + return + + if mode.startswith('dev'): + self.logger.debug("Skipping optipng in development mode.") + return + + supported = [ + "o", + "fix", + "force", + "preserve", + "quiet", + "log", + "f", + "i", + "zc", + "zm", + "zs", + "zw", + "full", + "nb", + "nc", + "np", + "nz" + ] + target = File(self.site.config.deploy_root_path.child( + resource.relative_deploy_path)) + optipng = self.app + args = [unicode(optipng)] + args.extend(self.process_args(supported)) + args.extend([unicode(target)]) + self.call_app(args) diff --git a/hyde/tests/ext/test_optipng.py b/hyde/tests/ext/test_optipng.py index dea9d73..d778e43 100644 --- a/hyde/tests/ext/test_optipng.py +++ b/hyde/tests/ext/test_optipng.py @@ -31,7 +31,7 @@ class TestOptipng(object): def test_can_execute_optipng(self): s = Site(TEST_SITE) s.config.mode = "production" - s.config.plugins = ['hyde.ext.plugins.optipng.OptiPNGPlugin'] + s.config.plugins = ['hyde.ext.plugins.images.OptiPNGPlugin'] s.config.optipng = Expando(dict(args=dict(quiet=""))) source =File(TEST_SITE.child('content/media/images/hyde-lt-b.png')) target = File(Folder(s.config.deploy_root_path).child('media/images/hyde-lt-b.png'))