-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
426 lines (384 loc) · 12.5 KB
/
index.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta content='user-scalable=0' name='viewport' />
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu+Mono" rel="stylesheet">
<style>
html, body {
font-family: 'Ubuntu', sans-serif;
margin: 0;
padding: 0;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: #9F9A7F;
}
#content {
position: fixed;
top: 75px;
bottom: 25px;
left: 25px;
right: 25px;
background-color: #D9D9D9;
overflow: auto;
padding: 20px;
border: 5px inset #A18A66;
}
#navig {
position: fixed;
top: 12.5px;
left: 25px;
height: 50px;
}
#back, #next, #home {
width: 50px;
height: 50px;
background: url(https://raw.githubusercontent.com/colobot/colobot-data/dev/textures/interface/button1.png);
background-size: 400px 400px;
}
#back {
background-position: -350px -300px;
}
#next {
background-position: 0px -300px;
}
#home {
background-position: -300px -150px;
}
h1 {
margin-top: 0;
}
h3 {
}
.pre {
font-family: 'Ubuntu Mono', monospace;
white-space: pre;
}
.code {
background-color: #FFFFCA;
padding: 10px;
}
a {
color: inherit;
text-decoration: none;
border-bottom: 1px solid #0000FF;
}
.token { color: #7D612A; }
.type { color: #4F7132; }
.const { color: #E12D2D; }
.this { color: #8B549B; }
.comment { color: #40454E; }
.keyword .token { color: #3D6E96; } /* token is old name used in SatCom */
.string { color: #3D6257; }
.key { background-color: #BFBFBF; }
</style>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
var BASEPATH = "/SatComOnline";
var current_branch = "master";
localStorage.lang = localStorage.lang || window.navigator.userLanguage || window.navigator.language || "en";
function checkLineType(line)
{
if (line.match(/^\s*$/) || line.match(/^\\[nctr];$/)) return 'whitespace';
if (line.match(/^\\[bt];/)) return 'header';
if (line.match(/^\s*([0-9]\)|[o-])/)) return 'bullet';
if (line.match(/^\\image.*;$/)) return 'image';
if (line.match(/^\\s;/) || line.match(/^\\c;\\s;/)) return 'code';
return 'plain';
}
function parseParagraphs(content)
{
content = content.split("\n");
var paragraphs = [];
var currentType = null;
var currentParagraph = "";
for (var i = 0; i < content.length; i++) {
var type = checkLineType(content[i]);
if ((type != currentType && currentType != null) || ['whitespace', 'header', 'bullet', 'image'].indexOf(currentType) !== -1) {
paragraphs.push({
type: currentType,
content: currentParagraph.substr(0, currentParagraph.length-1) // get rid of last \n
});
currentParagraph = "";
}
currentParagraph += content[i]+"\n";
currentType = type;
}
if(currentParagraph) {
paragraphs.push({
type: currentType,
content: currentParagraph.substr(0, currentParagraph.length-1) // get rid of last \n
});
}
return paragraphs;
}
function processTranslations(content)
{
// First, split it into "paragraphs", each one containing one gettext entry
var lines = content.split("\n");
var paragraphs = [];
var currentLines = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.match(/^#/)) continue; // TODO: detect fuzzy markers, ignore whole paragraph if found
if (line.match(/^\s*$/)) {
paragraphs.push(currentLines);
currentLines = [];
} else {
currentLines.push(line);
}
}
if (currentLines) {
paragraphs.push(currentLines);
}
// Now, convert each paragraph into msgid -> msgstr mapping and store it in a map
// (note that we do not support many of the advanced gettext features,
// but we don't really need them for anything here)
var translationMap = [];
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var lastLineType = null;
var data = [];
for (var j = 0; j < paragraph.length; j++) {
var line = paragraph[j];
var match = line.match(/^(msgid|msgstr) \"(.*)\"$/);
if (match) {
data[match[1]] = match[2];
lastLineType = match[1];
} else {
// This may be a multiline message
// Try to append to last known type
match = line.match(/^\"(.*)\"$/);
if (!match) {
console.error("Syntax error, no match for line", line);
continue;
}
var matchline = match[1];
matchline = matchline.replace(/\\n/g, "\n");
matchline = matchline.replace(/\\"/g, "\"");
if (!data[lastLineType]) {
data[lastLineType] = matchline;
} else {
data[lastLineType] += matchline;
}
}
}
if (data["msgid"] && data["msgstr"]) {
translationMap[data["msgid"]] = data["msgstr"];
} else {
// console.warn("No translation for ", data["msgid"]);
}
}
return translationMap;
}
// Convert file from Colobot syntax to the one used in .po files
// (and later in HTML conversion, for simplicity)
// See https://github.com/colobot/colobot-data/blob/master/README.i18n.md
function convertLine(line)
{
line = line.replace(/\\l;(.*?)\\u (.*?);/g, function(match, text, url) { return "<a "+url.replace("\\", "|")+">"+text+"</a>"; });
line = line.replace(/\\(button|key) (.*?);/g, "<$1 $2/>");
line = line.replace(/\\(const|type|token|key);(.*?)\\norm;/g, "<format $1>$2</format>");
line = line.replace(/\\c;([^\\;]*?)\\n;/g, "<code>$1</code>"); // TODO: Copying the same mistake again for compatibility - https://github.com/colobot/colobot/issues/810
line = line.replace(/\\(const|type|token|norm|key);/g, "<format $1>"); // TODO: clean up this state machine thingy and remove from spec
line = line.replace(/\\([tscbn]);/g, "<$1/>"); // TODO: weird state machine again
return line;
}
function processFile(content, translationMap)
{
function translate(s)
{
if (translationMap[s]) return translationMap[s];
else console.warn("No translation for", s);
return s;
}
var paragraphs = parseParagraphs(content);
var title = "SatCom Online";
for(var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
if (paragraph.type == 'header') {
var data = convertLine(paragraph.content);
var match = data.match(/^<([bt])\/>(.*)/);
if (match[1] == "b") {
data = translate(match[2]);
data = data.replace(/<(.*?)>/g, "");
title = data+" • "+title;
}
break;
}
}
document.title = title;
var out = "";
var lastType = null;
var listOpen = null;
var preloadImages = [];
for(var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
if (paragraph.type == 'whitespace') continue;
if (paragraph.type != 'bullet' && listOpen) {
out += "</"+listOpen+">\n";
listOpen = null;
}
var data = convertLine(paragraph.content);
var line = data;
if (paragraph.type == 'header') {
var match = data.match(/^<([bt])\/>(.*)/);
if (match[1] == "b") line = "<h1>"+translate(match[2])+"</h1>\n";
if (match[1] == "t") line = "<h3>"+translate(match[2])+"</h3>\n";
}
if (paragraph.type == 'bullet') {
var match = data.match(/^(\s*)([0-9]\)|[o-])(\s*)(.*)/);
if (!listOpen) {
listOpen = match[2].indexOf("o") !== -1 ? "ul" : "ol";
out += "<"+listOpen+">\n";
}
line = "<li>"+translate(match[4])+"</li>\n";
}
if (paragraph.type == 'plain') {
line = "<p>"+translate(data).replace(/\n/g, "<br>\n")+"</p>\n";
}
if (paragraph.type == 'code') {
// Just assume <s/> for code always, because I'm lazy and I can't really simulate a state machine with regex
data = data.replace(/<s\/>/g, "");
line = "<div class=\"code pre\">"+translate(data)+"</div>"; // TODO: syntax highlighting
}
if (paragraph.type == 'image') {
var match = data.match(/^(\\image )(.*)( \d* \d*;)$/);
var imageUrl = "https://raw.githubusercontent.com/colobot/colobot-data/"+current_branch+"/icons/"+match[2]+".png";
preloadImages.push(imageUrl);
line = "<img src=\""+imageUrl+"\">\n";
}
line = line.replace(/<a (.*?)>(.*?)<\/a>/g, function(match, url, name) { return "<a href=\""+BASEPATH+"/"+url.replace("|", "/")+"\" onclick=\"goto('"+url.replace("|", "/")+"'); return false;\">"+name+"</a>"; });
line = line.replace(/<c\/>([\s\S]*?)<n\/>/g, "<span class=\"pre\">$1</span>");
line = line.replace(/<code>([\s\S]*?)<\/code>/g, "<span class=\"pre\">$1</span>");
line = line.replace(/<format (.*?)>(.*?)<\/format>/g, "<span class=\"$1\">$2</span>");
line = line.replace(/<button ([0-9]*?)\/>/g, function(match, number) {
number = parseInt(number);
var fileid = Math.floor(number/64)+1;
var iconid = number%64;
var fileurl = "https://raw.githubusercontent.com/colobot/colobot-data/"+current_branch+"/textures/interface/button"+fileid+".png";
var w = iconid%8;
var h = Math.floor(iconid/8);
if(preloadImages.indexOf(fileurl) === -1) preloadImages.push(fileurl);
return "<span style=\"width: 1em; height: 1em; background: url("+fileurl+") "+(-w)+"em "+(-h)+"em; background-size: 8em 8em; display: inline-block; vertical-align: text-top;\"></span>";
});
line = line.replace(/<key (.*?)\/>/g, "$1"); // TODO: Maybe keep a list of default keybindings?
// TODO: we don't handle the legacy state machine stuff, hopefully not much of those is left. In the worst case we lose some formatting.
line = line.replace(/<format (.*?)\/>/, "");
line = line.replace(/<[tscbn]\/>/, "");
out += line;
lastType = paragraph.type;
}
(function(images, onload) {
if (images.length == 0) {
onload();
return;
}
var loaded = 0;
for(var i = 0; i < images.length; i++) {
var img = new Image();
img.onload = img.onerror = img.onabort = function() {
loaded++;
if (loaded >= images.length) onload();
}
img.src = images[i];
}
})(preloadImages, function() {
$("#content").html(out);
});
}
function loadFile(category, filename)
{
$.get("https://rawgit.com/colobot/colobot-data/"+current_branch+"/help/"+category+"/E/"+filename).done(function(content) {
if (localStorage.lang == "en") {
processFile(content, []);
} else {
$.get("https://rawgit.com/colobot/colobot-data/"+current_branch+"/help/"+category+"/po/"+localStorage.lang+".po").done(function(transContent) {
processFile(content, processTranslations(transContent));
});
}
});
}
function loadPath(url) {
var category, file;
var split = url.split("/");
if (split.length == 3) {
current_branch = split[0];
category = split[1];
file = split[2]+".txt";
} else if (split.length == 2) {
current_branch = split[0];
category = "generic";
file = split[1]+".txt";
} else console.log("Unable to parse path", url);
if(url.includes("/bot")) category = "bots";
loadFile(category, file);
}
function goto(url) {
loadPath(current_branch+"/"+url);
window.history.pushState(null, null, BASEPATH+"/"+current_branch+"/"+url);
}
function getPath() {
return document.location.pathname.replace(BASEPATH, "").substr(1);
}
window.onpopstate = function(event) {
loadPath(getPath());
};
function changeLang(newlang) {
localStorage.lang = newlang;
loadPath(getPath());
}
function changeBranch(newbranch) {
var path = getPath().match(/^(.*?)\/(.*)$/);
loadPath(newbranch+"/"+path[2]);
window.history.replaceState(null, null, BASEPATH+"/"+current_branch+"/"+path[2]);
}
function getBranches(callback) {
$.get("https://api.github.com/repos/colobot/colobot-data/branches", function(branchesData) {
$.get("https://api.github.com/repos/colobot/colobot-data/releases", function(releasesData) {
var branches = branchesData.map(function(x) { return x.name; });
var releases = releasesData.map(function(x) { return x.tag_name; });
callback(branches.concat(releases));
});
});
}
$(document).ready(function() {
getBranches(function(branches) {
branches.forEach(function(branch) {
$("#branch").append("<option value=\""+branch+"\">"+branch+"</option>");
});
var path = getPath();
if (path.length > 0) {
loadPath(path);
} else {
loadPath(current_branch+"/cbot");
window.history.replaceState(null, null, BASEPATH+"/"+current_branch+"/cbot");
}
$("#language").val(localStorage.lang);
$("#branch").val(current_branch);
});
});
</script>
</head>
<body>
<div id="navig">
<button id="back" onclick="window.history.go(-1);"></button>
<button id="next" onclick="window.history.go(1);"></button>
<button id="home" onclick="goto('cbot')"></button>
<select id="language" onchange="changeLang(document.getElementById('language').value);">
<option value="en">English</option>
<option value="pl">Polish</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="ru">Russian</option>
</select>
<select id="branch" onchange="changeBranch(document.getElementById('branch').value);">
</select>
</div>
<div id="content">Hey, this is SatCom in a WEB BROWSER!</div></td>
</body>
</html>