This commit is contained in:
Andrei Astafev 2019-04-20 18:33:28 +03:00
commit 515289eff2
4 changed files with 70 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

37
README.md Normal file
View File

@ -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

2
__init__.py Normal file
View File

@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from .yuicompressor_opt import *

30
yuicompressor_opt.py Normal file
View File

@ -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)