-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.js
215 lines (158 loc) · 5.48 KB
/
html.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
#! /usr/bin/env node
// 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 <[email protected]>');
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.'
}
]);
var options = cli.parse();
if((Object.keys(options).length <= 0) || options.help || !(options.file)) {
console.log(cli.getUsage({
header: 'CTML Decompiler',
footer: '\n By Sam Weaver <[email protected]>'
}));
process.exit(0);
}
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
__dirname = process.cwd();
var fileName = options.file;
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 += '.html';
}
var filePath = path.resolve(__dirname, fileName);
var fileText = fs.readFileSync(filePath, {
encoding: 'utf-8'
});
//1, load html into cheerio
var $ = cheerio.load(fileText);
var outputString = '';
var tabLength = 0;
var escapeDoubleQuotes = function(string) {
return string.replace(/\"/g, '\\"');
};
var removeNewlines = function(string) {
return string.replace(/\n/g, '').replace(/\r/g, '');
};
var processFunction = function(nodes) {
// this function will add each node to the output array
for (index in nodes) {
// skip if not a number
if (isNaN(index)) continue;
var node = nodes[index];
var $node = $(nodes[index]);
var id = $node.attr('id') || null;
var classes = ($node.attr('class') || '').split(' ');
if (classes.length == 1 && classes[0] == '') classes = null;
var attributes = node.attribs || null;
var name = node.name || null;
// tabLength++;
var elementString = '';
//add tabs to the element string for indent
for (var i = 0; i < tabLength; i++) {
elementString += ' ';
}
if (name) {
// console.log(name);
elementString += '-';
//name the element
elementString += name;
//if id, add id
if (id) {
elementString += ' #' + id;
}
//if classes, add classes
if (classes) {
for (classIndex in classes) {
elementString += ' .' + classes[classIndex];
}
}
//if attributes add attributes
if (attributes) {
for (attrKey in attributes) {
if (attrKey == 'id' || attrKey == 'class') continue;
elementString += ' [' + attrKey + '=\"' + escapeDoubleQuotes(attributes[attrKey]) + '\"]'
}
}
outputString += elementString;
elementString = '';
//we now recurse for each child
if(node.children.length > 0){
outputString += '\n';
tabLength++;
processFunction(node.children);
tabLength--;
//add tabs to the element string for indent
for (var i = 0; i < tabLength; i++) {
elementString += ' ';
}
} else {
outputString += ' ';
}
elementString += '-\n';
} else {
//we need to now get the internal text
//and we now return the tab level to default
// tabLength--;
if (node.type == 'text') {
var tex = removeNewlines(node.data.trim());
if (tex == '') continue;
// console.log(tex);
elementString += tex + '\n';
}
if (node.type == 'comment') {
var tex = removeNewlines(node.data.trim());
if(tex == '') continue;
elementString += '// '+tex+'\n';
}
}
outputString += elementString;
// console.log(elementString);
//
// elementString += $node.text();
// console.log(JSON.stringify($node.text()));
//additionally, we close the element
//finally, we append the elementString
// console.log(node);
}
};
processFunction($.root().children());
//value is now in outputString
console.log('\n[INF] Compile Complete.');
console.log('[INF] Writing to file: ctml_dist/' + path.basename(fileName, path.extname(fileName)) + '.ctml');
try {
fs.mkdirSync(__dirname + '/ctml_dist');
} catch (e) {
}
fs.writeFileSync('ctml_dist/' + path.basename(fileName, path.extname(fileName)) + '.ctml', outputString);
console.log('[INF] Saved!');
process.exit(0);