-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
517 lines (425 loc) · 16.2 KB
/
docs.js
File metadata and controls
517 lines (425 loc) · 16.2 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
const docsConfig = [
{
section: "Getting Started",
items: [
{ title: "Introduction", file: "intro.md" },
{ title: "Installation", file: "install.md" },
{ title: "Your First Program", file: "first_steps.md" }
]
},
{
section: "Core Concepts",
items: [
{ title: "S-Expressions", file: "sexpr.md" },
{ title: "Variables & Scope", file: "vars.md" },
{ title: "Functions", file: "funcs.md" }
]
},
{
section: "Standard Library",
items: [
{ title: "Math Operations", file: "math.md" },
{ title: "String Manipulation", file: "strings.md" },
{ title: "IO Operations", file: "io.md" }
]
}
];
const keywords = [
'start', 'if', 'let', 'chlet', 'else', 'and', 'or', 'not',
'cond', 'case', 'delay', 'quote', 'lambda', 'begin', 'set!',
'define', 'macro'
];
const commands = [
'load', 'input', 'gensym', 'tracing', 'make-closure', 'defined?',
'eval', 'apply', 'call-with-current-continuation', 'force',
'image', 'nl', 'err', 'gc', 'gc-verbose', 'new-segment', 'oblist',
'inexact->exact', 'exp', 'log', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan',
'sqrt', 'tarry', 'expt', 'floor', 'ceiling', 'truncate', 'round',
'+', '-', '*', '/', 'quotient', 'remainder', 'modulo', '=', '<', '>', '<=', '>=',
'head', 'tail', 'cons', 'set-head!', 'set-tail!', 'list', 'list*',
'reverse', 'append', 'length', 'assq', 'pair?', 'list?', 'null?',
'char->integer', 'integer->char', 'char-upcase', 'char-downcase',
'symbol->string', 'atom->string', 'string->sign', 'string->atom',
'eq?', 'eqv?', 'boolean?', 'eof-object?', 'sign?', 'number?',
'string?', 'integer?', 'real?', 'char?', 'port?', 'input-port?',
'output-port?', 'procedure?', 'environment?', 'vector?', 'closure?', 'micro?',
'make-string', 'string-length', 'string-ref', 'string-set!',
'string-append', 'substring',
'vector', 'make-vector', 'vector-length', 'vector-ref', 'vector-set!',
'char-alphabetic?', 'char-numeric?', 'char-whitespace?',
'char-upper-case?', 'char-lower-case?',
'current-input-port', 'current-output-port', 'open-input-file',
'open-output-file', 'open-input-output-file', 'open-input-string',
'open-input-output-string', 'open-output-string', 'get-output-string',
'close-input-port', 'close-output-port', 'interaction-environment',
'current-environment', 'read', 'read-char', 'peek-char', 'char-ready?',
'set-input-port', 'set-output-port',
'put', 'get', 'get-closure-code'
];
function escapeHtml(unsafe) {
if (!unsafe) return "";
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function highlightSyntax(text) {
let highlighted = '';
let i = 0;
const safeAdd = (char) => {
if (char === '<') return '<';
if (char === '>') return '>';
if (char === '&') return '&';
return char;
};
while (i < text.length) {
const char = text[i];
if (char === ';') {
let comment = ';'; i++;
while (i < text.length && text[i] !== '\n') {
comment += text[i]; i++;
}
highlighted += `<span class="comment">${escapeHtml(comment)}</span>`;
continue;
}
if (char === '"') {
let str = '"'; i++;
while (i < text.length) {
if (text[i] === '"' && text[i-1] !== '\\') break;
str += text[i]; i++;
}
if (i < text.length) { str += '"'; i++; }
highlighted += `<span class="string">${escapeHtml(str)}</span>`;
continue;
}
if (char === '[' || char === ']') {
highlighted += `<span class="bracket">${char}</span>`;
i++;
continue;
}
if (char === '\\' && text[i+1] === 'n') {
highlighted += `<span class="escape">\\n</span>`;
i += 2;
continue;
}
if (char === '\n') { highlighted += '<br>'; i++; continue; }
if (char === ' ') { highlighted += ' '; i++; continue; }
if (/[a-zA-Z0-9_\-\?\>\<\=\+\*\/!]/.test(char)) {
let word = '';
while (i < text.length && /[a-zA-Z0-9_\-\?\>\<\=\+\*\/!]/.test(text[i])) {
word += text[i];
i++;
}
if (keywords.includes(word)) {
highlighted += `<span class="keyword">${escapeHtml(word)}</span>`;
} else if (commands.includes(word)) {
highlighted += `<span class="command">${escapeHtml(word)}</span>`;
} else {
highlighted += escapeHtml(word);
}
continue;
}
highlighted += safeAdd(char);
i++;
}
return highlighted;
}
window.copyToClipboard = function(btn) {
const codeContainer = btn.parentElement;
const preBlock = codeContainer.querySelector('pre');
if (!preBlock) return;
const text = preBlock.innerText || preBlock.textContent;
navigator.clipboard.writeText(text).then(() => {
const originalText = btn.innerText;
btn.innerText = "Copied!";
setTimeout(() => btn.innerText = originalText, 2000);
}).catch(err => {
console.error('Failed to copy:', err);
btn.innerText = "Error";
});
};
const renderer = {
code(codeToken) {
let rawCode = typeof codeToken === 'string' ? codeToken : (codeToken.text || "");
if (typeof rawCode === 'string') {
rawCode = rawCode.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
} else {
rawCode = String(rawCode);
}
const highlighted = highlightSyntax(rawCode);
return `
<div class="code-container">
<button class="copy-btn" onclick="window.copyToClipboard(this)">Copy</button>
<pre><code>${highlighted}</code></pre>
</div>`;
}
};
marked.use({ renderer });
const BOOK_LOADER_HTML = `
<div class="loader-wrapper">
<div class="book">
<div class="book__pg-shadow"></div>
<div class="book__pg"></div>
<div class="book__pg book__pg--2"></div>
<div class="book__pg book__pg--3"></div>
<div class="book__pg book__pg--4"></div>
<div class="book__pg book__pg--5"></div>
</div>
</div>
`;
function setCookie(name, value, days) {
let expires = "";
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
let nameEQ = name + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
let globalRotationY = 0;
function updateDynamicFavicon(renderer, scene, camera, group) {
const link = document.querySelector("link[rel*='icon']") || document.createElement('link');
const originalScale = group.scale.x;
const originalZ = camera.position.z;
group.scale.set(1.6, 1.6, 1.6);
camera.position.z = 2.2;
renderer.render(scene, camera);
link.type = 'image/png';
link.rel = 'shortcut icon';
link.href = renderer.domElement.toDataURL("image/png");
if (!document.querySelector("link[rel*='icon']")) {
document.getElementsByTagName('head')[0].appendChild(link);
}
group.scale.set(originalScale, originalScale, originalScale);
camera.position.z = originalZ;
}
function create3DLogo(containerId, size, isInteractive) {
const container = document.getElementById(containerId);
if (!container) return;
container.innerHTML = '';
const scene = new THREE.Scene();
const cameraZ = size < 100 ? 2.5 : 3;
const camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
preserveDrawingBuffer: true
});
renderer.setSize(size, size);
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
const group = new THREE.Group();
const coreGeom = new THREE.IcosahedronGeometry(1, 0);
const coreMat = new THREE.MeshPhongMaterial({
color: 0x2563eb,
flatShading: true,
shininess: 100
});
const core = new THREE.Mesh(coreGeom, coreMat);
group.add(core);
const wireframeGeom = new THREE.IcosahedronGeometry(1.05, 0);
const wireframeMat = new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: true,
transparent: true,
opacity: 0.3
});
const wireframe = new THREE.Mesh(wireframeGeom, wireframeMat);
group.add(wireframe);
scene.add(group);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5).normalize();
scene.add(light);
scene.add(new THREE.AmbientLight(0x404040));
camera.position.z = cameraZ;
document.addEventListener('themeChanged', (e) => {
const isDark = e.detail === 'dark';
coreMat.color.setHex(isDark ? 0x3b82f6 : 0x2563eb);
wireframeMat.color.setHex(isDark ? 0xffffff : 0x000000);
wireframeMat.opacity = isDark ? 0.3 : 0.15;
});
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
let rotationOffset = 0;
let manualRotationX = 0;
const onMove = (e) => {
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
if (isDragging && isInteractive) {
if (e.cancelable && e.type === 'touchmove') e.preventDefault();
const deltaMove = {
x: clientX - previousMousePosition.x,
y: clientY - previousMousePosition.y
};
rotationOffset += deltaMove.x * 0.005;
manualRotationX += deltaMove.y * 0.005;
}
previousMousePosition = { x: clientX, y: clientY };
};
const startDrag = (e) => {
if (!isInteractive) return;
isDragging = true;
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
previousMousePosition = { x: clientX, y: clientY };
container.style.cursor = "grabbing";
};
const stopDrag = () => {
isDragging = false;
if(isInteractive) container.style.cursor = "move";
};
if (isInteractive) {
container.addEventListener('mousedown', startDrag);
container.addEventListener('touchstart', startDrag, { passive: false });
window.addEventListener('mouseup', stopDrag);
window.addEventListener('touchend', stopDrag);
window.addEventListener('mousemove', onMove);
window.addEventListener('touchmove', onMove, { passive: false });
}
let frameCount = 0;
function animate() {
requestAnimationFrame(animate);
if (containerId === 'sidebar-3d-logo') {
globalRotationY += 0.005;
frameCount++;
if (frameCount % 30 === 0) {
updateDynamicFavicon(renderer, scene, camera, group);
}
}
if (!isDragging) {
rotationOffset = rotationOffset * 0.95;
manualRotationX = manualRotationX * 0.95;
}
group.rotation.y = globalRotationY + rotationOffset;
group.rotation.x = 0.002 + manualRotationX;
renderer.render(scene, camera);
}
animate();
}
function toggleTheme() {
const checkbox = document.getElementById('themeCheckbox');
const theme = checkbox.checked ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', theme);
document.body.setAttribute('data-theme', theme);
setCookie('lax_theme', theme, 30);
document.dispatchEvent(new CustomEvent('themeChanged', { detail: theme }));
}
function initTheme() {
const savedTheme = getCookie('lax_theme') || 'dark';
const checkbox = document.getElementById('themeCheckbox');
document.documentElement.setAttribute('data-theme', savedTheme);
document.body.setAttribute('data-theme', savedTheme);
if(checkbox) checkbox.checked = (savedTheme === 'light');
document.dispatchEvent(new CustomEvent('themeChanged', { detail: savedTheme }));
}
const menuContainer = document.getElementById('doc-menu');
const contentContainer = document.getElementById('markdown-viewer');
function buildSidebar() {
if (!menuContainer) return;
menuContainer.innerHTML = '';
docsConfig.forEach((section) => {
const sectionDiv = document.createElement('div');
sectionDiv.className = 'menu-section';
const title = document.createElement('div');
title.className = 'menu-title';
title.innerText = section.section;
sectionDiv.appendChild(title);
section.items.forEach((item) => {
const link = document.createElement('a');
link.href = `#${item.file}`;
link.className = 'menu-link';
link.innerText = item.title;
link.dataset.file = item.file;
link.onclick = (e) => {
e.preventDefault();
loadPage(item.file);
const sidebar = document.getElementById('sidebar');
if (sidebar) sidebar.classList.remove('open');
};
sectionDiv.appendChild(link);
});
menuContainer.appendChild(sectionDiv);
});
}
async function loadPage(fileName) {
if (!contentContainer) return;
document.querySelectorAll('.menu-link').forEach(l => l.classList.remove('active'));
const activeLink = document.querySelector(`.menu-link[data-file="${fileName}"]`);
if(activeLink) activeLink.classList.add('active');
contentContainer.innerHTML = BOOK_LOADER_HTML;
contentContainer.classList.remove('fade-in');
try {
const response = await fetch(fileName);
if (!response.ok) {
const demoContent = generateDemoContent(fileName);
renderMarkdown(demoContent);
return;
}
const text = await response.text();
renderMarkdown(text);
} catch (err) {
const demoContent = generateDemoContent(fileName);
renderMarkdown(demoContent);
}
}
function renderMarkdown(text) {
const html = marked.parse(text);
contentContainer.innerHTML = html;
void contentContainer.offsetWidth;
contentContainer.classList.add('fade-in');
}
function generateDemoContent(fileName) {
return `
# ${fileName.replace('.md', '').toUpperCase()}
Welcome to the documentation for **${fileName}**.
## Overview
This is a generated placeholder because the actual file \`${fileName}\` was not found.
### Example Code
\`\`\`
[start
[image "Hello from ${fileName}"]
[spot x 10]
[if [> x 5]
[image "Big"]
[image "Small"]]
]
\`\`\`
`;
}
window.onload = () => {
initTheme();
create3DLogo('sidebar-3d-logo', 120, true);
create3DLogo('nav-mini-logo', 40, true);
buildSidebar();
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
if (mobileMenuBtn) {
mobileMenuBtn.onclick = () => {
document.getElementById('sidebar').classList.toggle('open');
};
}
document.addEventListener('click', (e) => {
const sidebar = document.getElementById('sidebar');
const btn = document.getElementById('mobile-menu-btn');
if (window.innerWidth <= 768 &&
sidebar.classList.contains('open') &&
!sidebar.contains(e.target) &&
!btn.contains(e.target)) {
sidebar.classList.remove('open');
}
});
if(docsConfig.length > 0 && docsConfig[0].items.length > 0) {
loadPage(docsConfig[0].items[0].file);
}
};