summaryrefslogtreecommitdiff
path: root/lib/exe/css.php
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2013-11-11 22:31:28 +0000
committerChristopher Smith <chris@jalakai.co.uk>2013-11-11 22:31:28 +0000
commit12ffbbc324be1f06ccfcff580f512990dca929b8 (patch)
treed52a7feb79e2a02ec48419b4390da3c3c2d45c28 /lib/exe/css.php
parent4eb5f931edaaabdd436f4c2802d0d293f8ef76cd (diff)
downloadrpg-12ffbbc324be1f06ccfcff580f512990dca929b8.tar.gz
rpg-12ffbbc324be1f06ccfcff580f512990dca929b8.tar.bz2
refactor to improve elegance
Diffstat (limited to 'lib/exe/css.php')
-rw-r--r--lib/exe/css.php75
1 files changed, 50 insertions, 25 deletions
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));
+ }
}
/**