aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/compressor/compressor.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/compressor/compressor.py')
-rw-r--r--plugins/compressor/compressor.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/plugins/compressor/compressor.py b/plugins/compressor/compressor.py
new file mode 100644
index 0000000..bd219d2
--- /dev/null
+++ b/plugins/compressor/compressor.py
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+from pelican import signals
+from subprocess import call
+import logging
+import os
+
+logger = logging.getLogger(__name__)
+
+# Display command output on DEBUG and TRACE
+SHOW_OUTPUT = logger.getEffectiveLevel() <= logging.DEBUG
+
+"""
+Minify CSS and JS files in output path
+with uglifycss and uglifyjs.
+"""
+def minify(pelican):
+ """
+ Minify CSS and JavaScript
+ :param pelican: The Pelican instance
+ """
+ for dirpath, _, filenames in os.walk(pelican.settings['OUTPUT_PATH']):
+ for name in filenames:
+ if os.path.splitext(name)[1] in ('.css'):
+ filepath = os.path.join(dirpath, name)
+ logger.info('minifiy %s with uglifycss', filepath)
+ debug = '--debug' if SHOW_OUTPUT else ''
+ call('uglifycss {} --output {} {}'.format(debug, filepath, filepath),
+ shell=True)
+ elif os.path.splitext(name)[1] in ('.js'):
+ filepath = os.path.join(dirpath, name)
+ logger.info('minifiy %s with uglifyjs', filepath)
+ verbose = '-v' if SHOW_OUTPUT else ''
+ min_filepath = filepath[:-3] + '.min' + filepath[-3:]
+ call("uglifyjs {} {} -o {} --mangle".format(filepath, verbose, min_filepath),
+ shell=True)
+
+def register():
+ signals.finalized.connect(minify)