* Licenced under Creative Commons BY-NC-SA terms * (http://creativecommons.org/licenses/by-nc-sa/3.0/). * * Converts an enhanced CSS to the standard one. * The enhancements handled are: * - // one-line comment * - @define CONSTANT value * - $CONSTANT expansion * */ /* * Example: */ $ESS_SOURCE = ' @define BG: white; // the default background @define CANVAS_WIDTH: 320px; body { background: $BG; // we do not use the canvas width here font-family: $FONT; } '; $CSS_OUTPUT = ' body { background: white; /* we do not use the canvas width here */ font-family: /* ECSS: constant FONT not found */; } '; /* * Limitations: * - one-line comments within constant declarations (before ';') cause havoc * - constants are expanded even within comments */ /* ----- do not touch below ----------------------------------------- */ $CONST_IDENTIFIER = '[_A-Za-z][-_A-Za-z0-9]*'; // @define const value $CONST_DEF = '/@define\s+('.$CONST_IDENTIFIER.')\s+(.*)\s*$/'; // { property: $const ; } $CONST_USE = '/\$('.$CONST_IDENTIFIER.')/'; // one-line comment, C++ style $ONE_LINE_COMMENT = '/(\/\/\s)(.*)$/'; // space required to prevent URL mistreatment // no or special arg -> print usage and die if (empty($_SERVER['QUERY_STRING']) || isset($_GET['help']) || isset($_GET['usage'])) { header("Content-type: text/plain"); echo ' Trivially Enhanced CSS Converter, version 1.0 Copyright (c) 2007 Premek Brada Licenced under Creative Commons BY-NC-SA terms (http://creativecommons.org/licenses/by-nc-sa/3.0/). Converts an enhanced CSS to the standard one. The enhancements handled are: - // one-line comment - @define CONSTANT value - $CONSTANT expansion Use the URI of the form http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?f=http://your.domain/your-style.css to run your CSS through the converter. '; die; } // which uri to parse $CSS = isset($_GET['f']) ? $_GET['f'] : ''; if (!$CSS) { error_log("TECSS parser: No extended CSS to parse, exiting."); die; } $f = @fopen($CSS,"r"); if ($f) { header("Content-Type: text/css"); $constants = array(); while (!feof($f)) { $buffer = fgets($f, 4096); // create regular comment out of a one-liner if ( strstr($buffer,'//') ) { $res = preg_replace($ONE_LINE_COMMENT, '/* \2 */', trim($buffer)); $buffer = $res."\n"; } // (attempt to) expand a const if ( strstr($buffer,'$') ) { preg_match($CONST_USE,$buffer, $matches); $constref = $matches[1]; $is_const = array_key_exists( $constref, $constants ); $constval = $is_const ? $constants[$constref] : '/* TECSS parser: constant '.$constref.' not found */'; $res = preg_replace('/\$'.$constref.'/', $constval, $buffer); $buffer = $res; } // define a const if ( strstr($buffer,'@define') ) { $matches = array(); $r = preg_match($CONST_DEF, trim($buffer), $matches); if ($r) { $constants[$matches[1]] = $matches[2]; } $buffer = ''; } echo $buffer; } fclose($f); } else { error_log("TECSS parser: Could not open CSS file $CSS to parse, exiting."); } ?>