This repository was archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
executable file
·116 lines (88 loc) · 3.46 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
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
var Parser = require('htmlparser2').Parser;
function ItemList(parent) {
this.parent = parent;
this.content = '';
this.spacer = '';
this.indent = parent ? parent.indent : '';
this.isFirstItem = true;
}
ItemList.prototype.addSpace = function (space) {
this.spacer += space;
if (space.indexOf("\n") !== -1) {
// reset indent when there are new lines
this.indent = /[^\n]*$/.exec(space)[0];
} else {
// otherwise keep appending to current indent
this.indent += space;
}
}
ItemList.prototype.add = function (data, ignoreComma) {
if (!ignoreComma) {
if (!this.isFirstItem) {
this.content += this.spacer.length ? ',' : ', ';
}
this.isFirstItem = false;
}
this.content += this.spacer;
this.spacer = '';
this.content += data;
}
function convert(inputMarkup) {
var elementStack = [];
var currentItemList = new ItemList(null);
var parser = new Parser({
onopentag: function (name, attribs) {
currentItemList = new ItemList(currentItemList);
elementStack.unshift([ name, attribs ]);
},
ontext: function (text) {
var lines = text.split("\n");
var isFirst = true;
lines.forEach(function (line) {
var lineMatch = /^(\s*)(.*?)(\s*)$/.exec(line);
var preSpace = lineMatch[1],
mainText = lineMatch[2],
postSpace = lineMatch[3];
if (!isFirst) {
currentItemList.addSpace("\n");
}
currentItemList.addSpace(preSpace);
if (mainText.length > 0) {
currentItemList.add(JSON.stringify(mainText));
}
isFirst = false;
});
},
onclosetag: function (tagname) {
var element = elementStack.shift();
var elementContent = currentItemList.content + currentItemList.spacer;
currentItemList = currentItemList.parent;
var indent = currentItemList.indent;
var attribs = element[1];
var id = attribs['id'];
var idSuffix = id !== undefined ? '#' + id : '';
delete attribs['id'];
var classNames = attribs['class'];
var classSuffix = (classNames !== undefined ? classNames : '').split(/\s+/g).filter(function (v) { return v.length > 0; }).map(function (cls) { return '.' + cls; }).join('');
delete attribs['class'];
var attrPairs = Object.keys(attribs).map(function (k) { return JSON.stringify(k) + ': ' + JSON.stringify(attribs[k]) });
var item = 'h(' + JSON.stringify(element[0] + idSuffix + classSuffix) + (
attrPairs.length
? ", {\n" + indent + ' ' + attrPairs.join(",\n" + indent + ' ') + "\n" + indent + "}"
: ''
) + (
elementContent.length
? ', [' + (elementContent[0] === "\n" ? '' : ' ') + elementContent + (elementContent.match(/\s$/) ? '' : ' ') + ']'
: ''
) + ')';
currentItemList.add(item);
},
oncomment: function (text) {
currentItemList.add('/*' + text + '*/', false); // @todo comment-safety
}
}, {decodeEntities: true});
parser.write(inputMarkup);
parser.end();
return currentItemList.content;
}
module.exports = convert;