-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStarblast Ship Mod Export to Ship Script Converter.html
100 lines (79 loc) · 3.29 KB
/
Starblast Ship Mod Export to Ship Script Converter.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Starblast Ship Mod Export to Ship Script Converter</title>
<style>
body {
background-color: #0b1b22;
}
#jsonInput {
background-color: #09161c;
color: #d1d3cf;
}
#indentationInput {
background-color: #09161c;
color: #d1d3cf;
}
#convert {
background: linear-gradient(135deg,#09161c 0,#0B3649 100%);
}
#coffeescriptOutput {
background-color: #09161c;
color: #d1d3cf;
}
</style>
</head>
<body>
<input type="text" id="jsonInput" placeholder="JSON" />
<input type="number" id="indentationInput" placeholder="Starting Indentation" value="1" />
<button type="button" id="convert" onclick="convert();">Convert</button>
<textarea id="coffeescriptOutput" rows="20" cols="80">CoffeeScript</textarea>
<script>
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
function convert() {
var json = document.getElementById("jsonInput").value;
var coffeescript = json;
coffeescript = replaceAll(coffeescript, ",\"", "\n");
coffeescript = replaceAll(coffeescript, "\t\"", "");
coffeescript = replaceAll(coffeescript, "{\"", "\n");
coffeescript = replaceAll(coffeescript, "\":", ": ");
var indent = 0;
try {
indent = Number(document.getElementById("indentationInput").value);
} catch (e) {
}
var coffeescriptLines = coffeescript.split(/\r\n|\r|\n/);
var typespecIndex = -1;
for (var i = 0; i < coffeescriptLines.length; i++) {
var line = coffeescriptLines[i];
for (var j = 0; j < indent; j++) {
line = " " + line;
}
if (line.length === line.indexOf(": ") + 2) {
indent++;
line = replaceAll(line, ": ", ":");
}
indent -= (line.match(/}/g) || []).length;
line += "\n";
if (line.indexOf("typespec:") !== -1) {
typespecIndex = i;
//coffeescriptLines[i - 1] = replaceAll(coffeescriptLines[i - 1], "\n", "");
}
if (i === 0) {
line = "return model =\n";
} else if (typespecIndex !== -1 && i >= typespecIndex) {
line = null;
}
coffeescriptLines[i] = line;
}
//coffeescript = coffeescriptLines.join("\n");
coffeescript = coffeescriptLines.join("");
coffeescript = replaceAll(coffeescript, "}", "");
document.getElementById("coffeescriptOutput").innerHTML = coffeescript;
}
</script>
</body>
</html>