diff options
author | Bryan Brattlof <bryanbrattlof@gmail.com> | 2019-09-08 20:47:36 -0500 |
---|---|---|
committer | Bryan Brattlof <bryanbrattlof@gmail.com> | 2019-09-08 20:47:36 -0500 |
commit | dec6c17f8de4de90fe16dc5c66d575301faf58f5 (patch) | |
tree | 0ff9eeda3869870a19cb9adac1f50778510e6a23 | |
parent | b141915cb947d93003cbf32071938c556ff20781 (diff) | |
parent | 01958e67a3c440e6bc66123bc48ac7a37a89a6a7 (diff) | |
download | pelican-htmlmin-dec6c17f8de4de90fe16dc5c66d575301faf58f5.tar.gz pelican-htmlmin-dec6c17f8de4de90fe16dc5c66d575301faf58f5.tar.bz2 |
Merge pypi updates1.0.0
-rw-r--r-- | .gitlab-ci.yml | 11 | ||||
-rw-r--r-- | __init__.py | 49 | ||||
-rw-r--r-- | license.txt | 18 | ||||
-rw-r--r-- | pelican-htmlmin/__init__.py | 1 | ||||
-rw-r--r-- | pelican-htmlmin/minify.py | 61 | ||||
-rw-r--r-- | readme.md | 5 | ||||
-rw-r--r-- | readme.rst | 95 | ||||
-rw-r--r-- | setup.py | 55 |
8 files changed, 241 insertions, 54 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..a4cc4cc --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,11 @@ +image: python:latest + +pypi: + stage: deploy + before_script: + - pip install twine + - python setup.py sdist bdist_wheel + script: + - twine upload dist/* + only: + - tags
\ No newline at end of file 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/license.txt b/license.txt new file mode 100644 index 0000000..cdfa46f --- /dev/null +++ b/license.txt @@ -0,0 +1,18 @@ +Copyright 2019 Bryan Brattlof + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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..2c84f1f --- /dev/null +++ b/pelican-htmlmin/minify.py @@ -0,0 +1,61 @@ +from pelican import signals + +import multiprocessing +import htmlmin +import os +import re + +import logging +logger = logging.getLogger(__name__) + + +def run(pelican): + + # should we run or not + if pelican.settings.get( + 'HTMLMIN_DEBUG', + logger.getEffectiveLevel() == logging.DEBUG + ): + # HTMLMIN_DEBUG is True or Pelican is in DEBUG mode + # Don't minify the output to help with debugging + return + + options = pelican.settings.get( + 'HTMLMIN_OPTIONS', + { + 'remove_commends': True, + 'remove_all_empty_space': True, + 'remove_optional_attribute_quotes': False + } + ) + htmlfile = re.compile( + pelican.settings.get('HTMLMIN_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: %s', e) + + +def register(): + """minify HTML at the end of the pelican build""" + 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/" - diff --git a/readme.rst b/readme.rst new file mode 100644 index 0000000..ec13d99 --- /dev/null +++ b/readme.rst @@ -0,0 +1,95 @@ +pelican-htmlmin +############### + +A `Pelican <https://github.com/getpelican/pelican/>`_ plugin that uses +`htmlmin <https://github.com/mankyd/htmlmin>`_ to remove comments and extra +whitespace from your website after the pages have been generated. + +Turning this: + +.. code-block:: html + + <html> + <head> + <title> Hello, World! </title> + </head> + <body> + <p> How <em>you</em> doing? </p> + </body> + </html> + + +Into this: + +.. code-block:: html + + <html><head><title>Hello, World!</title><body><p> How <em>you</em> doing? </p></body></html> + +Installing +########## + +:code:`pelican-htmlmin`, is available on pip: + +.. code-block:: + + $ pip install pelican-htmlmin + +After you've installed the package, update the :code:`PLUGINS` variable in your +:code:`pelicanconf.py` to include the package in the next pelican build. + +.. code-block:: python + + PLUGINS = [ + # ... + 'pelican-htmlmin', + # ... + ] + +Options +####### + +HTMLMIN_DEBUG +============= + +To help with debugging, if Pelican is in DEBUG mode, :code:`pelican-htmlmin` +will not minify files. You can override this using the :code:`HTMLMIN_DEBUG`: + +.. code-block:: python + + # minify generated files only if pelican is in DEBUG mode + HTMLMIN_DEBUG = logger.getEffectiveLevel() == logging.DEBUG + +HTMLMIN_MATCH +============= + +By default, :code:`pelican-htmlmin` looks for files ending with :code:`.html` +or :code:`.htm` to minify. Use the :code:`HTMLMIN_MATCH` to update the regular +expression that matches with the files you wish to minify. + +.. code-block:: python + + HTMLMIN_MATCH = r'.html?$' + +HTMLMIN_OPTIONS +=============== + +If you wish to pass arguments to :code:`htmlmin` directly, use the +:code:`HTMLMIN_OPTIONS` in :code:`{key: value}` form. + +.. code-block:: python + + HTMLMIN_OPTIONS = { + 'remove_commends': True, + 'remove_all_empty_space': True, + 'remove_optional_attribute_quotes': False + } + +For more information on the arguments you can give :code:`htmlmin`, see their +`documentation here +<https://htmlmin.readthedocs.io/en/latest/reference.html#main-functions>`_ + +Contributing +############ + +Please feel free to help. Issues, pull requests, and `patches via email +<https://bryanbrattlof.com/connect/>`_, all are warmly welcomed. diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..80bf6c4 --- /dev/null +++ b/setup.py @@ -0,0 +1,55 @@ +from setuptools import setup +from io import open +import os + +pwd = os.path.abspath(os.path.dirname(__file__)) + +VERSION = os.environ.get('CI_COMMIT_TAG', '0.0.0') + +REQUIRES = [ + 'htmlmin', +] + +description = open(os.path.join(pwd, 'readme.rst'), encoding='utf-8').read() + +setup( + name='pelican-htmlmin', + packages=['pelican-htmlmin'], + + version=VERSION, + url='https://gitlab.com/bryanbrattlof/pelican-htmlmin', + + description="Minifies HTML files generated by Pelican", + long_description=description, + + author='Bryan Brattlof', + author_email='hello@bryanbrattlof.com', + + license='MIT', + + install_requires=REQUIRES, + + # See https://pypi.python.org/pypi?%3Aaction=list_classifiers + classifiers=[ + 'Development Status :: 5 - Production/Stable', + + 'Intended Audience :: Developers', + 'Topic :: Software Development :: Libraries', + + 'Framework :: Pelican', + 'Framework :: Pelican :: Plugins', + + 'License :: OSI Approved :: MIT License', + + # Specify the Python versions you support here. In particular, ensure + # that you indicate whether you support Python 2, Python 3 or both. + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + + 'Natural Language :: English', + ], +) |