minify(input, remove_comments=False, remove_empty_space=False, remove_all_empty_space=False, reduce_boolean_attributes=False, keep_pre=False, pre_tags=(u'pre', u'textarea'), pre_attr='pre')¶
Minifies HTML in one shot.
- input – A string containing the HTML to be minified.
- remove_comments –
Remove comments found in HTML. Individual comments can be maintained by putting a
!as the first character inside the comment. Thus:
<!-- FOO --> <!--! BAR -->
Will become simply:
<!-- BAR -->
The added exclamation is removed.
- remove_empty_space – Remove empty space found in HTML between an opening and a closing tag and when it contains a newline or carriage return. If whitespace is found that is only spaces and/or tabs, it will be turned into a single space. Be careful, this can have unintended consequences.
- remove_all_empty_space – A more extreme version of
remove_empty_space, this removes all empty whitespace found between tags. This is almost gauranteed to break your HTML unless you are very careful. nothing
- keep_pre – By default, htmlmin uses the special attribute
preto allow you to demarcate areas of HTML that should not be minified. It removes this attribute as it finds it. Setting this value to
Truetells htmlmin to leave the attribute in the output.
- pre_tags – A list of tag names that should never be minified. You are
free to change this list as you see fit, but you will probably want to
textareaif you make any changes to the list. Note that
<style>tags are never minimized.
- pre_attr – Specifies the attribute that, when found in an HTML tag,
indicates that the content of the tag should not be minified. Defaults to
A string containing the minified HTML.
If you are going to be minifying multiple HTML documents, each with the same settings, consider using
Minifier(remove_comments=False, remove_empty_space=False, remove_all_empty_space=False, reduce_boolean_attributes=False, keep_pre=False, pre_tags=(u'pre', u'textarea'), pre_attr='pre')¶
An object that supports HTML Minification.
Options are passed into this class at initialization time and are then persisted across each use of the instance. If you are going to be minifying multiple peices of HTML, this will be more efficient than using
htmlmin.minifyfor an explanation of options.
Runs HTML through the minifier in one pass.
Parameters: input – HTML to be fed into the minimizer. Multiple chunks of HTML can be provided, and they are fed in sequentially as if they were concatenated. Returns: A string containing the minified HTML.
This is the simplest way to use an existing
Minifierinstance. This method takes in HTML and minfies it, returning the result. Note that this method resets the internal state of the parser before it does any work. If there is pending HTML in the buffers, it will be lost.
Feed more HTML into the input stream
Parameters: input – HTML to be fed into the minimizer. Multiple chunks of HTML can be provided, and they are fed in sequentially as if they were concatenated. You can also call this method multiple times to achieve the same effect.
Retrieve the minified output generated thus far.
Finishes current input HTML and returns mininified result.
This method flushes any remaining input HTML and returns the minified result. It resets the state of the internal parser in the process so that new HTML can be minified. Be sure to call this method before you reuse the
Minifierinstance on a new HTML document.
HTMLMinMiddleware(app, by_default=True, keep_header=False, debug=False, **kwargs)¶
WSGI Middleware that minifies html on the way out.
- by_default – Specifies if minification should be turned on or off by
default. Defaults to
- keep_header – The middleware recognizes one custom HTTP header that
can be used to turn minification on or off on a per-request basis:
X-HTML-Min-Enable. Setting the header to
truewill turn minfication on; anything else will turn minification off. If
by_defaultis set to
False, this header is how you would turn minification back on. The middleware, by default, removes the header from the output. Setting this to
Trueleaves the header in tact.
- debug – A quick setting to turn all minification off. The middleware is effectively bypassed.
This simple middleware minifies any HTML content that passes through it. Any additional keyword arguments beyond the three settings the middleware has are passed on to the internal minifier. The documentation for the options can be found under
- by_default – Specifies if minification should be turned on or off by default. Defaults to
Minifies HTML that is returned by a function.
A simple decorator that minifies the HTML output of any function that it decorates. It supports all the same options that
htmlmin.minifyhas. With no options, it uses
minify‘s default settings:
@htmlmin def foobar(): return ' minify me! '
@htmlmin(remove_comments=True) def foobar(): return ' minify me! <!-- and remove me! -->'