aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Brattlof <bryanbrattlof@gmail.com>2018-10-30 20:49:47 -0500
committerBryan Brattlof <bryanbrattlof@gmail.com>2018-10-30 20:49:47 -0500
commitad0ea5802b9f92da357cd5a71f031cab5f7917eb (patch)
tree343003d6da34d853485286ac24808c8af73f797b
parentcc842441b462af680674cef7973da812ba43a278 (diff)
downloadpelican-htmlmin-ad0ea5802b9f92da357cd5a71f031cab5f7917eb.tar.gz
pelican-htmlmin-ad0ea5802b9f92da357cd5a71f031cab5f7917eb.tar.bz2
Add initial code
-rw-r--r--__init__.py48
-rw-r--r--readme.md5
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)
diff --git a/readme.md b/readme.md
index 04f95e6..898c80c 100644
--- a/readme.md
+++ b/readme.md
@@ -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/"
+