Repository navigation
Expand file tree
/
Copy pathpcss.class.php
More file actions
165 lines (152 loc) · 4.01 KB
/
Copy pathpcss.class.php
File metadata and controls
165 lines (152 loc) · 4.01 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
155
156
157
158
159
160
161
162
163
164
165
<?php
class pcss {
public $file;
public $options = array(
'spacing' => 1,
'spacing_char' => "\t",
'pcss_includes' => array(),
'sort_properties' => true
);
private $lines = array();
public function __construct($file,$args=array()) {
$this->init($file,$args);
}
public function init($file,$args=array()) {
$this->file = $file;
$this->options = array_merge_recursive($this->options, $args);
$this->setLines();
}
private function getRule($index) {
$str = '';
$v = $this->lines[$index];
while(isset($v['parent'])) {
$v = $this->lines[$v['parent']];
$str = $v['line'] . ' ' . $str;
}
return $str;
}
private function getProperty($index) {
$str = '';
$v = $this->lines[$index];
if(isset($v['first'])) {
$str = $this->getRule($index) . "{\n";
}
$str .= str_repeat($this->options['indent_char'], $this->options['indent']) . $v['line'] . "\n";
if(isset($v['last'])) {
$str .= "}\n";
}
return $str;
}
private function setLines() {
ob_start();
if(is_array($this->options['pcss_includes'])) {
foreach($this->options['pcss_includes'] as $file) {
require $file;
}
}
require $this->file;
$this->lines = explode(PHP_EOL,ob_get_clean());
$this->lines = $this->buildLines();
if($this->options['sort_properties']) {
$this->sortProperties();
}
}
private function buildLines() {
$buckets = array();
$l = array();
$min_bucket = 0;
foreach($this->lines as $num => $line) {
$bucket = strspn($line," \t");
$buckets[$bucket] = true;
$line = trim($line);
$l[$num] = array(
'bucket' => $bucket,
'line' => $line,
'num' => $num,
'type' => substr($line,-1) == ';' ? 'property' : 'rule'
);
if($num == 0 || $bucket < $min_bucket) {
$min_bucket = $bucket;
}
}
$buckets_to_indexes = array_keys($buckets);
sort($buckets_to_indexes);
$buckets_to_indexes = array_flip($buckets_to_indexes);
//so now let's build it out
foreach($l as $num => $v) {
$v['index'] = $buckets_to_indexes[$v['bucket']];
if($num > 0 && $v['bucket'] > $min_bucket) {
//let's find the parent
$index = $num;
if($l[$index]['type'] == 'property') {
if($l[$index-1]['type'] == 'rule') {
$v['first'] = true;
}
if(!isset($l[$index+1]) || $l[$index+1]['type'] == 'rule') {
$v['last'] = true;
}
}
while($index > 0) {
$index--;
if($l[$index]['bucket'] < $v['bucket']) {
$v['parent'] = $index;
break;
}
}
}
$l[$num] = $v;
}
return $l;
}
private function sortProperties() {
$v = $this->lines[0];
for($i=0; $i < count($this->lines); $i++) {
$v = $this->lines[$i];
if($v['type'] == 'rule') {
continue;
}
$comp = array();
$tmp = array();
while($v['type'] == 'property') {
$comp[$i] = $v['line'];
$tmp[$i] = $v;
$i++;
$v = $this->lines[$i];
}
if(count($tmp) > 1) {
//let's sort this puppy
$tmp_order = array_keys($comp);
asort($comp);
$j = 0;
foreach($comp as $index => $val) {
if($j == 0) {
$tmp[$index]['first'] = true;
}
else {
unset($tmp[$index]['first']);
}
unset($tmp[$index]['last']);
$this->lines[$tmp_order[$j]] = $tmp[$index];
$j++;
}
$this->lines[$tmp_order[$j-1]]['last'] = true;
}
}
}
public function cssHeader() {
header('Content-Type: text/css');
}
public function getOutput() {
$str = '';
foreach($this->lines as $k => $v) {
if($v['type'] == 'property') {
$str .= $this->getProperty($k);
}
}
return $str;
}
public function render() {
$this->cssHeader();
echo $this->getOutput();
}
}