Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
883ef70
Added ability to rewrite url relative to imported files (enabled by d…
Nodge Aug 17, 2012
7b55905
Added ability to change relative paths according to path to root .les…
Nodge Aug 17, 2012
bb34b9e
Use parsed urls in compileValue() instead of re-parsing. (#119)
Nodge Aug 17, 2012
020d220
Calculate the first difference of the paths instead of using array_di…
Nodge Aug 18, 2012
d3161a0
Fixed bugs in rewriteUrls(). (#119)
Nodge Aug 18, 2012
14e54c3
Merge branch 'master' of https://github.com/Nodge/lessphp
Nodge Sep 21, 2012
c225b96
Merge branch 'master' of https://github.com/leafo/lessphp
Nodge Sep 21, 2012
c360c6f
Update README.md
Nodge Nov 7, 2012
6d99f34
Improving the ability to change relative paths according to path to root
neilime Jan 24, 2013
ad20a69
Merge pull request #1 from neilime/master
Nodge Jan 27, 2013
ad26c2c
Improving the ability to change relative paths according to path to r…
Nodge Jan 27, 2013
de9e44b
Merge branch 'master' of https://github.com/Nodge/lessphp
Nodge Jan 27, 2013
df7c34c
Set allowUrlRewrite default to false to be consistent with the
neilime Feb 22, 2013
d143490
Merge pull request #2 from neilime/url-rewrite
Nodge Feb 22, 2013
06c64b9
Merge branch 'master' of https://github.com/leafo/lessphp.git
neilime Feb 25, 2013
b44e1cc
Merge pull request #3 from neilime/master
Nodge Feb 25, 2013
7377e20
Merge branch 'master' of https://github.com/leafo/lessphp
Nodge Mar 11, 2013
e1d0b11
Merge branch 'master' of https://github.com/leafo/lessphp
Nodge Nov 20, 2013
c191e86
Update composer.json
Nodge Nov 21, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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/",
Expand Down
62 changes: 55 additions & 7 deletions lessc.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -807,7 +809,6 @@ protected function compileProp($prop, $block, $out) {
}
}


/**
* Compiles a primitive value into a CSS property value.
*
Expand All @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down