NAME

CSS::Tidy - reformat CSS

SYNOPSIS

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

VERSION

This documents version 0.04 of CSS-Tidy corresponding to git commit d237baf76e9f413725f83c33b825d7ed347e86d4 released on Sat Jul 30 19:54:16 2022 +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: 2021-01-28; Version: 0.04]

CSS::Tiny

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

Claims to reduce memory usage compared to the CSS module.

Preprocessors

Crayon

[⭐ Author: LNATION; Date: 2021-04-15; Version: 0.07]

"dedupe, minify and extend CSS". We haven't tried it, but from the documentation it seems to be a CSS preprocessor.

CSS::LESS

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

CSS::Prepare

[Author: MNF; Date: 2011-06-20; Version: v0.9.2.4]

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.

CSS::Scopifier

[⭐ Author: VANSTYN; Date: 2015-08-27; Version: 0.04]

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: 2021-02-07; Version: 0.13]

CSS::Packer

[⭐ Author: LEEJO; Date: 2021-03-02; Version: 2.08]

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]

CSS::Selector::Parser

[Author: HDP; Date: 2011-11-06; Version: 0.003]

CSS::Selector::Grammar

[Author: DFH; Date: 2014-07-04; Version: 0.002]

Graphics::ColorNames::CSS

[⭐ Author: CFAERBER; Date: 2019-05-01; Version: 1.14]

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-2022 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.