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