forked from nizsheanez/yii2-asset-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scss.php
170 lines (152 loc) · 5.49 KB
/
Scss.php
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace nizsheanez\assetConverter;
use Yii;
/**
* Class Scss
* @package nizsheanez\assetConverter
* @author Andrey Izman <[email protected]>
*/
class Scss extends Parser
{
/**
* @var bool compass support
*/
public $enableCompass = true;
/**
* @var array paths to import files
*/
public $importPaths = [];
/**
* @var bool if is true, compiler will place line numbers in your compiled output
*/
public $lineComments = false;
/**
* @var string output style
*/
public $outputStyle = 'nested';
/**
* @var array defined formatters
*/
protected $formatters = [
'compressed', 'crunched', 'expanded', 'nested',
];
/**
* Parse a Scss file and convert it into CSS
*
* @param string $src source file path
* @param string $dst destination file path
* @param array $options parser options
* @return mixed
* @throws \Exception
*/
public function parse($src, $dst, $options)
{
$this->importPaths = !empty($options['importPaths']) ? $options['importPaths'] : $this->importPaths;
$this->enableCompass = isset($options['enableCompass']) ? $options['enableCompass'] : $this->enableCompass;
$this->lineComments = isset($options['lineComments']) ? $options['lineComments'] : $this->lineComments;
$this->outputStyle = isset($options['outputStyle']) ? $options['outputStyle'] : $this->outputStyle;
$this->outputStyle = strtolower($this->outputStyle);
$parser = new \Leafo\ScssPhp\Compiler();
if (!empty($this->importPaths) && is_array($this->importPaths)) {
$paths = [''];
foreach ($this->importPaths as $path) {
$paths[] = Yii::getAlias($path);
}
$parser->setImportPaths($paths);
}
if (in_array($this->outputStyle, $this->formatters)) {
if ($this->lineComments && in_array($this->outputStyle, ['compressed', 'crunched'])) {
$this->lineComments = false;
}
$parser->setFormatter('Leafo\\ScssPhp\\Formatter\\' . ucfirst($this->outputStyle));
}
if ($this->enableCompass) {
new \scss_compass($parser);
}
if (!file_exists($src)) {
throw new \Exception("Failed to open file \"$src\"");
}
if ($this->lineComments) {
$content = self::insertLineComments($src, self::getRelativeFilename($src, $dst));
file_put_contents($dst, self::parseLineComments($parser->compile($content, $src)));
} else {
file_put_contents($dst, $parser->compile(file_get_contents($src), $src));
}
}
/**
* Returns the relative filename of $src to $dst
*
* @param string $src
* @param string $dst
* @return string
*/
protected static function getRelativeFilename($src, $dst) {
$src = explode('/', $src);
$dst = explode('/', $dst);
for ($index=1, $count=count($src); $index<$count; $index++) {
if (!isset($src[$index]) || !isset($dst[$index]) || $src[$index] !== $dst[$index]) {
break;
}
}
return str_repeat('../', count($dst)-$index-1) . implode('/', array_slice($src, $index));
}
/**
* Inserts the line numbers into comments
*
* @param string $src
* @param string $filename
* @return string
*/
protected static function insertLineComments($src, $filename)
{
$lines = explode("\n", preg_replace_callback('~/\*.+?\*/~sx',
function($m) {return str_repeat("\n", substr_count($m[0], "\n"));}, file_get_contents($src)));
foreach ($lines as $no => $line) {
$no++;
$newLine = '';
for ($pos = 0; ($pos = mb_strpos($line, '{')) !== false;) {
$comment = "/* line: $no, $filename */";
$newLine .= mb_substr($line, 0, $pos+1) . $comment;
$line = mb_substr($line, $pos+1);
}
if (!empty($newLine)) {
$lines[$no-1] = $newLine . $line;
}
}
return implode("\n", $lines);
}
/**
* Parses the line numbers comments and moves them into the header of the block
*
* @param string $content
* @return string
*/
protected static function parseLineComments($content)
{
$content = preg_replace('~(/\*.+?\*/)([\x0b-\x20]+)~sx', "\\1\n\\2", $content);
$lines = explode("\n", $content);
unset($content);
$begin = 0;
foreach ($lines as $id => $line) {
if (empty($line)) {
if ($id > 0 && $id === $begin+1) {
$begin = $id;
}
} elseif (strpos($line, '}') !== false) {
$begin = $id;
} elseif (strpos($line, '{') !== false) {
$begin = $id > 0 ? $id-1 : 0;
} elseif (strpos($line, '/*') !== false) {
unset($lines[$id]);
if ($begin > 0) {
for (; !isset($lines[$begin]); $begin--);
for ($next=$begin+1; !isset($lines[$next]); $next++);
$lines[$begin] .= "\n" . preg_replace('~^(\s+)*.+$~', '\1', $lines[$next]) . ltrim($line);
} else {
$lines[$begin] = preg_replace('~^(\s+)*.+$~', '\1', $lines[$begin]) . ltrim($line) . "\n" . $lines[$begin];
}
}
}
return implode("\n", $lines);
}
}