From 86f97bc655c2a5dc6b249986c4fe9d60cecb257d Mon Sep 17 00:00:00 2001 From: Bryan Brattlof Date: Sun, 8 Sep 2019 12:35:38 -0500 Subject: move minify scripts into package folder --- __init__.py | 49 ----------------------------------------- pelican-htmlmin/__init__.py | 1 + pelican-htmlmin/minify.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 5 ----- 4 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 __init__.py create mode 100644 pelican-htmlmin/__init__.py create mode 100644 pelican-htmlmin/minify.py delete mode 100644 readme.md diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 906b5a3..0000000 --- a/__init__.py +++ /dev/null @@ -1,49 +0,0 @@ - -from pelican import signals - -import multiprocessing -import htmlmin -import os -import re - -import logging -logger = logging.getLogger(__name__) - - -def run(pelican): - - if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": - return # don't minify content if pelican is in debug mode - - options = pelican.settings.get('MINIFY', {}) - htmlfile = re.compile(r'.html?$') - pool = multiprocessing.Pool() - - # find all html & htm files and give them to the workers to minify - for base, dirs, files in os.walk(pelican.settings['OUTPUT_PATH']): - for f in filter(htmlfile.search, files): - 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/pelican-htmlmin/__init__.py b/pelican-htmlmin/__init__.py new file mode 100644 index 0000000..73ff73b --- /dev/null +++ b/pelican-htmlmin/__init__.py @@ -0,0 +1 @@ +from .minify import register diff --git a/pelican-htmlmin/minify.py b/pelican-htmlmin/minify.py new file mode 100644 index 0000000..d403574 --- /dev/null +++ b/pelican-htmlmin/minify.py @@ -0,0 +1,53 @@ + +from pelican import signals + +import multiprocessing +import htmlmin +import os +import re + +import logging +logger = logging.getLogger(__name__) + + +def run(pelican): + + if pelican.setings.get('MINIFY_DEBUG', False): + return # specifically told to minify content or not + if logging.getLevelName(logger.getEffectiveLevel()) == 'DEBUG': + return # pelican is in debug mode, skip minification + + options = pelican.settings.get('MINIFY_OPTIONS', {}) + htmlfile = re.compile( + pelican.settings.get('MINIFY_MATCH', r'.html?$') + ) + pool = multiprocessing.Pool() + + # find all matching files and give to workers to minify + for base, dirs, files in os.walk(pelican.settings['OUTPUT_PATH']): + for f in filter(htmlfile.search, files): + filepath = os.path.join(base, f) + pool.apply_async(worker, (filepath, options)) + + # wait for the workers to finish + pool.close() + pool.join() + + +def worker(filepath, options): + """use htmlmin to minify the given file""" + rawhtml = open(filepath, encoding='utf-8').read() + with open(filepath, 'w', encoding='utf-8') as f: + logger.debug('Minifying: %s' % filepath) + try: + compressed = htmlmin.minify(rawhtml, **options) + f.write(compressed) + except Exception as e: + logger.critical('Minification failed: {}'.format(e)) + + +def register(): + """ + Minify HTML at the end of the processing loop + """ + signals.finalized.connect(run) diff --git a/readme.md b/readme.md deleted file mode 100644 index 898c80c..0000000 --- a/readme.md +++ /dev/null @@ -1,5 +0,0 @@ -# pelican-minify -An [Pelican] plugin to use htmlmin to remove extra whitespace from webpages. - -[Pelican]: "http://pelican.readthedocs.org/en/latest/" - -- cgit 22.23.6