-
Notifications
You must be signed in to change notification settings - Fork 1
/
csx-compiler.php
80 lines (60 loc) · 2.06 KB
/
csx-compiler.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
<?php
require "csx.php";
class CsxCompiler {
var $magic = null;
var $parser = null;
// prepare an array to build the csx string
var $csx = array();
// prepare an array to build the css string
var $css = array();
// creates a new Compiler object
function __construct($csx_params) {
// store the current working directory before changing it
$previous_working_dir = getcwd();
// change to the given working directory
chdir($csx_params['parent_dir']);
// reference the manifest files
$manifest_files = $csx_params['manifest_files'];
// regex for finding header csx in css files
$regex_csx_header = '/^\\s*\\/+\\*+\\s*<csx>\\s*\\*+\\/+\\s*(.*)\\/\\*+\\s*<\\/csx>\\s*\\*+\\/+\\s*(.*)$/sm';
// iterate over the manifest files
foreach($manifest_files as $filename) {
// insert the file name of css file
$this->css []= "\n\n/************************\n** ".pathinfo($filename,PATHINFO_FILENAME)."\n************************/\n";
// append entire contents of csx files to csx string
if(preg_match('/\\.csx$/', $filename)) {
$this->csx []= file_get_contents($filename);
}
// append only csx headers of css files to csx string
else if(preg_match('/\\.css$/', $filename)) {
$css_contents = file_get_contents($filename);
if(preg_match($regex_csx_header, $css_contents, $match)) {
$this->csx []= $match[1];
$this->css []= $match[2];
}
else {
$this->css []= $css_contents;
}
}
}
// let the magic happen
$scanner = new Scanner(implode("\n",$this->csx));
$lexer = new Lexer($scanner);
$this->parser = new Parser($lexer);
$this->magic = new Lookup($this->parser);
// change back to the original working dir
chdir($previous_working_dir);
}
// get the entire structure as a json object
function get_json() {
return json_encode($this->parser->getValues());
}
// generate css
function output() {
foreach($this->css as $key => $css_string) {
$this->css[$key] = $this->magic->replaceRules($css_string);
}
return implode("\n", $this->css);
}
}
?>