CSS Preprocessor tool and library

csspp

CSS Preprocessor (csspp) is a scripting language that transforms advanced (extended) CSS code into a script that browsers can read.

The input to csspp can be the same as standard CSS, or it can use extensions as described below such as using basic math, embedding rules in other rules (cascading by nesting) and many other things.

The project offers a C++ library that can readily be used in your own C++ project and a command line tool.

Status: version 1.0.10 released.

I have go a complete csspp command line to compile your CSS or SCSS files to one of 4 available formats.

Lexer

The lexer reads UTF-8 files and transform them in token.

Parser

The parser makes sure that the basic CSS 3 syntax is respected (that you have ':' and '{}' in the right place.)

Compiler

The compiler transforms the tree in a set of @-keywords and rules with declarations. This includes quite a few validations already.

The compiler includes support for C/C++ like expressions. It supports logical operators (&&, ||, and ^^), relational operators (<, >, <=, >=, =, and !=), additive operators (+ and -), multiplicative operators (*, /, and %), power operators (**), unary expressions (- and +), many functions.

What is still missing is a validation process of all the declarations. There are two such validations: (a) in a specific rule can only have a certain number of declarations, for example content: is not available for a table field; and (b) each declaration only accept a certain number of data entry that have to follow a specific syntactical definition from the CSS 3 language. For example, the color: declaration expects one parameter which has to be a color or one of the few identifiers that can be used in that declaration (such as inherit.) This will be done using external files as we already have to validate various entries such as identifiers used after a colon (:) in a list of selectors. These identifiers are limited and thus we can very easily check them and make sure they are valid.

Assembler

The assembler outputs a tree the compiler just worked on in one of 4 different formats (compressed, tidy, expanded, beautified.)

Source

A CSS Preprocessor tarball can be downloaded as a tarball from SourceForge.net. You can also find the source in the snapcpp project, again on SourceForge.net:

https://sourceforge.net/p/snapcpp/code/ci/master/tree/csspp/

Note that to compile the source you also need the snapCMakeModules, controlled_vars, and advgetopt modules. All of those are part of the tarball distribution.

Documentation

References of the csspp C++ library and command line are available online on this CSS Preprocessor website.

Example

The scripting language supported by csspp is based on the language developed by SASS. This language allows you to include variables and nested rules making it easier to develop complex rules.

$error_color: red;
div.warning {
  width: 75%;
  a {
    color: $error_color;
  }
}

This definition results in the following output:

div.warning {
  width: 75%;
}
div.warning a {
  color: red;
}

The variable was replaced by its content, and the nested rule (a) was output unnested.

These are certainly the main two features of this scripting language. There are many other features described in the documentation.

SASS

csspp is partially based on SASS. Most of the extensions of SASS will be implemented in csspp. However, the main reason for me to write csspp is to validate all the CSS rules before sending them to a browser and not so much having support for automatically generated rules and other such features of SASS.

Changes

1.0.10

  • Added a fix to the output of declaration with multiple argument lists. Each argument list has to be separated by a comma.

1.0.9

  • Added a few catch() to main() of the csspp tool. (Coverity)
  • Fixed a bug (4 instances) with the '&' operator. Version 1.0.8 had one fix.

1.0.8

  • Fixed a potential buffer overflow in lexer::ungetc().
  • Fixed a test in a while(), missing parenthesis, in a test.
  • Compiler created using 'this' instead of 'true' as parameter.
  • Fixed the name of a #define of a header file.

1.0.7

  • Keep identifier case since it can be important for classes and tags.

1.0.6

  • Implemented the @return so external functions work as expected.
  • Bug fixes and documentation updates.

1.0.5

  • Tests all the expressions, fixed many bugs in that area.
  • Quite a few other bug fixes.

1.0.4

  • Tests and corrections to the implementation of the power operator.

1.0.3

  • Tests and corrections to the implementation of the multiplicative operator.

1.0.2, 1.0.1

  • Various changes in link with bugs I have found while implementing the final tests (outside of the expression tests).
Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly