|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LesserPhp\Formatter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* lesserphp |
|
7
|
|
|
* https://www.maswaba.de/lesserphp |
|
8
|
|
|
* |
|
9
|
|
|
* LESS CSS compiler, adapted from http://lesscss.org |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright 2013, Leaf Corcoran <[email protected]> |
|
12
|
|
|
* Copyright 2016, Marcus Schwarz <[email protected]> |
|
13
|
|
|
* Licensed under MIT or GPLv3, see LICENSE |
|
14
|
|
|
* @package LesserPhp |
|
15
|
|
|
*/ |
|
16
|
|
|
class Classic implements FormatterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $indentChar = ' '; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $break = "\n"; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $open = ' {'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $close = '}'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $selectorSeparator = ', '; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $assignSeparator = ':'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $openSingle = ' { '; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $closeSingle = ' }'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var bool |
|
61
|
|
|
*/ |
|
62
|
|
|
private $disableSingle = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private $breakSelectors = false; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
private $compressColors = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var int |
|
76
|
|
|
*/ |
|
77
|
|
|
private $indentLevel = 0; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param int $n |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
38 |
|
public function indentStr($n = 0) |
|
85
|
|
|
{ |
|
86
|
38 |
|
return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $name |
|
91
|
|
|
* @param string $value |
|
92
|
|
|
* |
|
93
|
|
|
* @return string string |
|
94
|
|
|
*/ |
|
95
|
37 |
|
public function property($name, $value) |
|
96
|
|
|
{ |
|
97
|
37 |
|
return $name . $this->assignSeparator . $value . ';'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param $block |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
38 |
|
protected function isEmpty($block) |
|
106
|
|
|
{ |
|
107
|
38 |
|
if (empty($block->lines)) { |
|
108
|
34 |
|
foreach ($block->children as $child) { |
|
109
|
33 |
|
if (!$this->isEmpty($child)) { |
|
110
|
33 |
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
10 |
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
38 |
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $block |
|
122
|
|
|
* |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
38 |
|
public function block($block) |
|
126
|
|
|
{ |
|
127
|
38 |
|
if ($this->isEmpty($block)) { |
|
128
|
10 |
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
38 |
|
$inner = $pre = $this->indentStr(); |
|
132
|
|
|
|
|
133
|
38 |
|
$isSingle = !$this->disableSingle && |
|
134
|
38 |
|
$block->type === null && count($block->lines) === 1; |
|
135
|
|
|
|
|
136
|
38 |
|
if (!empty($block->selectors)) { |
|
137
|
35 |
|
$this->indentLevel++; |
|
138
|
|
|
|
|
139
|
35 |
|
if ($this->breakSelectors) { |
|
140
|
35 |
|
$selectorSeparator = $this->selectorSeparator . $this->break . $pre; |
|
141
|
|
|
} else { |
|
142
|
1 |
|
$selectorSeparator = $this->selectorSeparator; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
35 |
|
echo $pre . implode($selectorSeparator, $block->selectors); |
|
146
|
35 |
|
if ($isSingle) { |
|
147
|
1 |
|
echo $this->openSingle; |
|
148
|
1 |
|
$inner = ''; |
|
149
|
|
|
} else { |
|
150
|
35 |
|
echo $this->open . $this->break; |
|
151
|
35 |
|
$inner = $this->indentStr(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
38 |
|
if (!empty($block->lines)) { |
|
157
|
38 |
|
$glue = $this->break . $inner; |
|
158
|
38 |
|
echo $inner . implode($glue, $block->lines); |
|
159
|
38 |
|
if (!$isSingle && !empty($block->children)) { |
|
160
|
6 |
|
echo $this->break; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
38 |
|
foreach ($block->children as $child) { |
|
165
|
35 |
|
$this->block($child); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
38 |
|
if (!empty($block->selectors)) { |
|
169
|
35 |
|
if (!$isSingle && empty($block->children)) { |
|
170
|
35 |
|
echo $this->break; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
35 |
|
if ($isSingle) { |
|
174
|
1 |
|
echo $this->closeSingle . $this->break; |
|
175
|
|
|
} else { |
|
176
|
35 |
|
echo $pre . $this->close . $this->break; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
35 |
|
$this->indentLevel--; |
|
180
|
|
|
} |
|
181
|
38 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
3 |
|
public function getSelectorSeparator() |
|
187
|
|
|
{ |
|
188
|
3 |
|
return $this->selectorSeparator; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $separator |
|
193
|
|
|
*/ |
|
194
|
49 |
|
protected function setSelectorSeparator($separator) |
|
195
|
|
|
{ |
|
196
|
49 |
|
$this->selectorSeparator = $separator; |
|
197
|
49 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
12 |
|
public function getCompressColors() |
|
203
|
|
|
{ |
|
204
|
12 |
|
return $this->compressColors; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param bool $compress |
|
209
|
|
|
*/ |
|
210
|
1 |
|
protected function setCompressColors($compress) |
|
211
|
|
|
{ |
|
212
|
1 |
|
$this->compressColors = $compress; |
|
213
|
1 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param bool $breakSelectors |
|
217
|
|
|
*/ |
|
218
|
49 |
|
protected function setBreakSelectors($breakSelectors) |
|
219
|
|
|
{ |
|
220
|
49 |
|
$this->breakSelectors = $breakSelectors; |
|
221
|
49 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param bool $disableSingle |
|
225
|
|
|
*/ |
|
226
|
49 |
|
protected function setDisabledSingle($disableSingle) |
|
227
|
|
|
{ |
|
228
|
49 |
|
$this->disableSingle = $disableSingle; |
|
229
|
49 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $breakChar |
|
233
|
|
|
*/ |
|
234
|
1 |
|
protected function setBreakChar($breakChar) |
|
235
|
|
|
{ |
|
236
|
1 |
|
$this->break = $breakChar; |
|
237
|
1 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string $openChar |
|
241
|
|
|
*/ |
|
242
|
1 |
|
protected function setOpenChar($openChar) |
|
243
|
|
|
{ |
|
244
|
1 |
|
$this->open = $openChar; |
|
245
|
1 |
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $assignOperator |
|
249
|
|
|
*/ |
|
250
|
49 |
|
protected function setAssignOperator($assignOperator) |
|
251
|
|
|
{ |
|
252
|
49 |
|
$this->assignSeparator = $assignOperator; |
|
253
|
49 |
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|