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
154 lines (128 loc) · 2.8 KB
/
Formatter.php
File metadata and controls
154 lines (128 loc) · 2.8 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* SCSSPHP
*
* @copyright 2012-2015 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
{
/**
* @var integer
*/
public $indentLevel;
/**
* @var string
*/
public $indentChar;
/**
* @var string
*/
public $break;
/**
* @var string
*/
public $open;
/**
* @var string
*/
public $close;
/**
* @var string
*/
public $tagSeparator;
/**
* @var string
*/
public $assignSeparator;
/**
* Return indentation (whitespace)
*
* @param integer $n
* @return string
*/
protected function indentStr($n = 0)
{
return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
}
/**
* Return property assignment
*
* @param string $name
* @param mixed $value
* @return string
*/
public function property($name, $value)
{
return $name . $this->assignSeparator . $value . ';';
}
/**
* Output lines inside a block
*
* @param string $inner
* @param \stdClass $block
*/
protected function blockLines($inner, $block)
{
$glue = $this->break.$inner;
echo $inner . implode($glue, $block->lines);
if (! empty($block->children)) {
echo $this->break;
}
}
/**
* Output non-empty block
*
* @param \stdClass $block
*/
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;
}
}
/**
* Entry point to formatting a block
*
* @param \stdClass $block An abstract syntax tree
* @return string
*/
public function format($block)
{
ob_start();
$this->block($block);
$out = ob_get_clean();
return $out;
}
}