.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "HTMLMIN" "1" "Apr 06, 2024" "0.1" "htmlmin" .SH NAME htmlmin \- htmlmin Documentation .sp An HTML Minifier with Seatbelts .SH QUICKSTART .sp For single invocations, there is the \fI\%htmlmin.minify\fP method. It takes input html as a string for its first argument and returns minified html. It accepts multiple different options that allow you to tune the amount of minification being done, with the defaults being the safest available options: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C >>> import htmlmin >>> input_html = \(aq\(aq\(aq

htmlmin rocks

      and rolls
    
\(aq\(aq\(aq >>> htmlmin.minify(input_html) u\(aq

htmlmin rocks

\en        and rolls\en      
\(aq >>> print htmlmin.minify(input_html)

htmlmin rocks

        and rolls
      
.ft P .fi .UNINDENT .UNINDENT .sp If there is a chunk of html which you do not want minified, put a \fBpre\fP attribute on an HTML tag that wraps it. htmlmin will leave the contents of the tag alone and will remove the \fBpre\fP attribute before it is output: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C >>> import htmlmin >>> input_html = \(aq\(aq\(aq minified not minified \(aq\(aq\(aq >>> htmlmin.minify(input_html) u\(aq minified not minified \(aq .ft P .fi .UNINDENT .UNINDENT .sp Attributes will be condensed to their smallest possible representation by default. You can prefix an individual attribute with \fBpre\-\fP to leave it unchanged: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C >>> import htmlmin >>> input_html = \(aq\(aq\(aq\(aq\(aq\(aq >>> htmlmin.minify(input_html) u\(aq\(dq>\(aq .ft P .fi .UNINDENT .UNINDENT .sp The \fI\%minify\fP function works well for one off minifications. However, if you are going to minify several pieces of HTML, the \fI\%Minifier\fP class is provided. It works similarly, but allows for persistence of options between invocations and recycles the internal data structures used for minification. .SS Command Line .sp htmlmin is invoked by running: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C htmlmin input.html output.html .ft P .fi .UNINDENT .UNINDENT .sp If no output file is specified, it will print to \fBstdout\fP\&. If no input specified, it reads form \fBstdin\fP\&. Help with options can be retrieved at any time by running \fIhtmlmin \-h\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C htmlmin \-h usage: htmlmin [\-h] [\-c] [\-s] [\-\-remove\-all\-empty\-space] [\-\-keep\-optional\-attribute\-quotes] [\-H] [\-k] [\-a PRE_ATTR] [\-p [TAG [TAG ...]]] [\-e ENCODING] [INPUT] [OUTPUT] Minify HTML positional arguments: INPUT File path to html file to minify. Defaults to stdin. OUTPUT File path to output to. Defaults to stdout. optional arguments: \-h, \-\-help show this help message and exit \-c, \-\-remove\-comments When set, comments will be removed. They can be kept on an individual basis by starting them with a \(aq!\(aq: . The \(aq!\(aq will be removed from the final output. If you want a \(aq!\(aq as the leading character of your comment, put two of them: . \-s, \-\-remove\-empty\-space When set, this removes empty space betwen tags in certain cases. Specifically, it will remove empty space if and only if there a newline character occurs within the space. Thus, code like \(aqx y\(aq will be left alone, but code such as \(aq ... ...\(aq will become \(aq......\(aq. Note that this CAN break your html if you spread two inline tags over two lines. Use with caution. \-\-remove\-all\-empty\-space When set, this removes ALL empty space betwen tags. WARNING: this can and likely will cause unintended consequences. For instance, \(aqX Y\(aq will become \(aqXY\(aq. Putting whitespace along with other text will avoid this problem. Only use if you are confident in the result. Whitespace is not removed from inside of tags, thus \(aq \(aq will be left alone. \-\-keep\-optional\-attribute\-quotes When set, this keeps all attribute quotes, even if they are optional. \-H, \-\-in\-head If you are parsing only a fragment of HTML, and the fragment occurs in the head of the document, setting this will remove some extra whitespace. \-k, \-\-keep\-pre\-attr HTMLMin supports the propietary attribute \(aqpre\(aq that can be added to elements to prevent minification. This attribute is removed by default. Set this flag to keep the \(aqpre\(aq attributes in place. \-a PRE_ATTR, \-\-pre\-attr PRE_ATTR The attribute htmlmin looks for to find blocks of HTML that it should not minify. This attribute will be removed from the HTML unless \(aq\-k\(aq is specified. Defaults to \(aqpre\(aq. You can also prefix individual tag attributes with \(ga\(ga{pre_attr}\-\(ga\(ga to prevent the contents of the individual attribute from being changed. \-p [TAG [TAG ...]], \-\-pre\-tags [TAG [TAG ...]] By default, the contents of \(aqpre\(aq, and \(aqtextarea\(aq tags are left unminified. You can specify different tags using the \-\-pre\-tags option. \(aqscript\(aq and \(aqstyle\(aq tags are always left unmininfied. \-e ENCODING, \-\-encoding ENCODING Encoding to read and write with. Default \(aqutf\-8\(aq. .ft P .fi .UNINDENT .UNINDENT .SH TUTORIAL & EXAMPLES .sp Coming soon... .SH API REFERENCE .SS Main Functions .INDENT 0.0 .TP .B htmlmin.minify(input, remove_comments=False, remove_empty_space=False, remove_all_empty_space=False, reduce_empty_attributes=True, reduce_boolean_attributes=False, remove_optional_attribute_quotes=True, convert_charrefs=True, keep_pre=False, pre_tags=(\(aqpre\(aq, \(aqtextarea\(aq), pre_attr=\(aqpre\(aq, cls=) Minifies HTML in one shot. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBinput\fP \-\- A string containing the HTML to be minified. .IP \(bu 2 \fBremove_comments\fP \-\- .sp Remove comments found in HTML. Individual comments can be maintained by putting a \fB!\fP as the first character inside the comment. Thus: .INDENT 2.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .sp Will become simply: .INDENT 2.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .sp The added exclamation is removed. .IP \(bu 2 \fBremove_empty_space\fP \-\- 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. .IP \(bu 2 \fBremove_all_empty_space\fP \-\- A more extreme version of \fBremove_empty_space\fP, this removes all empty whitespace found between tags. This is almost guaranteed to break your HTML unless you are very careful. .IP \(bu 2 \fBreduce_boolean_attributes\fP \-\- Where allowed by the HTML5 specification, attributes such as \(aqdisabled\(aq and \(aqreadonly\(aq will have their value removed, so \(aqdisabled=\(dqtrue\(dq\(aq will simply become \(aqdisabled\(aq. This is generally a good option to turn on except when JavaScript relies on the values. .IP \(bu 2 \fBremove_optional_attribute_quotes\fP \-\- When True, optional quotes around attributes are removed. When False, all attribute quotes are left intact. Defaults to True. .IP \(bu 2 \fBconver_charrefs\fP \-\- Decode character references such as & and . to their single charater values where safe. This currently only applies to attributes. Data content between tags will be left encoded. .IP \(bu 2 \fBkeep_pre\fP \-\- By default, htmlmin uses the special attribute \fBpre\fP to allow you to demarcate areas of HTML that should not be minified. It removes this attribute as it finds it. Setting this value to \fBTrue\fP tells htmlmin to leave the attribute in the output. .IP \(bu 2 \fBpre_tags\fP \-\- 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 include \fBpre\fP and \fBtextarea\fP if you make any changes to the list. Note that \fB