Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created August 31, 2011 19:31
Show Gist options
  • Select an option

  • Save nathggns/1184470 to your computer and use it in GitHub Desktop.

Select an option

Save nathggns/1184470 to your computer and use it in GitHub Desktop.
My kickass less apache handler
<?php
require_once("lessc.php");
$file = $_SERVER["PATH_TRANSLATED"];
$array = explode('.', $file);
$nfile = $array[0] . '.less';
$name = $array[0];
header("Content-type: text/css");
if (file_exists($nfile))
{
$code = file_get_contents($nfile);
$lc = new lessc();
try {
$code = preg_replace_callback("/border-radius: ([^;]*)/", function($matches) {
$r = "-webkit-border-radius: ";
$r .= $matches[1];
$r .= ";\n-moz-border-radius: ";
$r .= $matches[1];
$r .= ";\n-khtml-border-radius: ";
$r .= $matches[1];
$r .= ";\nborder-radius: ";
$r .= $matches[1];
$r .= ";";
return $r;
}, $code);
$code = preg_replace_callback("/box-shadow: ([^;]*)/", function($matches) {
$r = "-webkit-box-shadow: ";
$r .= $matches[1];
$r .= ";\n-moz-box-shadow: ";
$r .= $matches[1];
$r .= ";\nbox-shadow: ";
$r .= $matches[1];
$r .= ";";
return $r;
}, $code);
$compiled = $code;
$compiled = preg_replace_callback('/background-size: ?([^;]*)/', function($matches) {
$string = "-moz-background-size: $matches[1];\n";
$string .= "-webkit-background-size: $matches[1];\n";
return $string . "background-size: $matches[1]";
}, $compiled);
$compiled = preg_replace_callback('/background: ?linear-gradient\(([^,]*?), ?([^,]*?), ?([^,]*?)\)/', function($matches) {
$r = "background: $matches[2];\n";
$r .= "background: -webkit-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -moz-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -ms-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -o-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: linear-gradient($matches[1], $matches[2], $matches[3])";
return $r;
}, $compiled);
$code = $compiled;
$compiled = $lc->parse($code);
$compiled = preg_replace_callback('/background: ?linear-gradient\(([^,]*), ?([^,]*?), ?([^,]*?), ?([^,]*?)\)/', function($matches) {
$r = "background: -webkit-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -moz-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -ms-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: -o-linear-gradient($matches[1], $matches[2], $matches[3]);\n";
$r .= "background: linear-gradient($matches[1], $matches[2], $matches[3])";
if ($matches[2][0] == '#') $matches[2] = substr($matches[2], 1);
if ($matches[3][0] == '#') $matches[3] = substr($matches[3], 1);
$d = 'background: url(';
$d .= file_get_contents("http://localhost/gradient.php?direction=$matches[1]&height=$matches[4]&stop1=$matches[2]&stop2=$matches[3];");
$d .= ") repeat-x;\n";
$r = $d . $r;
return $r;
}, $compiled);
$compiled = preg_replace('/([0-9]+?) ([px|%|em|pt])/', '\1\2', $compiled);
file_put_contents($name . '.bak', file_get_contents($file));
file_put_contents($file, $compiled);
echo $compiled;
} catch (exception $ex) {
echo '/* Problem with LESSCSS, reverting to normal css - ' . $ex . ' */';
echo file_get_contents($file);
}
} else {
echo file_get_contents($file);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment