Repository navigation
Expand file tree
/
Copy pathsync-content.mjs
More file actions
225 lines (215 loc) · 11.5 KB
/
Copy pathsync-content.mjs
File metadata and controls
225 lines (215 loc) · 11.5 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
// Pulls docs and tutorials from the RustingStudio repositories on GitHub into
// src/content/pages/, and RustingScience posts into src/content/posts/.
// Run with `npm run sync-content`, then commit the result.
// `npm run sync-content -- strata` syncs one project and skips RustingScience.
import { mkdir, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
const ORG = 'RustingStudio';
const ONLY = process.argv[2];
const OUT = new URL('../src/content/pages/', import.meta.url);
// [repo path, slug, group, title override?, extra frontmatter?]
const MANIFEST = {
engine: {
repo: 'RustingEngine',
docs: [
['README.md', 'index', 'Start', 'Overview'],
['docs/getting-started.md', 'getting-started', 'Start'],
['docs/godot-migration.md', 'godot-migration', 'Start'],
['docs/concepts.md', 'concepts', 'Start'],
['editor_gui.md', 'editor', 'Guides'],
['docs/collaboration.md', 'collaboration', 'Guides'],
['docs/assets.md', 'assets', 'Guides'],
['docs/platforms.md', 'platforms', 'Guides'],
['docs/2d.md', '2d', 'Guides'],
['docs/input.md', 'input', 'Guides'],
['docs/physics.md', 'physics', 'Guides'],
['docs/determinism.md', 'determinism', 'Guides'],
['docs/networking.md', 'networking', 'Guides'],
['docs/animation.md', 'animation', 'Guides'],
['docs/effects.md', 'effects', 'Guides'],
['docs/procedural.md', 'procedural', 'Guides'],
['docs/lighting.md', 'lighting', 'Guides'],
['docs/look-and-feel.md', 'look-and-feel', 'Guides'],
['docs/cameras.md', 'cameras', 'Guides'],
['docs/audio.md', 'audio', 'Guides'],
['docs/menus-and-ui.md', 'menus-and-ui', 'Guides'],
['docs/modding.md', 'modding', 'Guides'],
['docs/gpu-condition-shaders.md', 'gpu-condition-shaders', 'Guides'],
['docs/surface-shaders.md', 'surface-shaders', 'Guides'],
['docs/shader-packs.md', 'shader-packs', 'Guides'],
['docs/scripting.md', 'scripting', 'Guides'],
['docs/plugin-authoring.md', 'plugin-authoring', 'Guides'],
['docs/pitfalls.md', 'pitfalls', 'Guides'],
['architecture.md', 'architecture', 'Reference'],
['docs/dev-environment.md', 'dev-environment', 'Reference'],
['roadmap.md', 'roadmap', 'Reference'],
['CHANGELOG.md', 'changelog', 'Reference'],
],
tutorials: [
['docs/tutorials/01-hello-cube.md', '01-hello-cube', 'Tutorials', null, { minutes: 10 }],
['docs/tutorials/02-coin-run-cli.md', '02-coin-run-cli', 'Tutorials'],
['docs/tutorials/03-gameplay-plugin.md', '03-gameplay-plugin', 'Tutorials'],
['docs/tutorials/04-gpu-cube-rain.md', '04-gpu-cube-rain', 'Tutorials'],
['docs/tutorials/05-physics-sandbox.md', '05-physics-sandbox', 'Tutorials'],
['docs/tutorials/06-multiplayer-physics.md', '06-multiplayer-physics', 'Tutorials'],
],
},
brain: {
repo: 'RustingBrain',
docs: [
['README.md', 'index', 'Start', 'Overview'],
['INSTALL_CUDA.md', 'install-cuda', 'Start'],
['IMPORT_MODELS.md', 'import-models', 'Guides'],
['docs/baseline.md', 'baseline', 'Reference'],
['docs/measurements.md', 'measurements', 'Reference'],
['CHANGELOG.md', 'changelog', 'Reference'],
],
// Times and parts come from tutorials/README.md.
tutorials: [
['tutorials/1_introduction.md', '1-introduction', 'Foundations', null, { minutes: 30 }],
['tutorials/2_setup.md', '2-setup', 'Foundations', null, { minutes: 10 }],
['tutorials/3_xor_problem.md', '3-xor-problem', 'Foundations', null, { minutes: 30 }],
['tutorials/4_data.md', '4-data', 'Foundations', null, { minutes: 45 }],
['tutorials/5_regression.md', '5-regression', 'The dense toolkit', null, { minutes: 45 }],
['tutorials/6_classification.md', '6-classification', 'The dense toolkit', null, { minutes: 45 }],
['tutorials/7_training_loop.md', '7-training-loop', 'The dense toolkit', null, { minutes: 60 }],
['tutorials/8_evaluation.md', '8-evaluation', 'The dense toolkit', null, { minutes: 45 }],
['tutorials/9_save_load.md', '9-save-load', 'The dense toolkit', null, { minutes: 40 }],
['tutorials/10_full_project.md', '10-full-project', 'The dense toolkit', null, { minutes: 90 }],
['tutorials/11_gpu_cuda.md', '11-gpu-cuda', 'Going further', null, { minutes: 60 }],
['tutorials/12_import_models.md', '12-import-models', 'Going further', null, { minutes: 45 }],
['tutorials/13_troubleshooting.md', '13-troubleshooting', 'Going further'],
['tutorials/14_language_model.md', '14-language-model', 'Language models', null, { minutes: 120 }],
['tutorials/15_mixture_of_experts.md', '15-mixture-of-experts', 'Language models', null, { minutes: 90 }],
['tutorials/16_training_an_llm.md', '16-training-an-llm', 'Language models'],
['tutorials/17_lora_fine_tuning.md', '17-lora-fine-tuning', 'Language models', null, { minutes: 45 }],
['tutorials/18_text_to_image.md', '18-text-to-image', 'Images', null, { minutes: 45 }],
],
},
shader: {
repo: 'RustingShader',
docs: [
['README.md', 'index', 'Start', 'Overview'],
['CHANGELOG.md', 'changelog', 'Reference'],
],
tutorials: [],
},
strata: {
repo: 'RustingStrata',
docs: [
['README.md', 'index', 'Start', 'Overview'],
['docs/USAGE.md', 'usage', 'Guides'],
['docs/HOSTING.md', 'hosting', 'Guides'],
['docs/LORA.md', 'lora', 'Guides'],
['docs/VISION.md', 'vision', 'Guides'],
['docs/SUPPORTED_GPUS.md', 'supported-gpus', 'Guides'],
['docs/AMD.md', 'amd', 'Guides'],
['docs/BENCHMARKS.md', 'benchmarks', 'Reference'],
['docs/baseline.md', 'baseline', 'Reference'],
['docs/m2-results.md', 'm2-results', 'Reference'],
['docs/m4-results.md', 'm4-results', 'Reference'],
['docs/m6-results.md', 'v1-results', 'Reference'],
['docs/V100-results.md', 'v100-results', 'Reference'],
['docs/datacenter-nvidia-results.md', 'datacenter-nvidia-results', 'Reference'],
['THANKS.md', 'thanks', 'Reference'],
],
tutorials: [],
},
runner: {
repo: 'RustingRunner',
docs: [
['README.md', 'index', 'Start', 'Overview'],
],
tutorials: [],
},
rpu: {
repo: 'RustingPU',
docs: [
['README.md', 'index', 'Start', 'Overview'],
['docs/engineering.md', 'engineering', 'Reference', 'Design'],
['docs/perf-log.md', 'perf-log', 'Reference'],
['docs/m1-results.md', 'm1-results', 'Reference'],
['CHANGELOG.md', 'changelog', 'Reference'],
],
tutorials: [],
},
};
const get = async (url, as = 'text') => {
const res = await fetch(url, { headers: { 'User-Agent': 'rustingstudio-site-sync' } });
if (!res.ok) throw new Error(`${res.status} ${url}`);
return res[as]();
};
const route = (project, kind, slug) => `/${project}/${kind}/${slug === 'index' ? '' : slug + '/'}`;
// "**Bold** and [link](x) `code`" -> "Bold and link code"
const plain = (s) => s.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[*`]/g, '').replace(/\s+/g, ' ').trim();
function convert(md, file, repo, routes, tree) {
const dir = path.posix.dirname(file);
// Imported upstream prose may still contain links from before the organization rename.
md = md.replace(/(https:\/\/(?:github\.com|raw\.githubusercontent\.com)\/)GoingRusting\//g, `$1${ORG}/`);
// Only touch prose, never fenced code.
const parts = md.split(/^(```[\s\S]*?^```)/m);
for (let i = 0; i < parts.length; i += 2) {
parts[i] = parts[i]
.replace(/<!--[\s\S]*?-->\n?/g, '')
// <picture>/<img> figures become markdown images; the site is dark only, so take the dark source.
.replace(/<picture>[\s\S]*?<\/picture>|<img\b[^>]*>/g, (html) => {
const src = html.match(/<source[^>]*prefers-color-scheme:\s*dark[^>]*srcset="([^"\s]+)"/)?.[1]
?? html.match(/<source[^>]*srcset="([^"\s]+)"[^>]*prefers-color-scheme:\s*dark/)?.[1]
?? html.match(/<img[^>]*\ssrc="([^"]+)"/)?.[1];
return src ? `![${html.match(/\salt="([^"]*)"/)?.[1] ?? ''}](${src})` : html;
})
.replace(/(!?)\[([^\]]*)\]\(([^)\s]+)\)/g, (all, bang, text, target) => {
if (/^[a-z]+:|^#|^mailto:/i.test(target)) return all;
const [p, hash = ''] = target.split('#');
const resolved = path.posix.normalize(path.posix.join(dir, p));
if (bang) {
if (!tree.has(resolved)) return ''; // image missing upstream: drop it
return ``;
}
if (routes.has(resolved)) return `[${text}](${routes.get(resolved)}${hash ? '#' + hash : ''})`;
return `[${text}](https://github.com/${ORG}/${repo}/blob/main/${resolved}${hash ? '#' + hash : ''})`;
});
}
return parts.join('');
}
for (const [project, m] of Object.entries(MANIFEST).filter(([p]) => !ONLY || p === ONLY)) {
const treeJson = await get(`https://api.github.com/repos/${ORG}/${m.repo}/git/trees/main?recursive=1`, 'json');
const tree = new Set(treeJson.tree.map((t) => t.path));
const routes = new Map();
for (const kind of ['docs', 'tutorials'])
for (const [file, slug] of m[kind]) routes.set(file, route(project, kind, slug));
await rm(new URL(`${project}/`, OUT), { recursive: true, force: true });
for (const kind of ['docs', 'tutorials']) {
await mkdir(new URL(`${project}/${kind}/`, OUT), { recursive: true });
for (const [order, [file, slug, group, titleOverride, extra = {}]] of m[kind].entries()) {
let md = await get(`https://raw.githubusercontent.com/${ORG}/${m.repo}/main/${file}`);
md = convert(md, file, m.repo, routes, tree);
const h1 = md.match(/^# (.+)$/m);
if (h1) md = md.replace(h1[0], ''); // DocsShell renders the title
const title = titleOverride ?? plain(h1?.[1] ?? slug);
const firstPara = md.split(/\n\s*\n/).map((b) => b.trim()).find((b) => b && !/^([#>!|`<-]|\* |\d+\.)/.test(b) && !/^\*\*[^*]+:\*\*/.test(b));
const fm = { title, project, kind, group, order, description: plain(firstPara ?? ''), source: `https://github.com/${ORG}/${m.repo}/blob/main/${file}`, ...extra };
const yaml = Object.entries(fm).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join('\n');
await writeFile(new URL(`${project}/${kind}/${slug}.md`, OUT), `---\n${yaml}\n---\n${md.trimStart()}`);
console.log(`${project}/${kind}/${slug} ${title}`);
}
}
}
if (ONLY) process.exit(0);
// RustingScience: every posts/<slug>/index.md becomes /science/<slug>/. Front matter passes through as written.
const SCIENCE = 'RustingScience';
const POSTS = new URL('../src/content/posts/', import.meta.url);
const sciTree = (await get(`https://api.github.com/repos/${ORG}/${SCIENCE}/git/trees/main?recursive=1`, 'json')).tree.map((t) => t.path);
await rm(POSTS, { recursive: true, force: true });
await mkdir(POSTS, { recursive: true });
for (const file of sciTree.filter((p) => /^posts\/[^/]+\/index\.md$/.test(p))) {
const slug = file.split('/')[1];
const raw = await get(`https://raw.githubusercontent.com/${ORG}/${SCIENCE}/main/${file}`);
const [, fm, body] = raw.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/) ?? [];
if (!fm) throw new Error(`${file}: no front matter`);
const md = convert(body, file, SCIENCE, new Map(), new Set(sciTree));
const minutes = Math.max(1, Math.round(md.replace(/```[\s\S]*?```/g, '').split(/\s+/).length / 230));
const source = `https://github.com/${ORG}/${SCIENCE}/blob/main/${file}`;
await writeFile(new URL(`${slug}.md`, POSTS), `---\n${fm}\nminutes: ${minutes}\nsource: ${JSON.stringify(source)}\n---\n${md.trimStart()}`);
console.log(`science/${slug}`);
}