-
Notifications
You must be signed in to change notification settings - Fork 16.2k
/
Copy pathcompileAnimationList.js
66 lines (52 loc) · 1.72 KB
/
compileAnimationList.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
const fs = require('fs');
const path = require('path');
/**
* Get and categorize all the animation names and compile
* to HTML lists
* @param {string} dir - directory containing the css file
* @param {string} file - css file name
*/
function compileAnimationlist(dir = '../source', file = 'animate.css') {
const filePath = path.join(__dirname, dir, file);
const content = fs.readFileSync(filePath, 'utf8');
const globalRegex = /\/(.*)\w/g;
const itemRegex = /(\/)(.*)(\.)/;
const rawList = content.match(globalRegex);
let currentGroup;
let list = {};
rawList.forEach((i) => {
const item = i.match(itemRegex);
if (item == null) {
const title = i.replace('/* ', '');
currentGroup = title;
list[title] = [];
return currentGroup;
}
return list[currentGroup].push(item[2]);
}, {});
const itemTemplate = (item) => `
<li class="animation-item" data-animation="${item}">
<span class="animation-item--title">${item}</span>
<button class="copy-icon" type="button">
<span class="tooltip">Copy class name to clipboard</span>
</button>
</li>`;
const listTemplate = (title, items) => {
const parsedTitle = title.toLowerCase().replace(' ', '_');
return `
<section class="${parsedTitle}" id="${parsedTitle}">
<h3 class="animation-title">${title}</h3>
<ul class="animation-group">${items.join('\n')}</ul>
</section>
`;
};
const compile = (list) => {
const titles = Object.keys(list);
return titles.map((title) => {
const items = list[title].map((item) => itemTemplate(item));
return listTemplate(title, items);
});
};
return compile(list).join('\n');
}
module.exports = compileAnimationlist;