diff --git a/README.md b/README.md index d39c3f8c..4b47bf8d 100644 --- a/README.md +++ b/README.md @@ -94,3 +94,9 @@ output run this: For more help, run `plessc --help` +### This fork ([nodge/lessphp][5]) + +The only change in this fork is the ability to change relative paths according to path to root .less file. (Fixes [#119][6]). + + [5]: https://github.com/Nodge/lessphp/ + [6]: https://github.com/leafo/lessphp/issues/119 diff --git a/composer.json b/composer.json index 0f06ba09..4073c622 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "leafo/lessphp", + "name": "nodge/lessphp", "type": "library", "description": "lessphp is a compiler for LESS written in PHP.", "homepage": "http://leafo.net/lessphp/", diff --git a/lessc.inc.php b/lessc.inc.php index 6ad6c649..37bbc7fb 100644 --- a/lessc.inc.php +++ b/lessc.inc.php @@ -54,6 +54,9 @@ class lessc { public $importDisabled = false; public $importDir = ''; + public $allowUrlRewrite = false; // rewrite urls relative to imported files + public $baseUrlPath = null; + protected $numberPrecision = null; protected $allParsedFiles = array(); @@ -118,7 +121,6 @@ protected function tryImport($importPath, $parentBlock, $out) { $this->addParsedFile($realPath); $parser = $this->makeParser($realPath); $root = $parser->parse(file_get_contents($realPath)); - // set the parents of all the block props foreach ($root->props as $prop) { if ($prop[0] == "block") { @@ -807,7 +809,6 @@ protected function compileProp($prop, $block, $out) { } } - /** * Compiles a primitive value into a CSS property value. * @@ -819,12 +820,16 @@ protected function compileProp($prop, $block, $out) { * The input is expected to be reduced. This function will not work on * things like expressions and variables. */ - protected function compileValue($value) { + protected function compileValue($value, $inUrl = false) { switch ($value[0]) { case 'list': // [1] - delimiter // [2] - array of values - return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); + $values = array(); + foreach ($value[2] as $item) { + $values[] = $this->compileValue($item, $inUrl); + } + return implode($value[1], $values); case 'raw_color': if (!empty($this->formatter->compressColors)) { return $this->compileValue($this->coerceColor($value)); @@ -846,10 +851,14 @@ protected function compileValue($value) { list(, $delim, $content) = $value; foreach ($content as &$part) { if (is_array($part)) { - $part = $this->compileValue($part); + $part = $this->compileValue($part, false); } } - return $delim . implode($content) . $delim; + $content = implode($content); + if ($inUrl && $this->allowUrlRewrite) { + $content = $this->rewriteUrls($content); + } + return $delim . $content . $delim; case 'color': // [1] - red component (either number or a %) // [2] - green component @@ -877,12 +886,47 @@ protected function compileValue($value) { case 'function': list(, $name, $args) = $value; - return $name.'('.$this->compileValue($args).')'; + return $name.'('.$this->compileValue($args, $name === 'url').')'; default: // assumed to be unit $this->throwError("unknown value type: $value[0]"); } } + /** + * Change relative paths according to path to root .less file. + */ + protected function rewriteUrls($url) { + $baseImportDir = realpath(isset($this->baseUrlPath) ? $this->baseUrlPath : end($this->importDir)); + $lastImportDir = realpath(reset($this->importDir)); + + if ($baseImportDir === $lastImportDir) + return $url; + + $arguments = null; + if(strpos($url,'?') !== false) + list($url, $arguments) = explode('?', $url); + + $urlPath = realpath($lastImportDir.DIRECTORY_SEPARATOR.$url); + if ($urlPath === false) + return $arguments ? $url.'?'.$arguments : $url; + + $baseArray = explode(DIRECTORY_SEPARATOR, $baseImportDir); + $urlArray = explode(DIRECTORY_SEPARATOR, $urlPath); + + $i = 0; + foreach ($baseArray as $i => $segment) { + if (!isset($baseArray[$i], $urlArray[$i]) || $baseArray[$i] !== $urlArray[$i]) + break; + else + $i++; // if the above condition is not satisfied, `i` must be equal to count($baseArray) + } + + $newUrl = str_repeat('../', count($baseArray) - $i); + $newUrl .= implode('/', array_slice($urlArray, $i)); + + return $arguments ? $newUrl.'?'.$arguments : $newUrl; + } + protected function lib_pow($args) { list($base, $exp) = $this->assertArgs($args, 2, "pow"); return pow($this->assertNumber($base), $this->assertNumber($exp)); @@ -2047,6 +2091,10 @@ public function addImportDir($dir) { $this->importDir[] = $dir; } + public function setAllowUrlRewrite($allowUrlRewrite){ + $this->allowUrlRewrite = (bool)$allowUrlRewrite; + } + public function allParsedFiles() { return $this->allParsedFiles; }