-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmj2xml.js
67 lines (60 loc) · 1.72 KB
/
tmj2xml.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
const fs = require('fs')
const json = JSON.parse(fs.readFileSync('syntaxes\\hexa.tmLanguage.json').toString())
function toXml(json, tabs) {
if (json instanceof Array) {
const result = [`<array>`]
for (const value of json) {
result.push(tabs + '\t' + toXml(value, tabs + '\t'))
}
result.push(`${tabs}</array>`)
return result.join('\n')
}
if (json instanceof Object) {
const result = [`<dict>`]
for (const value of Reflect.ownKeys(json)) {
result.push(tabs + '\t' + `<key>${value}</key>`)
result.push(tabs + '\t' + toXml(json[value], tabs + '\t'))
}
result.push(`${tabs}</dict>`)
return result.join('\n')
}
if (json instanceof String || typeof(json) == "string") {
return `<string>${
json
.split('<').join('@__preserve_lt@')
.split('>').join('@__preserve_gt@')
.split('&').join('@__preserve_amp@')
.split('&').join('&')
.split('@__preserve_lt@').join('<')
.split('@__preserve_gt@').join('>')
.split('@__preserve_amp@').join('&')
.split('<').join('<')
.split('>').join('>')
}</string>`
}
return '?????????????????????????????????????????'
}
const output =
`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>${json.scopeName.split('.')[1]}</string>
</array>
<key>scopeName</key>
<string>${json.scopeName}</string>
<key>uuid</key>
<string>${json.uuid}</string>
<key>name</key>
<string>${json.name}</string>
<key>patterns</key>
${toXml(json.patterns,'\t')}
<key>repository</key>
${toXml(json.repository,'\t')}
</dict>
</plist>
`
fs.writeFileSync('Hexa.tmLanguage', output.trim() + '\n')