extract-inline-styles.pl - Move inline CSS styles to a stylesheet
0.001
perl extract-inline-styles.pl [OPTIONS] [<FILENAME]
extract-inline-styles.pl traverses an HTML document or fragment, replacing inline style attributes[1] with generated classes and places the styles for the classes in a <style>
tag in the <head>
element of the document or at the end (sic!) of the document if no <head>
element is found.
[1] See https://www.nomensa.com/blog/2011/inline-styles-and-why-they-are-considered-harmful-accessibility
To minimize redundancy styles are normalized and elements with equivalent styles receive the same class. Normalization is performed by the following steps:
- Hexadecimal RGB color expressions of the form
#789abc
or#9ab
are normalized to six uppercase hex digits#789ABC
/#99AABB
. - Sequences of whitespace are replaced with a single space character.
- Leading and trailing whitespace is removed.
- Whitespace before colons and semicolons is removed.
- Property names are lowercased.
- Properties are sorted alphabetically.
Styles which are identical after this normalization are subsumed under the same class.
-
-b, --bom ; -B, --no-bom
-
Put or don't put a Byte Order Mark at the beginning of the output.
If this is left undefined a BOM will be included in the output if there was a BOM in the input HTML file.
-
-
-e, --encoding ENCODING (Default: UTF-8)
-
The encoding to use if reading from STDIN or if no encoding declaration is found in the input file.
The output encoding is guaranteed to be the one found in the input file, if any, or else this encoding.
-
-
-i, --input FILENAME
- If specified the file FILENAME will be opened with IO:HTML, and the HTML will be read from it. Otherwise the HTML will be read from STDIN.
-
-o, --output FILENAME
- If specified the file FILENAME will be opened with IO:HTML, and the processed HTML will be written to it. Otherwise the HTML will be written to STDOUT.
-
-p, --prefix STRING (Default: 'inline-style-' )
- The prefix for the classes generated for each unique style. Each class will consist of this prefix plus a unique number.
- Encode
- Getopt::Long
- IO::HTML
- List::Pairwise
- Mojo::DOM
- Pod::Usage
- autodie 2.29
- perl 5.010001
Benct Philip Jonsson (bpjonsson@gmail.com, https://github.com/bpj)
Copyright 2017- Benct Philip Jonsson
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. See http://dev.perl.org/licenses/.