commit 515289eff2c41b621aa709bd77137207f7c1d1e8 Author: Andrey Astafyev Date: Sat Apr 20 18:33:28 2019 +0300 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b7101f --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# YUI Compressor Plugin + +A pelican plugin that minifies CSS/JS files using YUI Compressor during the +building step. + +# Installation + +YUI Compressor needs to be present on your system. One way to obtain it is by +installing it using pip: + +Important: This method assumes that JRE is already installed. + +```bash +pip install yuicompressor +``` + +More information about YUI Compressor: https://github.com/yui/yuicompressor + +# Instructions + +Add `yuicompressor` to `pelicanconf.py` after installing YUI Compressor: + +```python +PLUGINS = ['yuicompressor'] +``` + +By default, this plugin expects the YUI Compressor executable to be named +`yuicompressor`. This can be changed by defining `YUICOMPRESSOR_EXECUTABLE` in +`pelicanconf.py`: + +```python +YUICOMPRESSOR_EXECUTABLE = 'yui-compressor' +``` + +# Licence + +GNU AFFERO GENERAL PUBLIC LICENSE Version 3 diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..27f35fd --- /dev/null +++ b/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from .yuicompressor_opt import * diff --git a/yuicompressor_opt.py b/yuicompressor_opt.py new file mode 100644 index 0000000..9dc9648 --- /dev/null +++ b/yuicompressor_opt.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +from pelican import signals +from subprocess import check_call +import logging +import os + +logger = logging.getLogger(__name__) + +""" +Minify CSS and JS files in output path with YUI Compressor from Yahoo. +Required: an existing installation of YUI Compressor. +""" + +def minify(pelican): + """ + Minify CSS and JS with YUI Compressor + :param pelican: The Pelican instance + """ + executable = pelican.settings.get('YUICOMPRESSOR_EXECUTABLE', 'yuicompressor') + extra_options = pelican.settings.get('YUICOMPRESSOR_EXTRA_OPTIONS', []) + for dirpath, _, filenames in os.walk(pelican.settings['OUTPUT_PATH']): + for name in filenames: + if os.path.splitext(name)[1] in ('.css','.js'): + filepath = os.path.join(dirpath, name) + logger.info('minify %s', filepath) + check_call([executable] + extra_options + ['--charset', 'utf-8', filepath, '-o', filepath]) + +def register(): + signals.finalized.connect(minify)