summaryrefslogtreecommitdiff
path: root/lib/exe/css.php
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2013-11-17 12:49:34 -0800
committerChristopher Smith <chris@jalakai.co.uk>2013-11-17 12:49:34 -0800
commitad72cb390239ebdbb57f05f2a3c95ad52f6285cd (patch)
tree73ebc31f730f9bbec6446b9e4134af314f760110 /lib/exe/css.php
parentb5a4e4fb55eff9a1c6c300b61e073677f94fd6ec (diff)
parent12ffbbc324be1f06ccfcff580f512990dca929b8 (diff)
downloadrpg-ad72cb390239ebdbb57f05f2a3c95ad52f6285cd.tar.gz
rpg-ad72cb390239ebdbb57f05f2a3c95ad52f6285cd.tar.bz2
Merge pull request #407 from splitbrain/FS#2875
FS#2875 : Allow less to parse @import of .less files
Diffstat (limited to 'lib/exe/css.php')
-rw-r--r--lib/exe/css.php71
1 files changed, 64 insertions, 7 deletions
diff --git a/lib/exe/css.php b/lib/exe/css.php
index af7f9e4f1..02fc6eab7 100644
--- a/lib/exe/css.php
+++ b/lib/exe/css.php
@@ -173,6 +173,12 @@ function css_out(){
*/
function css_parseless($css) {
$less = new lessc();
+ $less->importDir[] = DOKU_INC;
+
+ if (defined('DOKU_UNITTEST')){
+ $less->importDir[] = TMP_DIR;
+ }
+
try {
return $less->compile($css);
} catch(Exception $e) {
@@ -400,18 +406,69 @@ function css_filetypes(){
* given location prefix
*/
function css_loadfile($file,$location=''){
- if(!@file_exists($file)) return '';
- $css = io_readFile($file);
- if(!$location) return $css;
+ $css_file = new DokuCssFile($file);
+ return $css_file->load($location);
+}
- $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css);
- $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css);
+class DokuCssFile {
- return $css;
+ protected $filepath;
+ protected $location;
+ private $relative_path = null;
+
+ public function __construct($file) {
+ $this->filepath = $file;
+ }
+
+ public function load($location='') {
+ if (!@file_exists($this->filepath)) return '';
+
+ $css = io_readFile($this->filepath);
+ if (!$location) return $css;
+
+ $this->location = $location;
+
+ $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
+ $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
+
+ return $css;
+ }
+
+ private function getRelativePath(){
+
+ if (is_null($this->relative_path)) {
+ $basedir = array(DOKU_INC);
+ if (defined('DOKU_UNITTEST')) {
+ $basedir[] = realpath(TMP_DIR);
+ }
+ $regex = '#^('.join('|',$basedir).')#';
+
+ $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
+ }
+
+ return $this->relative_path;
+ }
+
+ public function replacements($match) {
+
+ if (preg_match('#^(/|data:|https?://)#',$match[3])) {
+ return $match[0];
+ }
+ else if (substr($match[3],-5) == '.less') {
+ if ($match[3]{0} != '/') {
+ $match[3] = $this->getRelativePath() . '/' . $match[3];
+ }
+ }
+ else {
+ $match[3] = $this->location . $match[3];
+ }
+
+ return join('',array_slice($match,1));
+ }
}
/**
- * Converte local image URLs to data URLs if the filesize is small
+ * Convert local image URLs to data URLs if the filesize is small
*
* Callback for preg_replace_callback
*/