| 1 |
<?php |
| 2 |
/** |
| 3 |
* SCSSPHP |
| 4 |
* |
| 5 |
* @copyright 2012-2014 Leaf Corcoran |
| 6 |
* |
| 7 |
* @license http://opensource.org/licenses/gpl-license GPL-3.0 |
| 8 |
* @license http://opensource.org/licenses/MIT MIT |
| 9 |
* |
| 10 |
* @link http://leafo.net/scssphp |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Leafo\ScssPhp; |
| 14 |
|
| 15 |
/** |
| 16 |
* SCSS base formatter |
| 17 |
* |
| 18 |
* @author Leaf Corcoran <leafot@gmail.com> |
| 19 |
*/ |
| 20 |
abstract class Formatter |
| 21 |
{ |
| 22 |
public $indentLevel; |
| 23 |
public $indentChar; |
| 24 |
public $break; |
| 25 |
public $open; |
| 26 |
public $close; |
| 27 |
public $tagSeparator; |
| 28 |
public $assignSeparator; |
| 29 |
|
| 30 |
protected function indentStr($n = 0) |
| 31 |
{ |
| 32 |
return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
| 33 |
} |
| 34 |
|
| 35 |
public function property($name, $value) |
| 36 |
{ |
| 37 |
return $name . $this->assignSeparator . $value . ';'; |
| 38 |
} |
| 39 |
|
| 40 |
protected function blockLines($inner, $block) |
| 41 |
{ |
| 42 |
$glue = $this->break.$inner; |
| 43 |
echo $inner . implode($glue, $block->lines); |
| 44 |
|
| 45 |
if (!empty($block->children)) { |
| 46 |
echo $this->break; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
protected function block($block) |
| 51 |
{ |
| 52 |
if (empty($block->lines) && empty($block->children)) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$inner = $pre = $this->indentStr(); |
| 57 |
|
| 58 |
if (!empty($block->selectors)) { |
| 59 |
echo $pre . |
| 60 |
implode($this->tagSeparator, $block->selectors) . |
| 61 |
$this->open . $this->break; |
| 62 |
$this->indentLevel++; |
| 63 |
$inner = $this->indentStr(); |
| 64 |
} |
| 65 |
|
| 66 |
if (!empty($block->lines)) { |
| 67 |
$this->blockLines($inner, $block); |
| 68 |
} |
| 69 |
|
| 70 |
foreach ($block->children as $child) { |
| 71 |
$this->block($child); |
| 72 |
} |
| 73 |
|
| 74 |
if (!empty($block->selectors)) { |
| 75 |
$this->indentLevel--; |
| 76 |
|
| 77 |
if (empty($block->children)) { |
| 78 |
echo $this->break; |
| 79 |
} |
| 80 |
|
| 81 |
echo $pre . $this->close . $this->break; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function format($block) |
| 86 |
{ |
| 87 |
ob_start(); |
| 88 |
$this->block($block); |
| 89 |
$out = ob_get_clean(); |
| 90 |
|
| 91 |
return $out; |
| 92 |
} |
| 93 |
} |
| 94 |
|