-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.php
81 lines (78 loc) · 2.1 KB
/
reduce.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
<?php
$classes = file("C:\Users\alan\Documents\Span\classes.txt");
foreach ($classes as $classLine) {
$className = trim($classLine);
if ($className) {
$classLookup[$className] = 1;
}
}
$css = file("C:\Users\alan\Documents\Span\stylesheet.css");
$currentContext = "";
$contextOutput = false;
$inStyle = false;
$outputtingStyle = false;
foreach ($css as $cssLine) {
switch (substr(trim($cssLine), 0, 1)) {
case false: // empty
break;
case "@":
if ($currentContext) {
echo "/* *** NESTED CONTEXT */";
}
$currentContext .= $cssLine;
$contextOutput = false;
break;
case "}":
if ($inStyle) {
if ($outputtingStyle) {
echo $cssLine;
$outputtingStyle = false;
}
$inStyle = false;
} elseif ($currentContext) {
if ($outputtingContext) {
echo $cssLine;
$outputtingContext = false;
}
$currentContext = "";
} else {
echo "/* *** DANGLING BRACE */";
}
break;
default:
if ($inStyle) {
if ($outputtingStyle) {
echo $cssLine;
}
} else {
if (substr(trim($cssLine),-1,1) == "{") {
$inStyle = true;
foreach(explode(",", $cssLine) as $term) {
$matches = [];
if(preg_match_all("/[.]([-A-Za-z0-9_]+)/",$term, $matches)) {
foreach($matches[1] as $match) {
if ($classLookup[$match]) {
$outputtingStyle = true;
break;
}
}
} else {
$outputtingStyle = true;
}
}
if ($outputtingStyle) {
if ($currentContext) {
if (!$outputtingContext) {
echo $currentContext;
$outputtingContext = true;
}
}
echo $cssLine;
}
} else {
echo "/* *** NO START BRACE */";
}
}
}
}
?>