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.

CCSS is developed by Dario Teixeira and is licensed under the terms of the GNU GPL 2.0.

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:

Inverse_scheme:
	{
	color: #fff;
	background: #000;
	}

h1
	{
	Inverse_scheme;
	font-weight: bold;
	}
Arithmetic:

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:

Left: 10em;
Right: 5em;
Total: Left + Right;

h1	{
	padding: (1.5em + 0.5em) * 2;
	width: 2 * Total;
	}

CCSS will produce the following output:

h1
	{
	padding: 4em;
	width: 30em;
	}

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':

h1
	{
	foo1: 1in + 1in;
	foo2: 1in + 2.54cm;
	foo3: 1in + 25.4mm;
	foo4: 1in + 72pt;
	foo5: 1in + 6pc;
	}

Dependencies

Menhir is used as the parser generator, while scanning is done with Ulex. Other requirements are Batteries and Pcre-ocaml.

Downloads and development

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.4, released on 2013-09-27. Here is the changelog for the last few releases:

Version 1.4 (2013-09-27)

  • Support for mixins.
  • Support for unicode ranges.

Version 1.3 (2012-09-28)

  • Now using OASIS for build system.
  • Migrated from ExtLib to Batteries.

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.

Links