-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmincss.php
More file actions
63 lines (52 loc) · 1.16 KB
/
mincss.php
File metadata and controls
63 lines (52 loc) · 1.16 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
<?php
function mincss_load ($file)
{
$key_pattern = '#({{)\s*\w+\s*:[^}]+\s*(}})#';
$ref_pattern = '#({{)\w+(}})#';
$data = file_get_contents($file);
/**
* Extract keys from file
*/
$keys = null;
preg_match_all ($key_pattern, $data, $keys, PREG_PATTERN_ORDER);
$keys = $keys[0];
$keys_temp = null;
foreach ($keys as $key => $value)
{
$temp = str_replace ('{{', '', $value);
$temp = str_replace ('}}', '', $temp);
$temp = explode (':', $temp, 2);
$key = trim($temp[0], " \t\n\r\0\x0B");
$value = trim($temp[1], " \t\n\r\0\x0B");
$keys_temp[$key] = $value;
}
$keys = $keys_temp;
/**
* Remove keys definitions
*/
foreach ($keys as $key => $value)
{
$data = preg_replace($key_pattern, '', $data);
}
/**
* Replace variables with defined values
*/
foreach ($keys as $key => $value)
{
$data = str_replace('{{' . $key . '}}', $value, $data);
$data = str_replace('$' . $key, $value, $data);
}
/**
* Remove multiple line-breaks
*/
$data = preg_replace("/[\r\n]{3,}/", "\n\n", $data);
/**
* Render as CSS
*/
header('Content-Type: text/css; charset: UTF-8');
echo $data;
/**
* Return true on success
*/
return true;
}