-
Notifications
You must be signed in to change notification settings - Fork 11
/
manipulate-css.php
84 lines (64 loc) · 2.07 KB
/
manipulate-css.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
<?php
declare(strict_types=1);
if (count($argv) != 2) {
echo "Usage: $argv[0] <FILENAME>";
exit(1);
}
$orange_codes = '/#(ff8000|ED7109|ed7109|ff6700|c60|bf6000|d96d00|20a0ff)/';
$rgba_codes = '/(rgba\(255,128,0|rgba\(255,147,35|rgba\(255,147,38)/';
$final_css = array();
$placeholder = '##maincolor##';
// Datei als Array eingelesen
$array = explode("}", file_get_contents($argv[1]));
$last = count($array)-1;
foreach($array as $key => $value){
$inside = "";
// verschachtelte zusammenführen
if ( substr_count($value, "{") > 1 ){
$inside = "y";
$inside_start = $key;
$inside_ende = $key;
}
if ( trim($value) == "" AND $inside == "y" ){
$inside_ende = $key;
while($inside_start < $key){
$array[$key] = trim($array[$key] ."\n". $array[$inside_start] ."}");
unset($array[$inside_start]);
$inside_start++;
}
$inside = "n";
$value = $array[$key];
}
if( $inside != "y" ){
$array[$key] = trim($value."}");
}
}
// clean last element
if ( trim($array[$last]) == "}" ) {
unset($array[$last]);
}
// Array-Index wieder neu aufbauen
$array = array_values($array);
//print_r($array);
// 1. save code-block only if it contains a "#..."
foreach($array as $key => $value){
if( preg_match($orange_codes, $value) OR preg_match($rgba_codes, $value) ){
// 2. replace orange code with placehoder
$tmp = str_replace(["\r", "\n"], '', preg_replace($rgba_codes, 'rgba(##mainrgba##', preg_replace($orange_codes, $placeholder, trim($value))));
//$tmp = $tmp.'}';
$final_css[] = $tmp;
}
}
// implode array
$final_css_string = implode("\n",$final_css);
// jede Zeile durchgehen und alles entfernen, was kein . { } oder # enthält:
$final_css_linebyline = explode("\n", $final_css_string);
foreach($final_css_linebyline as $key => $value){
if( !preg_match('/\.[a-zA-Z]|{|}|#/', $value) ){
unset($final_css_linebyline[$key]);
}
}
$final_css_linebyline = array_values($final_css_linebyline);
$final_css_string2 = implode("\n",$final_css_linebyline);
// Save to file
file_put_contents($argv[1], $final_css_string2);