diff --git a/README.md b/README.md index fea13d2..b0f366a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,22 @@ Will output: When any CSS or JS file is updated or removed the file will be generated with a new hash and filename. +Path Replacement in CSS +================ + +Adding array pairs as extra parameters to the CSS compressor will perform a search/replace with those strings. Useful for paths that break once files are inside the cache folder. + + {{ craft.compressor.css( + [ + '/style/style.css', + '/style/media-queries.css' + ], + ["fonts/", "../fonts/"], + ["images/", "../images/"] + ) }} + + + Setup ================ diff --git a/services/CompressorService.php b/services/CompressorService.php index b372ccd..c729850 100755 --- a/services/CompressorService.php +++ b/services/CompressorService.php @@ -110,6 +110,7 @@ public function css($css) } else if ($recache['recache'] === true) { + $cached_file = $this->makeCachePath("cached." . $recache['md5'] . ".css"); $css_content = ""; @@ -120,6 +121,15 @@ public function css($css) $css_content .= file_get_contents($file); } + // Get find/replace pairs + $searchReplacePairs = func_get_args(); + array_shift($searchReplacePairs); + + foreach ($searchReplacePairs as $searchReplace) { + if (!is_array($searchReplace)) { continue; } + $css_content = str_replace($searchReplace[0], $searchReplace[1], $css_content); + } + $css = trim($css_content); $css = str_replace("\r\n", "\n", $css); $search = array("/\/\*[^!][\d\D]*?\*\/|\t+/", "/\s+/", "/\}\s+/"); diff --git a/variables/CompressorVariable.php b/variables/CompressorVariable.php old mode 100644 new mode 100755 index 56ed5f1..a7c310d --- a/variables/CompressorVariable.php +++ b/variables/CompressorVariable.php @@ -16,7 +16,7 @@ class CompressorVariable { public function css($files) { - return craft()->compressor->css($files); + return call_user_func_array([craft()->compressor, "css"], func_get_args()); } public function js($files)