CSSPP is a PHP based cascading stylesheet preprocessor intended to extend the capabilities of CSS beyond specification, reduce typing and ease production of more modular/logical standards compliant css files.
Used and tested on a few production sites, it has allowed highly effective, clean, and quick output of complex layouts. Now free to the public; available as zip file or at framwurk.org subversion repository. Accepting patches, bug reports, ideas and better example/documentation content.
Download http://framwurk.org/data/csspp-0.9.1.zip and extract the directory csspp into your host's document root. Alternatively, you may export the code from the framwurk.org subversion repository into a directory named 'csspp':
svn export https://svn.framwurk.org/~/csspp/trunk csspp
Here is an example of a document root tree with the css preprocessor in place
Create an .htaccess file in the document root with the following contents:
# Turn on Rewriting
RewriteEngine On
RewriteBase /
# CSS Preprocessor
RewriteCond %{REQUEST_URI} \.CSS$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /csspp/process.php?css=%{REQUEST_FILENAME} [L]
This will effectively redirect all requests to filenames with a .CSS extension to the css preprocessor (leaving your *.css files alone).
The system you are on should support case-sensitive file names (linux, osx, unix, etc), otherwise you must modify the rewrite above (and process.php) to use another expression and conditions.
Change the permissions of /csspp/cache/ directory to 771, so that the cached files dont leak out for any reason.
You should now be able to request http://example.com/stylesheet-to-process.CSS (or the path/name of your style sheet) to output the stylesheet preprocessed into valid css.
You may insert a style sheet with the extension .CSS (capitalized; instead of .css) anywhere in the document root and it will be automatically preprocessed.
Your style sheet should be valid and clean with semi-colons in the right places... no error recovery implemented yet.
<link rel="styleshet" type="text/css" href="/styles/parse/me.CSS" />
You may change the sequence options before the call to parse() in process.php like so (the default options shown):
$csspp->sequence_options = csspp::COMMENTS|csspp::CONSTANTS|csspp::BASES|csspp::NESTED|csspp::FORMAT|csspp::TIMESTAMP|csspp::INCLUDES;
Include a file on the server side. Paths are relative to the current file.
@include (../path/relative/to/the/current/file.CSS);
Constants can be defined in singular or plural forms:
@constant name:value;
@constants {
name:value;
...
}
An example of using a constant to set the body's background-color:
@constant background_color:red; body { background: background_color; }
generates:
body { background: red; }
As of version 0.8, this is disabled by default. Variables can be passed via the query string. Any variables defined are replaced with the $_GET array from php. They can be enabled via options.
It is against recommendation to use variables in production enviroments as they can cause the stylesheet to recompiled on every request; leaving a hole for a potential denial of service.
@variable $background_color:white;
body {
background:$background_color;
}
when accessed with http://example.com/styles/test.css?background_color=red, will generate:
body {
background:red;
}
Above, $background_color's default is set to white.
rules can be nested
to append a class to a parent selector you must use the .. (double dot) operator.
body { ..blue { background:blue; } ..red { background:red; } .box { background:green; } h1 { font-size:1.6em; } h2 { font-size:1.4em; } }
produces:
body.blue { background:blue; } body.red { background:red; } body .box { background:green; } body h1 { font-size:1.6em; } body h2 { font-size:1.4em; }
nested rules with a . (dot) will be appended as a child selector.
One can also pull in properties from multiple "abstract class"-type rules.
@abstract base_class {
padding:1em;
}
body {
extends:base_class;
background:blue;
}
generates
body { padding:1em; background:blue; }
Comments can be inserted for multiples lines like:
/* Multiple line comments ... */
and for single lines like:
// single line comment
As of version 0.9, a // directly preceded by a colon is considered part of a property value (ie. http://, ftp://). However, things will break if a // is found without a preceding colon anywhere else within a property value. This is a known issue and the only way to prevent it is to put the rule in a normal *.css file (or a @nopp file) or disable the comment stripping via the options.
to prevent processing of the file, even if it is named *.CSS, prepend @nopp inside of it.
@nopp
This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 Unported License.