CCSS: CSS + Variables + Arithmetic

About CCSS

CCSS is a preprocessor/pretty-printer for CSS (Cascading Style Sheets).
It extends the CSS language with support for declaration of variables
(both for expressions and mixins), and basic arithmetic operations
(addition, subtraction, multiplication, division). The programme is
supposed to be used as a filter: it reads the CSS source from stdin
and outputs its result on stdout.

While it does not claim to support all the dark corners of CSS,
in practice CCSS handles most CSS found in the wild, including CSS3
constructs. Its primary advantage over similar preprocessors is speed:
the project is implemented in Ocaml,
a language renowned for producing native-code executables of blazing
performance.

Features

Variables for expressions and mixins:

With CCSS, you may declare and use variables in your CSS code (in fact,
CCSS variables are actually constants, since they are assigned upon
declaration and may not be changed). Variable identifiers must begin
with an uppercase letter, and be followed by any combination of letters,
numbers, and the characters '-' (dash) and '_'
(underscore).

There are two use cases for variables. The first consists in assigning any
CSS expression (not only quantities) to a variable. The assigned expression
can subsequently be referenced by the variable identifier. The code below
demonstrates this situation:

Foo: 20em;
Bar: 1px solid black;
h1 {
width: Foo;
border: Bar;
}

The second use of variables is to declare mixins, ie, declaration blocks
that can be included within subsequent declaration blocks. The code below
illustrates the use of mixins:

CCSS extends CSS expressions with basic arithmetic operations (addition,
subtraction, multiplication, and division). The operands must be CSS
quantities (either dimensionless or with an attached unit), or other
expressions that ultimately resolve into a quantity. Moreover, variables
whose value is a quantity (or an expression which resolves into a quantity)
may also be used as operand.

The operators are '+', '-', '*',
and '÷'. Note that multiplication and division have
precedence over addition and subtraction, but you may use parentheses
to group operations. Consider thus the following input:

The reader will have noticed that CCSS must be unit-aware when performing
arithmetic. As a matter of fact, the programme performs a basic sanity
check of units, and will complain if you try, for example, to add
'1em' with '10px'. By default, CCSS will
not make any attempt to convert units even if they are convertible,
such 'cm' and 'mm'. If you wish for CCSS to
attempt unit conversion, please provide option '--convert'
on the command line (short version '-c').

Units can be grouped into four categories, and conversion is possible if
the units belong to the same category. Upon conversion, the result will
be the same unit as the first operand. The categories and corresponding
units are as follows:

length:

mm, cm, in, pt, pc

angle:

deg, grad, rad

time:

ms, s

frequency:

hz, khz

As an illustration of unit conversion, the result for all the following
arithmetic operations is the same, '2in':

Dependencies

Downloads and development

CCSS is available in OPAM, which is the easiest and recommended
way to get the package. Otherwise, CCSS is distributed in source-code form. You can get all releases
from the project's page at GitHub or the
OCaml Forge. The latest version is
CCSS 1.6, released on 2016-01-25.

Building and installing

The build system is generated with OASIS.
Run 'configure' if you wish to modify the default install destination. Enter 'make'
and 'make install' to build and install the binary executable.