From 30f686eb67205a1da8765c992e2f9ee1a158c712 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Tue, 5 Nov 2013 20:15:00 +0000 Subject: add DOKU_INC to less import directories --- lib/exe/css.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/exe') diff --git a/lib/exe/css.php b/lib/exe/css.php index f0bd24b43..bc0645400 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) { -- cgit v1.2.3 From de737055c55e54246de5000d601a089328c1af1c Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Tue, 5 Nov 2013 20:15:39 +0000 Subject: update url/file rewriting in css_loadfile() to support @import of less files --- lib/exe/css.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'lib/exe') diff --git a/lib/exe/css.php b/lib/exe/css.php index bc0645400..3929b2d4b 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -403,12 +403,38 @@ function css_loadfile($file,$location=''){ $css = io_readFile($file); if(!$location) return $css; - $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); - $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); + global $css_location; + global $css_current_dir; + $css_current_dir = preg_replace('#^('.DOKU_INC.(defined('DOKU_UNITTEST')?'|'.realpath(TMP_DIR) : '').')#','',dirname($file)).'/'; + $css_location = $location; + + $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#','css_loadfile_callback',$css); + $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#','css_loadfile_callback',$css); +# $css = preg_replace_callback('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); +# $css = preg_replace_callback('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); return $css; } +function css_loadfile_callback($match){ + global $css_location; + global $css_current_dir; + + if (preg_match('#^(/|data:|https?://)#',$match[3])) { + return $match[0]; + } + else if (substr($match[3],-5) == '.less') { + if ($match[3]{0} != '/') { + $match[3] = $css_current_dir . $match[3]; + } + } + else { + $match[3] = $css_location . $match[3]; + } + + return join('',array_slice($match,1)); +} + /** * Converte local image URLs to data URLs if the filesize is small * -- cgit v1.2.3 From 4eb5f931edaaabdd436f4c2802d0d293f8ef76cd Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 11 Nov 2013 22:03:58 +0000 Subject: fix sp. in comment --- lib/exe/css.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/exe') diff --git a/lib/exe/css.php b/lib/exe/css.php index 3929b2d4b..948251440 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -436,7 +436,7 @@ function css_loadfile_callback($match){ } /** - * 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 */ -- cgit v1.2.3 From 12ffbbc324be1f06ccfcff580f512990dca929b8 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 11 Nov 2013 22:31:28 +0000 Subject: refactor to improve elegance --- lib/exe/css.php | 75 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 25 deletions(-) (limited to 'lib/exe') diff --git a/lib/exe/css.php b/lib/exe/css.php index 948251440..9cde09545 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -399,40 +399,65 @@ 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); +} - global $css_location; - global $css_current_dir; - $css_current_dir = preg_replace('#^('.DOKU_INC.(defined('DOKU_UNITTEST')?'|'.realpath(TMP_DIR) : '').')#','',dirname($file)).'/'; - $css_location = $location; +class DokuCssFile { - $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#','css_loadfile_callback',$css); - $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#','css_loadfile_callback',$css); -# $css = preg_replace_callback('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); -# $css = preg_replace_callback('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); + protected $filepath; + protected $location; + private $relative_path = null; - return $css; -} + public function __construct($file) { + $this->filepath = $file; + } + + public function load($location='') { + if (!@file_exists($this->filepath)) return ''; -function css_loadfile_callback($match){ - global $css_location; - global $css_current_dir; + $css = io_readFile($this->filepath); + if (!$location) return $css; - if (preg_match('#^(/|data:|https?://)#',$match[3])) { - return $match[0]; + $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; } - else if (substr($match[3],-5) == '.less') { - if ($match[3]{0} != '/') { - $match[3] = $css_current_dir . $match[3]; + + 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; } - else { - $match[3] = $css_location . $match[3]; - } - return join('',array_slice($match,1)); + 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)); + } } /** -- cgit v1.2.3