forked from allwcons/acss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
223 lines (199 loc) · 7.46 KB
/
compiler.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Extend String prototype to support splice operation
if (!String.prototype.splice) {
/**
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
// Function to process and replace CSS code with custom logic
function replaceCss(css_code) {
var EXPRESSION_DEFINER = [";", ":", "(", ")", ","];
var parsedcss = css_code;
var new_css = parsedcss;
var finished_operation_code = [];
var regexp = new RegExp(' [+-/*] ', 'g');
var match;
while ((match = regexp.exec(parsedcss)) !== null) {
var index = match.index;
var start = 0, end = 0;
var current_index = index - 1;
// Find start of the operation
while (start == 0) {
var char = parsedcss.charAt(current_index);
if (EXPRESSION_DEFINER.indexOf(char) != -1) {
start = current_index + 2;
break;
}
if (current_index == 0) break;
current_index--;
}
// Find end of the operation
current_index = index + 2;
while (end == 0) {
var char = parsedcss.charAt(current_index);
if (EXPRESSION_DEFINER.indexOf(char) != -1) {
end = current_index - 1;
break;
}
if (current_index == parsedcss.length) break;
current_index++;
}
var begin_code = parsedcss.substr(0, start);
var end_code = parsedcss.substring(end + 1, parsedcss.length);
var rawoperationcode = parsedcss.substring(start - 1, end + 1);
var operationcode = "calc(" + rawoperationcode + ")";
// Replace only if not already processed
if (finished_operation_code.indexOf(rawoperationcode) == -1) {
new_css = new_css.replace(rawoperationcode, operationcode);
} else {
break;
}
finished_operation_code.push(rawoperationcode);
}
// Replace custom variables and return modified CSS
new_css = new_css.replace(/\$(\w+)\s*:/g, "--$1:");
new_css = new_css.replace(/\$(\w+)/g, "var(--$1)");
return new_css;
}
// Patterns for 'if' and hook extraction
let if_pattern = /if @(\w+)\s*\?\s*(.*)\s*>\s*(.*);/g;
// Templates for conditional CSS
function if_else_template(_if, _else, key) {
return `\n--if_${key}:${_if};--else_${key}:${_else};\n`
}
function if_else_var_template(key) {
return `var(--else_${key},var(--if_${key}));`
}
// Compile ACSS to standard CSS
function compileAcss(code) {
let SELECTOR_DEFINER = /}(.|\n)[^{]*/g;
let match;
let selectors = [];
let newcode = code;
while ((match = if_pattern.exec(code)) != null) {
let data = {};
let key = uuid();
let _value = match[0];
let _event = match[1];
let _if = match[2].trim(); // Trim spaces around values
let _else = match[3].trim(); // Trim spaces around values
let length = _value.length;
let index = match.index;
let start = null;
let current_index = index;
let var_code = if_else_var_template(key);
code = code.splice(index, length, var_code);
let PROPERTY_DEFINER = ["\s", "\n", ";"];
while (!start) {
let char = code.charAt(current_index);
if (PROPERTY_DEFINER.indexOf(char) != -1) {
start = current_index;
break;
}
current_index--;
}
let if_else_code = if_else_template(_if, _else, key);
code = code.splice(start, 0, if_else_code);
let new_start = null;
current_index = start;
let selector = "";
let status = false;
while (!new_start) {
let char = code.charAt(current_index);
if (char == "}") {
status = false;
break;
}
if (status) {
selector = char + selector;
}
if (char == "{") {
status = true;
}
if (current_index == 0) break;
current_index--;
}
selector = selector.trim();
data = { _event, _if, _else, selector, key };
selectors.push(data);
}
let hook = compileGlobalHook(code);
return [hook[1], selectors, hook[0]];
}
// Compile global hook for event handlers
function compileGlobalHook(code) {
let reg_pattern = /@(\w+) as (\w+)/g;
let global_hook_data = [];
let match;
while ((match = reg_pattern.exec(code)) != null) {
let event = match[1];
let index = match.index;
let event_as = match[2];
let start = null;
let current_index = index - 2;
let selector = "";
let length_selector = 0;
let length_input = match[0].length;
while (!start) {
let char = code.charAt(current_index);
if (char != "}" && current_index != 0) {
selector = char + selector;
} else {
start = current_index;
break;
}
length_selector++;
current_index--;
}
code = code.splice(start, length_input + length_selector + 3, "");
selector = selector.trim();
global_hook_data.push({
selector: selector, event, event_as
});
}
return [global_hook_data, code];
}
var fs = require('fs');
let args = process.argv.slice(2);
let filename = args[0];
let js_filename = args[1];
// Watch for file changes and process ACSS files
function watch_acss() {
let css_data = fs.readFileSync(filename, 'utf8');
if (args[0].includes(".acss")) {
let new_css_data = replaceCss(css_data);
new_css_data = compileAcss(new_css_data);
let newfile = filename.slice(0, -5) + ".css";
fs.writeFileSync(newfile, new_css_data[0]);
fs.writeFileSync(js_filename, `
let setup_json = ${JSON.stringify(new_css_data[1])}
let external_hook = ${JSON.stringify(new_css_data[2])}
window.onload = () => installAcss()
`);
} else {
console.log("Unknown extension. Please use an .acss file.");
}
}
// Initial file processing
console.log("Watching file changes...");
fs.watchFile(filename, {}, () => {
watch_acss();
console.log("Recompiled the changes");
});
watch_acss();
module.exports = { replaceCss, compileAcss };