NAME

CSS::Tidy - reformat CSS

SYNOPSIS

use CSS::Tidy 'tidy_css';
my $output = tidy_css ($input);

VERSION

This documents version 0.03 of CSS-Tidy corresponding to git commit d5e498c8e56c332304999dec5f4f915a7c67a6cc released on Sat Mar 27 10:49:16 2021 +0900.

DESCRIPTION

This reformats CSS.

The idea is to have a formatting script which can be used to consistently format CSS.

FUNCTIONS

copy_css

copy_css (in => $infile, out => $outfile);

Copy CSS from a file specified by "in" to a file specified by "out".

in

Input file. The file is read in via the function read_text in "File::Slurper" so it must be in the UTF-8 encoding.

out

Output file. The file is written using write_text from "File::Slurper" so it must be in the UTF-8 encoding.

decomment

Remove comments from the output file.

tidy_css

my $output = tidy_css ($input, %options);

Given CSS in $input, reformat it into a consistent format and return the value.

Options are as follows:

decomment

Set to a true value to remove comments from the output

TRANSFORMATIONS APPLIED

Indentation

Indentation is changed to multiples of four spaces.

Trailing whitespace

Trailing whitespace (blanks after the end of the line) is removed.

Property/value pairs

The spacing between property/value pairs such as

font-size:21em;

is altered to put a single space after the colon:

font-size: 21em;
Declaration blocks

A blank line is inserted between declaration blocks.

Single-line braces are expanded out
.something { something:something; }

is expanded to

.something {
    something: something;
}
Comments have a blank line before and after

If the comment is on a line by itself, it will be given one blank line before and after itself.

SCRIPT

The script csstidy runs "tidy_css" on STDIN and prints the result to STDOUT. There is also css-tidy.el in the distribution which enables one to run csstidy on the current buffer.

DEPENDENCIES

C::Tokenize

This is used for comment regex.

File::Slurper

This is used to read and write files

SEE ALSO

About CSS

CSS Specification

CSS Syntax Module Level 3 at the W3 Consortium.

Other CPAN modules

Parsers

CSS

[Author: IAMCAL; Date: 2011-03-22; Version: 1.09]

CSS::Croco

[Author: GUGU; Date: 2009-12-26; Version: 0.09]

CSS::DOM

[⭐ Author: SPROUT; Date: 2018-01-29; Version: 0.17]

CSS::Object

[⭐ Author: JDEGUEST; Date: 2020-08-12; Version: v0.1.3]

CSS::SAC

[Author: BJOERN; Date: 2008-07-05; Version: 0.08]

👎 This seems to be unusable, it fails on the CSS stylesheets we've tried it on.

CSS::Simple

[Author: KAMELKEV; Date: 2017-11-16; Version: 3224]

CSS::Struct

[Author: SKIM; Date: 2020-08-11; Version: 0.03]

CSS::Tiny

[⭐ Author: CHORNY; Date: 2016-03-10; Version: 1.20]

Preprocessors

CSS::LESS

[Author: MUGIFLY; Date: 2013-09-20; Version: v0.0.3]

CSS::Sass

[⭐ Author: OCBNET; Date: 2020-05-04; Version: v3.6.3]

Unfortunately this is built on top of a deprecated library which is being replaced with Dart Sass.

Minifiers

CSS::Compressor

[⭐ Author: JANUS; Date: 2018-12-03; Version: 0.05]

CSS::Minifier

[⭐ Author: PMICHAUX; Date: 2007-08-03; Version: 0.01]

CSS::Minifier::XS

[⭐ Author: GTERMARS; Date: 2020-12-31; Version: 0.11]

CSS::Packer

[⭐ Author: LEEJO; Date: 2018-11-06; Version: 2.07]

CSS::Squish

[Author: TSIBLEY; Date: 2010-11-05; Version: 0.10]

Other

CSS::Coverage

[⭐ Author: SARTAK; Date: 2013-08-08; Version: 0.04]

👎 Test whether your styles are actually in use or not. Unfortunately it depends on "CSS::SAC" which seems to be unusable.

CSS::Inliner

[⭐ Author: KAMELKEV; Date: 2020-01-01; Version: 4014]

Graphics::ColorNames::CSS

This provides a map from the names of colours valid in CSS to their RGB values.

Test::CSS

[Author: MANWAR; Date: 2017-09-09; Version: 0.08]

Test CSS files or strings using regular expressions and a list of properties. The properties list is somewhat out of date, for example it doesn't have the touch-action property. See https://github.com/manwar/Test-CSS/issues/2.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

This package and associated files are copyright (C) 2021 Ben Bullock.

You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.