| 1 |
<?php |
| 2 |
/** |
| 3 |
* SCSSPHP |
| 4 |
* |
| 5 |
* @copyright 2012-2015 Leaf Corcoran |
| 6 |
* |
| 7 |
* @license http://opensource.org/licenses/MIT MIT |
| 8 |
* |
| 9 |
* @link http://leafo.github.io/scssphp |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Leafo\ScssPhp; |
| 13 |
|
| 14 |
/** |
| 15 |
* SCSS base formatter |
| 16 |
* |
| 17 |
* @author Leaf Corcoran <leafot@gmail.com> |
| 18 |
*/ |
| 19 |
abstract class Formatter |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var integer |
| 23 |
*/ |
| 24 |
public $indentLevel; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $indentChar; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $break; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $open; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public $close; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $tagSeparator; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public $assignSeparator; |
| 55 |
|
| 56 |
abstract public function __construct(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Return indentation (whitespace) |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
protected function indentStr() |
| 64 |
{ |
| 65 |
return ''; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Return property assignment |
| 70 |
* |
| 71 |
* @param string $name |
| 72 |
* @param mixed $value |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function property($name, $value) |
| 77 |
{ |
| 78 |
return rtrim($name) . $this->assignSeparator . $value . ';'; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Strip semi-colon appended by property(); it's a separator, not a terminator |
| 83 |
* |
| 84 |
* @param array $lines |
| 85 |
*/ |
| 86 |
public function stripSemicolon(&$lines) |
| 87 |
{ |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Output lines inside a block |
| 92 |
* |
| 93 |
* @param \stdClass $block |
| 94 |
*/ |
| 95 |
protected function blockLines($block) |
| 96 |
{ |
| 97 |
$inner = $this->indentStr(); |
| 98 |
|
| 99 |
$glue = $this->break . $inner; |
| 100 |
|
| 101 |
echo $inner . implode($glue, $block->lines); |
| 102 |
|
| 103 |
if (! empty($block->children)) { |
| 104 |
echo $this->break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Output block selectors |
| 110 |
* |
| 111 |
* @param \stdClass $block |
| 112 |
*/ |
| 113 |
protected function blockSelectors($block) |
| 114 |
{ |
| 115 |
$inner = $this->indentStr(); |
| 116 |
|
| 117 |
echo $inner |
| 118 |
. implode($this->tagSeparator, $block->selectors) |
| 119 |
. $this->open . $this->break; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Output block children |
| 124 |
* |
| 125 |
* @param \stdClass $block |
| 126 |
*/ |
| 127 |
protected function blockChildren($block) |
| 128 |
{ |
| 129 |
foreach ($block->children as $child) { |
| 130 |
$this->block($child); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Output non-empty block |
| 136 |
* |
| 137 |
* @param \stdClass $block |
| 138 |
*/ |
| 139 |
protected function block($block) |
| 140 |
{ |
| 141 |
if (empty($block->lines) && empty($block->children)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$pre = $this->indentStr(); |
| 146 |
|
| 147 |
if (! empty($block->selectors)) { |
| 148 |
$this->blockSelectors($block); |
| 149 |
|
| 150 |
$this->indentLevel++; |
| 151 |
} |
| 152 |
|
| 153 |
if (! empty($block->lines)) { |
| 154 |
$this->blockLines($block); |
| 155 |
} |
| 156 |
|
| 157 |
if (! empty($block->children)) { |
| 158 |
$this->blockChildren($block); |
| 159 |
} |
| 160 |
|
| 161 |
if (! empty($block->selectors)) { |
| 162 |
$this->indentLevel--; |
| 163 |
|
| 164 |
if (empty($block->children)) { |
| 165 |
echo $this->break; |
| 166 |
} |
| 167 |
|
| 168 |
echo $pre . $this->close . $this->break; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Entry point to formatting a block |
| 174 |
* |
| 175 |
* @param \stdClass $block An abstract syntax tree |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function format($block) |
| 180 |
{ |
| 181 |
ob_start(); |
| 182 |
|
| 183 |
$this->block($block); |
| 184 |
|
| 185 |
$out = ob_get_clean(); |
| 186 |
|
| 187 |
return $out; |
| 188 |
} |
| 189 |
} |
| 190 |
|