Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin: HtmlMinPlugin for minifying HTML. #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions hyde/ext/plugins/minify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""
Minifying resources.
"""

from hyde.plugin import Plugin

class HtmlMinPlugin(Plugin):
"""
The plugin class for html minification
Based upon http://htmlmin.readthedocs.org. See the docs for more info.
Supported settings:
extensions: list of file extensions to filter. By default: ['.html', ]
All the parameters supported by the htmlmin library,
e.g: "remove_comments: True".
"""
def __init__(self, site):
super(HtmlMinPlugin, self).__init__(site)
import htmlmin

# Filter out all the settings that are not relevant to htmlmin.
module_settings = dict(self.settings)

self.minifier = htmlmin.Minifier(**module_settings)

def text_resource_complete(self, resource, text):
"""
Minify after finishing with the text.
"""

mode = getattr(self.site.config, 'mode', 'production')

if mode.startswith('dev'):
self.logger.debug("Skipping HtmlMin in development mode.")
return

if resource.source_file.kind != 'html':
return

return self.minifier.minify(text)