diff options
author | Bryan Brattlof <bryanbrattlof@gmail.com> | 2018-10-30 20:49:47 -0500 |
---|---|---|
committer | Bryan Brattlof <bryanbrattlof@gmail.com> | 2018-10-30 20:49:47 -0500 |
commit | ad0ea5802b9f92da357cd5a71f031cab5f7917eb (patch) | |
tree | 343003d6da34d853485286ac24808c8af73f797b | |
parent | cc842441b462af680674cef7973da812ba43a278 (diff) | |
download | pelican-htmlmin-ad0ea5802b9f92da357cd5a71f031cab5f7917eb.tar.gz pelican-htmlmin-ad0ea5802b9f92da357cd5a71f031cab5f7917eb.tar.bz2 |
Add initial code
-rw-r--r-- | __init__.py | 48 | ||||
-rw-r--r-- | readme.md | 5 |
2 files changed, 52 insertions, 1 deletions
diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..0104d8f --- /dev/null +++ b/__init__.py @@ -0,0 +1,48 @@ + +from pelican import signals + +import multiprocessing +import htmlmin +import os +import re + +import logging +logger = logging.getLogger(__name__) + + +def run(pelican): + options = pelican.settings.get('MINIFY', {}) + htmlfile = re.compile(r'.html?$') + pool = multiprocessing.Pool() + + for base, dirs, files in os.walk(pelican.settings['OUTPUT_PATH']): + for f in files: + # filter only html files + if not htmlfile.search(f): + continue + # add full html path to wooker pool + filepath = os.path.join(base, f) + pool.apply_async(worker, (filepath, options)) + + pool.close() + pool.join() + + +def worker(filepath, options): + rawhtml = open(filepath, encoding='utf-8').read() + with open(filepath, 'w', encoding='utf-8') as f: + try: + logger.debug('Minifying: %s' % filepath) + compressed = htmlmin.minify(rawhtml, **options) + f.write(compressed) + except Exception as e: + logger.critical('Minification failed: {}'.format(e)) + finally: + f.close() + + +def register(): + """ + Minify HTML at the end of the processing loop + """ + signals.finalized.connect(run) @@ -1,2 +1,5 @@ # pelican-minify -An Pelican plugin to use htmlmin to remove extra whitespace. +An [Pelican] plugin to use htmlmin to remove extra whitespace from webpages. + +[Pelican]: "http://pelican.readthedocs.org/en/latest/" + |