-
Notifications
You must be signed in to change notification settings - Fork 9
/
convert.js
100 lines (89 loc) · 2.56 KB
/
convert.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
const hljs = require('highlight.js');
const fs = require('fs');
const path = require('path');
const dir = './node_modules/highlight.js/lib/languages/';
function goArray(strings) {
return "[]string{"+strings.map(a => JSON.stringify(a)).join(", ")+"}";
}
function cleanRegex(obj, parents, path) {
if (!parents) {
parents = [];
}
if (!path) {
path = [];
}
parents = parents.concat([obj]);
for (let prop in obj) {
const val = obj[prop];
if (val instanceof RegExp) {
obj[prop] = val.source;
} else if (val === Object(val)) {
var idx = parents.indexOf(val);
if (idx === -1) {
obj[prop] = cleanRegex(val, parents, path.concat(prop.toString()));
} else {
if (val instanceof Array) {
obj[prop] = [{Ref: path.slice(0, idx), IsArray: true}];
} else {
obj[prop] = {Ref: path.slice(0, idx)};
}
}
}
// Hack to fix edge case where there should be no end.
if (prop == "end" && val === false) {
obj[prop] = ".^";
}
if (prop == "subLanguage") {
if (typeof val === "string") {
obj[prop] = [val];
} else if (val.length === 0) {
obj[prop] = ["all"];
}
}
// Need to call JSON.parse to correctly parse unicode escape sequences in
// the regexps.
let regexp = obj[prop];
if (typeof regexp === "string") {
// lisp strangeness.
regexp = regexp.replace("[^]*", ".*");
while (true) {
const matches = regexp.match(/\\u[0-9A-Fa-f]{4}/)
if (!matches) {
break;
}
const match = matches[0];
regexp = regexp.replace(match, JSON.parse('"'+match+'"'));
}
obj[prop] = regexp;
}
if (obj instanceof Array && obj[prop] === "self") {
obj[prop] = {Ref: path.slice(0,path.length-1)};
}
}
return obj;
}
fs.readdir(dir, (err, files) => {
files.forEach(file => {
const p = dir + file;
const lang = path.basename(file, ".js");
fs.readFile(p, (err, data) => {
if (err) throw err;
const def = cleanRegex(eval(data.toString())(hljs));
const aliases = [lang].concat(def.aliases);
console.log("Language:", lang);
try {
const json = JSON.stringify(def).replace(/`/g, "`+\"`\"+`");
fs.writeFile("languages/"+lang+".go",
`package languages
import "github.com/d4l3k/go-highlight/registry"
func init() {
registry.Register(${goArray(aliases)}, \`${json}\`)
}`, (err) => {
if (err) throw err;
});
} catch (e) {
console.log(" - failed: ", e);
};
});
});
});