Skip to content

Instantly share code, notes, and snippets.

@lackneets
Last active May 18, 2016 04:43
Show Gist options
  • Save lackneets/42874e9f407bd80769edf0d7206ca6f4 to your computer and use it in GitHub Desktop.
Save lackneets/42874e9f407bd80769edf0d7206ca6f4 to your computer and use it in GitHub Desktop.
lessc php adapter
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^(.*\.less)$ %{ENV:BASE}/$1.css [QSA,L]
RewriteRule ^(.*\.less)\.css$ %{ENV:BASE}/lessc.php [QSA,L]
</IfModule>
<?php
/*
Less Auto Compiler
Author: Lackneets
Dependency: "npm install less -g"
Files: .htaccess and lessc.php
*/
ini_set('display_errors', true);
$filename = preg_replace('/\.less(\.css)*$/i', '.less', $_SERVER['DOCUMENT_ROOT'] . preg_replace('/\?.+$/', '', $_SERVER['REQUEST_URI']));
if($filename == __FILE__){
header("HTTP/1.0 404 Not Found");
echo "{$_SERVER['REQUEST_URI']} Not Found";
}else if(file_exists($filename)){
header("Content-Type: text/css");
$start = microtime(true);
$result = shell_exec('/usr/bin/lessc --no-color --no-ie-compat "'.$filename.'" 2>&1');
if(strpos($result, 'Error:') > 0){
echo " body{ padding: 0; margin: 0; }
body:before{
content: \"".escapeResult($result)."\";
position:relative;
z-index: 100000;
font-size: 15px;
display: block;
padding: 10px;
white-space: pre;
background: #FFE4E1;
color: #333;
}";
}else{
echo "/* Compiled in " . round(microtime(true)-$start, 3)*1000 . "ms */\n";
echo $result;
}
}else{
header("HTTP/1.0 404 Not Found");
echo "{$_SERVER['REQUEST_URI']} Not Found";
}
function escapeResult($string){
$string = preg_replace('/[\n\r]/', '\A ', $string);
$string = preg_replace('/"/', '\\\"', $string);
$string = preg_replace('/\'/', '\\\'', $string);
return preg_replace('/\033\[[0-9;]*m/', '', $string);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment