-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctml.js
342 lines (276 loc) · 11 KB
/
ctml.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#! /usr/bin/env node
// CTML - Sam Weaver <sam@samweaver.com>
//
// A modern parser for modern html
// loosely following: http://www.w3.org/TR/html-markup/syntax.html
// Very nice looking intro logo
console.log(' ');
console.log('-------------------------------');
console.log(' * ( ');
console.log(' ( * ) ( ` )\\ ) ');
console.log(' )\\ ` ) /( )\\))( (()/( ');
console.log(' (((_) ( )(_))((_)()\\ /(_)) ');
console.log(' )\\___ (_(_()) (_()((_)(_)) ');
console.log('((/ __||_ _| | \\/ || | ');
console.log(' | (__ | | | |\\/| || |__ ');
console.log(' \\___| |_| |_| |_||____| ');
console.log('-------------------------------');
console.log('Sam Weaver <sam@samweaver.com>');
console.log('-------------------------------');
console.log(' ');
var cliArgs = require("command-line-args");
var cli = cliArgs([
{
name: 'help',
type: Boolean,
description: 'Output this screen.'
},
{
name: 'file',
type: String,
defaultOption: true,
description: 'The input file.'
},
{
name: 'comments',
alias: 'c',
type: Boolean,
description: 'Output CTML comments as HTML comments.'
}
]);
var options = cli.parse();
if((Object.keys(options).length <= 0) || options.help || !(options.file)) {
console.log(cli.getUsage({
header: 'CTML Compiler',
footer: '\n By Sam Weaver <sam@samweaver.com>'
}));
process.exit(0);
}
var fs = require('fs');
var path = require('path');
var $ = require('cheerio');
var tidy = require('htmltidy').tidy;
__dirname = process.cwd();
var fileName = options.file || null;
if (fileName == null) {
console.log('[ERR] You must provide a file name as the argument for CTML.');
console.log('[INF] Usage: ctml FILENAME');
process.exit(0);
}
if (path.extname(fileName) == '') {
fileName += '.ctml';
}
var filePath = path.resolve(__dirname, fileName);
var fileText = fs.readFileSync(filePath, {
encoding: 'utf-8'
});
// Now, start parsing
var originalFileText = fileText;
// 1, split on newlines
var parts = fileText.split(/\n/g);
// 2, separate into objects by element
var elementArray = [];
var currentElement = null;
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function matchRespectingQuotes(str, delim, dontEscape) {
if (!dontEscape) {
var delim = escapeRegExp(delim);
}
var regex = new RegExp("[^" + delim + "\"']+|\"([^\"]*)\"|'([^']*)'", 'g');
// console.log(str.match(regex));
return str.match(regex);
}
function splitRespectingQuotes(str, delim, dontEscape) {
if (!dontEscape) {
var delim = escapeRegExp(delim);
}
var regex = new RegExp('('+delim+')(?=(?:[^"]|"[^"]*")*$)','g');
return str.split(regex);
}
// 2.1 loop through array
for (var index in parts) {
// console.log(index);
var value = parts[index];
value = value.trim();
if (value.length > 0) {
if (value.charAt(0) == '-') {
//2.2 if begins with hyphen, it's an element
//2.3 if it's only a hyphen, it's an closing element
//2.4 spaces in element definitions are ignored, unless quoted
//2.5 dots are reserved for class definitions, unless quoted
// valueMatched = value.match(/[^\s"']+|"([^"]*)"|'([^']*)'/g);
valueMatched = matchRespectingQuotes(value, '\\s', true);
for (iter in valueMatched) {
// replace spaces with %s% and remove quotes
// .replace(/\"/g, '').replace(/\'/g, '')
valueMatched[iter] = valueMatched[iter].replace(/ /g, '%s%');
}
// valueMatched2 = splitRespectingQuotes(valueMatched.join(''),'..');
//
// for (iter2 in valueMatched2) {
// // replace .. with %dd%
// valueMatched2[iter2] = valueMatched2[iter2].replace(/\.\./g, '%dd%');
// }
//
// valueMatched3 = splitRespectingQuotes(valueMatched2.join(''),'.');
//
// for (iter3 in valueMatched3){
// // replace . with %d% and remove quotes
// valueMatched3[iter3] = valueMatched3[iter3].replace(/\./g,'%d%').replace(/\"/g, '').replace(/\'/g, '');
// }
//join
value = valueMatched.join('');
var finishElement = function() {
//if not master, just unnest
// console.log('element closed with name ' + currentElement.name);
if (currentElement.parent) {
var parent = currentElement.parent;
delete currentElement.parent;
parent.children.push(currentElement);
currentElement = parent;
} else {
elementArray.push(currentElement);
currentElement = null;
}
};
if (value.length == 1) {
finishElement();
} else {
// if currentElement is already defined, make a child thing
if (currentElement != null) {
var temp = currentElement;
currentElement = {};
currentElement.parent = temp;
} else {
currentElement = {};
}
currentElement.children = [];
//we begin the parsing dance
// var elementParts = value.split(/(\.|#|\[|\])/g);
var elementParts = splitRespectingQuotes(value, '.');
value = elementParts.join('%nt%dot%');
var elementParts = splitRespectingQuotes(value,'#');
value = elementParts.join('%nt%id%');
var elementParts = splitRespectingQuotes(value,'[');
value = elementParts.join('%nt%lb%');
var elementParts = splitRespectingQuotes(value,']');
value = elementParts.join('%nt%rb%');
//remove quotes
value = value.replace(/"/g,'').replace(/'/g,'');
var elementParts = value.split(/(%nt%dot%|%nt%id%|%nt%lb%|%nt%rb%)/g);
// console.log(elementParts);
// continue;
currentElement.name = elementParts[0].replace('-', '');
currentElement.id = [];
currentElement.classes = [];
currentElement.attributes = [];
// now, loop through the parts
var nextArrName = null;
for (var index in elementParts) {
if (index == 0) continue;
switch (elementParts[index]) {
case "%nt%id%":
nextArrName = "id";
break;
case "%nt%dot%":
nextArrName = "classes";
break;
case "%nt%lb%":
nextArrName = "attributes";
break;
default:
if (nextArrName != null) {
if (nextArrName == 'attributes') {
var arr = matchRespectingQuotes(elementParts[index], '=');
// console.log(arr);
elementParts[index] = {
key: arr[0]
};
if (arr.length > 1) {
arr.shift();
var str = arr.join('=');
// replace %s% with space
elementParts[index].value = str.replace(/%s%/g, ' ').replace(/%dd%/g, '..').replace(/%d%/g, '.');
}
}
currentElement[nextArrName].push(elementParts[index]);
nextArrName = null;
}
break;
}
}
if (value.charAt(value.length - 1) == '-') {
// it ends on the same line
finishElement();
}
// Drop empty arrays
// if(currentElement.id.length == 0) delete currentElement.id;
// if(currentElement.attributes.length == 0) delete currentElement.attributes;
// if(currentElement.classes.length == 0) delete currentElement.classes;
// console.log('element initialized with name ' + currentElement.name);
}
} else if (value.length > 1 && value.charAt(0) == '/' && value.charAt(1) == '/') {
// its a comment, ignore it completely
// TODO: add config option to output comments
if(options.comments) {
currentElement.children.push(' <!-- '+value+'-->');
}
} else {
// not an element, add to children of currentElement
currentElement.children.push(value);
}
}
}
// console.log('\n' + JSON.stringify(JSON.decycle(elementArray)));
// 3, compile into actual HTML
var html = $.load('<html></html>')('html');
// 3.1 loop through the object
var loopFunction = function(parent, object) {
for (key in object) {
//first, ensure object, otherwise, simply append
if (typeof object[key] !== 'object') {
parent.append(object[key] + '\n');
continue;
}
var name = object[key].name;
var newEl = $('<' + name + '></' + name + '>');
for (index in object[key].attributes) {
var attrKey = object[key].attributes[index].key || '';
var attrValue = object[key].attributes[index].value || '';
newEl.attr(attrKey, attrValue);
}
if (object[key].id.length > 0) {
newEl.attr('id', object[key].id[1]);
}
for (index in object[key].classes) {
var className = object[key].classes[index] || '';
if(className == '.') continue;
newEl.addClass(className);
}
//recurse
loopFunction(newEl, object[key].children);
//finish
parent.append(newEl);
}
};
loopFunction(html, elementArray);
// 4, beautify
// console.log(html.html());
tidy(html.html(), {
'doctype': 'html5',
'tidy-mark': false,
'indent': true,
'wrap': 0
}, function(err, html) {
console.log('\n[INF] Compile Complete.');
console.log('[INF] Writing to file: dist/' + path.basename(fileName, path.extname(fileName)) + '.html');
try {
fs.mkdirSync(__dirname + '/dist');
} catch (e) {
}
fs.writeFileSync('dist/' + path.basename(fileName, path.extname(fileName)) + '.html', html);
console.log('[INF] Saved!');
process.exit(0);
});