-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.61 KB
/
index.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
const fs = require('fs');
const pug = require('pug');
const autoprefixer = require('autoprefixer');
const templateFunc = pug.compileFile('./templates/testcase.pug');
const data = JSON.parse(fs.readFileSync('./testcases/data.json', 'utf-8'));
let clippingContent = '';
let maskingContent = '';
for (let key in data.testcases) {
const dataThemes = data.testcases[key];
let nb = 1;
dataThemes.forEach(testcase => {
let CSS, SVG;
try {
CSS = fs.readFileSync('./testcases/' + testcase.id + '.css', 'utf-8');
SVG = fs.readFileSync('./testcases/' + testcase.id + '.svg', 'utf-8');
} catch (e) {}
const sandboxCSS = handleSandbox(CSS, testcase.id);
testcase.CSS = CSS;
testcase.sandboxCSS = sandboxCSS;
testcase.SVG = SVG;
testcase.nb = nb;
testcase.type = key.substr(0,1).toUpperCase();
const fn = templateFunc(testcase);
if (key == 'clipping') {
clippingContent += fn;
} else if (key == 'masking') {
maskingContent += fn;
}
nb++;
});
}
let result = pug.renderFile('./templates/index.pug', {clipping: clippingContent, masking: maskingContent});
fs.writeFileSync('index.html', result);
function handleSandbox (code, id) {
// prefix first
code = autoprefixer.process(code).css;
// cleanup
code = code.replace(/^\s+/g,'').replace(/\s+$/g,'');
var reg = /(\{|\})/g;
code = code.split(reg);
for (var i = 0; i < code.length - 1; i+=4) {
var selectors = code[i].split(',');
for (var j = 0; j < selectors.length; j++) {
selectors[j] = '#js-' + id + ' ' + selectors[j];
}
code[i] = selectors.join(',');
}
return code.join('');
}