Skip to content

Instantly share code, notes, and snippets.

@hejrobin
Created August 5, 2011 13:36
Show Gist options
  • Save hejrobin/1127549 to your computer and use it in GitHub Desktop.
Save hejrobin/1127549 to your computer and use it in GitHub Desktop.
<?php
//ini_set('display_errors', 'On');
//error_reporting(E_ALL);
class Stylesheet {
private $css_rule_regex = '/(?ims)([a-z0-9\s\.\:#_\-@]+)\{([^\}]*)\}/';
public function __construct($css_data) {
$this->unparsed = $css_data;
$this->css_data = $css_data;
}
protected function parse() {
preg_match_all($this->css_rule_regex, $this->css_data, $css_rules);
$parsed_css = array();
foreach($css_rules[0] as $key => $data) {
$selector = trim($css_rules[1][$key]);
$rules = explode(';', trim($css_rules[2][$key]));
$parsed_css[$selector] = array();
foreach($rules as $rule) {
$rule = trim($rule);
if(!empty($rule)) {
$_rule = array(
trim(substr($rule, 0, strpos($rule, ':'))),
trim(substr($rule, strpos($rule, ':')), ': ')
);
$parsed_css[$selector][][$_rule[0]] = $_rule[1];
}
}
}
return $parsed_css;
}
public function parseReserved() {
preg_match_all('~^(@[a-z0-9_\-]+)\s+([^\r\n]+)~im', $this->css_data, $constants, PREG_SET_ORDER);
$parsed_css = $this->css_data;
foreach($constants as $constant) {
$const = $constant[1];
$value = $constant[2];
if($const[2] !== '{') {
switch($const) {
case '@url-base' :
$value = trim($value, ';"\'');
preg_match_all("/url\(['|\"](.*)['|\"]\)/i", $parsed_css, $matches);
$values = $matches[1];
foreach($values as $_value) {
$url = (preg_match("/(http(s?):\/\/|ftp:\/\/{1})/i", $_value)) ? $_value : $value . $_value;
$parsed_css = preg_replace("/(" . str_ireplace('/', '\/', $_value) . ")/", $url, $parsed_css);
}
break;
}
}
}
return $parsed_css;
}
protected function compile() {
$parsed_css = $this->parse();
$css_output = '';
foreach($parsed_css as $css_selector => $css_rules) {
if($css_selector !== '@variables') {
$css_output .= "{$css_selector} {\n";
foreach($css_rules as $offset => $rule) {
$variables = array();
foreach($parsed_css['@variables'] as $_variables) {
foreach($_variables as $key => $value) {
$variables[$key] = $value;
}
}
foreach($rule as $property => $value) {
preg_match('/var\((.*)\)/', $value, $match);
$css_output .= "\t{$property}: ";
$css_output .= preg_replace('/var\(' . $match[1] . '\)/', $variables[$match[1]], $value);
$css_output .= ";\n";
}
}
$css_output .= "}\n\n";
}
}
return $css_output;
}
public function getCompiled() {
return $this->compile();
}
public function getCompressed() {
$css_output = $this->compile();
$css_output = str_replace(array("\r\n", "\r", "\n", "\t", ' '), ' ', $css_output);
return $css_output;
}
public function __tostring() {
$this->css_data = $this->parseReserved();
$this->css_data = $this->getCompiled();
return $this->css_data;
}
}
$stylesheet = new Stylesheet('
@url-base "../";
@variables {
foo: bold 12px arial, helvetica, sans-serif;
bar: red;
baz: yellow;
}
body {
font: var(foo);
color: var(bar);
}
');
echo '<pre>';
//echo $stylesheet->parseReserved();
echo $stylesheet;
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment