Skip to content

Commit

Permalink
Update less.php
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed Jul 20, 2014
1 parent c6cfb0e commit 4160b7a
Show file tree
Hide file tree
Showing 59 changed files with 612 additions and 308 deletions.
35 changes: 30 additions & 5 deletions Contrib/less.php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,24 @@ This method will check the modified time and size of each less file (including i
Note: When changes are found, this method will return a different file name for the new cached content.

```php
$to_cache = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
Less_Cache::$cache_dir = '/var/www/writable_folder';
$css_file_name = Less_Cache::Get( $to_cache );
$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
$options = array( 'cache_dir' => '/var/www/writable_folder' );
$css_file_name = Less_Cache::Get( $less_files, $options );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
```

#### Caching CSS With Variables
Passing options to Less_Cache::Get()

```php
$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
$options = array( 'cache_dir' => '/var/www/writable_folder' );
$variables = array( 'width' => '100px' );
$css_file_name = Less_Cache::Get( $less_files, $options, $variables );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
```


#### Parser Caching
less.php will save serialized parser data for each .less file if a writable folder is passed to the SetCacheDir() method.
Note: This feature only caches intermediate parsing results to improve the performance of repeated css generation.
Expand All @@ -193,6 +205,19 @@ $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```

You can specify the caching technique used by changing the ```cache_method``` option. Supported methods are:
* ```php```: Creates valid PHP files which can be included without any changes (default method).
* ```var_export```: Like "php", but using PHPs ```var_export()``` function without any optimizations.
It's recommended to use "php" instead.
* ```serialize```: Faster, but pretty memory-intense.
* ```callback```: Use custom callback functions to implement your own caching method. Give the "cache_callback_get" and
"cache_callback_set" options with callables (see PHPs ```call_user_func()``` and ```is_callable()``` functions). less.php
will pass the parser object (class ```Less_Parser```), the path to the parsed .less file ("/some/path/to/file.less") and
an identifier that will change every time the .less file is modified. The ```get``` callback must return the ruleset
(an array with ```Less_Tree``` objects) provided as fourth parameter of the ```set``` callback. If something goes wrong,
return ```NULL``` (cache doesn't exist) or ```FALSE```.



Source Maps
---
Expand Down Expand Up @@ -265,7 +290,7 @@ How to use / install:
2. Find the compiler under Appearance > LESS Compiler in your WordPress dashboard
3. Enter your LESS code in the text area and press (re)compile

Use the built-in compiler to:
Use the built-in compiler to:
- set any [Bootstrap](http://getbootstrap.com/customize/) variable or use Bootstrap's mixins:
-`@navbar-default-color: blue;`
- create a custom button: `.btn-custom {
Expand All @@ -290,7 +315,7 @@ Transitioning from Leafo/lessphp
---
Projects looking for an easy transition from leafo/lessphp can use the lessc.inc.php adapter. To use, [Download the less.php source code](https://github.com/oyejorge/less.php/archive/master.zip) and unzip the files into your project so that the new 'lessc.inc.php' replaces the existing 'lessc.inc.php'.

Note, the 'setPreserveComments', 'registerFunction' and 'unregisterFunction' will no longer have any effect on the compiled less.
Note, the 'setPreserveComments' will no longer have any effect on the compiled less.

Credits
---
Expand Down
69 changes: 36 additions & 33 deletions Contrib/less.php/bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ require_once dirname(__FILE__) . '/../lib/Less/Autoloader.php';
Less_Autoloader::register();

// Create our environment
$env = array('compress' => false);

$silent = false;
$watch = false;
$env = array('compress' => false);
$silent = false;
$watch = false;
$rootpath = '';

// Check for arguments
array_shift($argv);
Expand Down Expand Up @@ -43,7 +43,6 @@ Usage: lessc [options] sources [destination]
-ru, --relative-urls re-write relative urls to the base less file.
-rp, --rootpath=URL Set rootpath for url rewriting in relative imports and urls.
Works with or without the relative-urls option.
--insecure Allow imports from insecure https hosts.
-w, --watch Watch input files for changes.
Expand All @@ -53,44 +52,48 @@ EOD;
case 'silent':
$silent = true;
break;

case 'w':
case 'watch':
$watch = true;
break;

case 'v':
case 'version':
echo "lessc " . Less_Parser::version . " (less.php)\n\n";
echo "lessc " . Less_Version::version . " (less.php)\n\n";
exit;
case 'x':
case 'compress':
Less_Environment::$compress = true;
break;
case 'include-path':
$paths = preg_split('#;|\:#', $value);
$env->paths = $paths;
break;
case 'strict-imports':
$env->strictImports = true;
break;
case 'sm':
case 'strict-math':
$env->strictMath = ($value === 'on');

case 'rp':
case 'rootpath':
$rootpath = $value;
break;
case 'su':
case 'strict-units':
$env->strictUnits = ($value === 'on');


//parser options
case 'compress':
$env['compress'] = true;
break;

case 'ru':
case 'relative-urls':
$env->relativeUrls = true;
$env['relativeUrls'] = true;
break;
case 'rp':
case 'rootpath':
$env->rootpath = $value;

case 'su':
case 'strict-units':
$env['strictUnits'] = ($value === 'on');
break;
case 'insecure':
$env->insecure = true;

case 'sm':
case 'strict-math':
$env['strictMath'] = ($value === 'on');
break;
case 'w':
case 'watch':
$watch = true;

case 'x':
case 'include-path':
$env['import_dirs'] = preg_split('#;|\:#', $value);
break;

}
}
}
Expand Down Expand Up @@ -145,7 +148,7 @@ if ($watch) {
$parser = new Less_Parser($env);
foreach ($inputs as $input) {
try {
$parser->parseFile($input, $env->rootpath);
$parser->parseFile($input, $rootpath);
}
catch (Exception $e) {
echo("lessc: " . $e->getMessage() . " \n");
Expand Down
20 changes: 17 additions & 3 deletions Contrib/less.php/lessc.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class lessc{

public $importDir = '';
protected $allParsedFiles = array();
protected $libFunctions = array();
protected $registeredVars = array();
private $formatterName;

Expand All @@ -40,8 +41,12 @@ public function setFormatter($name)
}

public function setPreserveComments($preserve) {}
public function registerFunction($name, $func) {}
public function unregisterFunction($name) {}
public function registerFunction($name, $func) {
$this->libFunctions[$name] = $func;
}
public function unregisterFunction($name) {
unset($this->libFunctions[$name]);
}

public function setVariables($variables){
foreach( $variables as $name => $value ){
Expand All @@ -60,7 +65,7 @@ public function unsetVariable($name){
public function parse($buffer, $presets = array()){
$options = array();
$this->setVariables($presets);

switch($this->formatterName){
case 'compressed':
$options['compress'] = true;
Expand All @@ -70,6 +75,9 @@ public function parse($buffer, $presets = array()){
$parser = new Less_Parser($options);
$parser->setImportDirs($this->getImportDirs());
if( count( $this->registeredVars ) ) $parser->ModifyVars( $this->registeredVars );
foreach ($this->libFunctions as $name => $func) {
$parser->registerFunction($name, $func);
}
$parser->parse($buffer);

return $parser->getCss();
Expand All @@ -96,6 +104,9 @@ public function compile($string, $name = null){
if( count( $this->registeredVars ) ){
$parser->ModifyVars( $this->registeredVars );
}
foreach ($this->libFunctions as $name => $func) {
$parser->registerFunction($name, $func);
}
$parser->parse($string);
$out = $parser->getCss();

Expand Down Expand Up @@ -127,6 +138,9 @@ public function compileFile($fname, $outFname = null) {
$parser = new Less_Parser();
$parser->SetImportDirs($this->getImportDirs());
if( count( $this->registeredVars ) ) $parser->ModifyVars( $this->registeredVars );
foreach ($this->libFunctions as $name => $func) {
$parser->registerFunction($name, $func);
}
$parser->parseFile($fname);
$out = $parser->getCss();

Expand Down
Loading

0 comments on commit 4160b7a

Please sign in to comment.