184 lines
11 KiB
JavaScript
184 lines
11 KiB
JavaScript
#!/usr/bin/env node
|
||
// Generates web/teaching/cases/production-line-course.html from course.json,
|
||
// per-episode scripts and the media directory. Missing media is marked pending.
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
|
||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||
const webRoot = path.resolve(here, "../..");
|
||
const course = JSON.parse(fs.readFileSync(path.join(here, "course.json"), "utf8"));
|
||
const mediaDir = path.join(webRoot, "teaching", "media", "line-course");
|
||
const outFile = path.join(webRoot, "teaching", "cases", "production-line-course.html");
|
||
|
||
const esc = value => String(value ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||
const mediaBase = `/media/line-course/`;
|
||
const has = suffix => episode => fs.existsSync(path.join(mediaDir, `${episode.id}-${episode.slug}${suffix}`));
|
||
|
||
const episodeHtml = episode => {
|
||
const scriptPath = path.join(here, "episodes", episode.id, "script.zh.json");
|
||
const lesson = fs.existsSync(scriptPath)
|
||
? JSON.parse(fs.readFileSync(scriptPath, "utf8")).lesson ?? {}
|
||
: {};
|
||
const base = `${mediaBase}${episode.id}-${episode.slug}`;
|
||
const hasVideo = has(".mp4")(episode);
|
||
const parameters = (lesson.parameters ?? []).map(item =>
|
||
`<tr><th>${esc(item.name)}</th><td>${esc(item.value)}</td></tr>`).join("");
|
||
const checklist = (lesson.checklist ?? []).map(item => `<li>${esc(item)}</li>`).join("");
|
||
const mistakes = (lesson.mistakes ?? []).map(item => `<li>${esc(item)}</li>`).join("");
|
||
const objectives = (lesson.objectives ?? []).map(item => `<li>${esc(item)}</li>`).join("");
|
||
const source = path.join(here, "checkpoints", `${episode.id}.FCStd`);
|
||
const copied = path.join(mediaDir, "checkpoints", `${episode.id}.FCStd`);
|
||
if (fs.existsSync(source)) {
|
||
fs.mkdirSync(path.dirname(copied), { recursive: true });
|
||
fs.copyFileSync(source, copied);
|
||
}
|
||
const checkpoint = fs.existsSync(copied)
|
||
? `<a class="asset" href="${mediaBase}checkpoints/${episode.id}.FCStd" download>下载 ${episode.id} 检查点 FCStd</a>`
|
||
: `<span class="asset muted">检查点:${esc(episode.checkpointOut ?? "无")}</span>`;
|
||
const stillSource = path.join(here, "stills", episode.id);
|
||
const stillFiles = fs.existsSync(stillSource)
|
||
? fs.readdirSync(stillSource).filter(name => name.endsWith(".png")).sort()
|
||
: [];
|
||
const stillTarget = path.join(mediaDir, "stills", episode.id);
|
||
if (stillFiles.length) {
|
||
fs.mkdirSync(stillTarget, { recursive: true });
|
||
for (const name of stillFiles) fs.copyFileSync(path.join(stillSource, name), path.join(stillTarget, name));
|
||
}
|
||
const stillsHtml = stillFiles.map(name =>
|
||
`<figure><img loading="lazy" src="${mediaBase}stills/${episode.id}/${name}" alt="${esc(episode.id)} ${esc(name)}" /><figcaption>${esc(name.replace(/\.png$/, ""))}</figcaption></figure>`).join("");
|
||
return `<article class="episode${hasVideo ? "" : " pending"}" id="${esc(episode.id)}" data-episode="${esc(episode.id)}">
|
||
<header>
|
||
<span class="badge">${esc(episode.id)}</span>
|
||
<h3>${esc(episode.title)}</h3>
|
||
<label class="done"><input type="checkbox" data-progress="${esc(episode.id)}" /> 已完成</label>
|
||
</header>
|
||
${hasVideo
|
||
? `<video controls preload="metadata" poster="${base}-poster.png" width="100%">
|
||
<source src="${base}.mp4" type="video/mp4" />
|
||
<source src="${base}.webm" type="video/webm" />
|
||
<track kind="subtitles" srclang="zh" label="中文" src="${base}.zh.vtt" default />
|
||
</video>
|
||
<p class="links"><a href="${base}.mp4" download>MP4</a> · <a href="${base}.webm" download>WebM</a> · <a href="${base}.zh.srt" download>SRT</a></p>`
|
||
: `<p class="pending-note">视频待制作(脚本与参数已就绪)。</p>`}
|
||
<details><summary>分步截图(${stillFiles.length})</summary><div class="stills">${stillsHtml}</div></details>
|
||
<details><summary>学习目标与参数表</summary>
|
||
<ul>${objectives}</ul>
|
||
<table>${parameters}</table>
|
||
</details>
|
||
<details><summary>自检清单</summary><ul>${checklist}</ul></details>
|
||
<details><summary>常见错误与练习</summary><ul>${mistakes}</ul><p>${esc(lesson.exercise ?? "")}</p></details>
|
||
<p>${checkpoint}</p>
|
||
</article>`;
|
||
};
|
||
|
||
const seasons = (course.seasons ?? []).map(season => {
|
||
const episodes = season.episodes.map(id => course.episodes.find(item => item.id === id)).filter(Boolean);
|
||
return `<section class="season" id="${esc(season.id)}">
|
||
<h2>${esc(season.id)} · ${esc(season.title)}</h2>
|
||
<div class="grid">${episodes.map(episodeHtml).join("\n")}</div>
|
||
</section>`;
|
||
}).join("\n");
|
||
|
||
const spec = JSON.parse(fs.readFileSync(path.join(here, "line-spec.json"), "utf8"));
|
||
const episodeOfModel = new Map();
|
||
for (const episode of course.episodes) for (const model of episode.models ?? []) episodeOfModel.set(model.name, episode.id);
|
||
const inventoryHtml = `<section class="season" id="inventory">
|
||
<h2>参考零件总表 · ${spec.modelCount} 个模型 / ${spec.partCount} 个教学部件</h2>
|
||
<table>
|
||
<thead><tr><th>模型</th><th>GLB 文件</th><th>集</th><th>部件</th><th>包围盒 mm</th></tr></thead>
|
||
<tbody>${spec.models.map(model => `<tr><td>${esc(model.label)}</td><td>${esc(model.file)}</td><td>${esc(episodeOfModel.get(model.name) ?? "-")}</td><td>${model.parts.length}</td><td>${model.overallDims.map(value => Math.round(value)).join(" × ")}</td></tr>`).join("")}</tbody>
|
||
</table>
|
||
</section>`;
|
||
|
||
const heroHtml = fs.existsSync(path.join(webRoot, "teaching", "cases", "production-line-assembly.html"))
|
||
? `<section class="hero">
|
||
<h2>整线场景:参考模型与重建结果</h2>
|
||
<div class="hero-grid">
|
||
<figure><a href="/cases/production-line-assembly.html"><img src="/cases/production-line-assembly.png" alt="按参考 GLB 重建的生产线场景" /></a><figcaption>重建结果:FreeCAD 27 件模型 · <a href="/cases/production-line-assembly.html">交互查看</a></figcaption></figure>
|
||
<figure><a href="/cases/production-line-reference.html"><img src="/cases/production-line-reference.png" alt="参考 GLB 产线场景" /></a><figcaption>参考模型 27 个 GLB · <a href="/cases/production-line-reference.html">交互查看</a></figcaption></figure>
|
||
</div>
|
||
</section>` : "";
|
||
|
||
const html = `<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<link rel="icon" href="data:," />
|
||
<title>${esc(course.title)}</title>
|
||
<style>
|
||
:root { color-scheme: dark; --bg: #0f1419; --panel: #17212b; --line: #2a3a45; --text: #dbe6ee; --muted: #93a6b3; --accent: #73c6d4; }
|
||
* { box-sizing: border-box; }
|
||
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.6 system-ui, "Noto Sans SC", sans-serif; }
|
||
header.top { position: sticky; top: 0; z-index: 2; background: #0b1116ee; border-bottom: 1px solid var(--line); padding: 12px 20px; backdrop-filter: blur(6px); }
|
||
header.top h1 { margin: 0 0 6px; font-size: 20px; }
|
||
.progress { height: 8px; background: var(--panel); border-radius: 4px; overflow: hidden; max-width: 520px; }
|
||
.progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
|
||
main { max-width: 1280px; margin: 0 auto; padding: 20px; }
|
||
.season h2 { margin: 28px 0 12px; font-size: 18px; color: var(--accent); }
|
||
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; }
|
||
.episode { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
|
||
.episode.pending { opacity: .75; border-style: dashed; }
|
||
.episode header { display: flex; align-items: center; gap: 8px; }
|
||
.episode h3 { margin: 0; font-size: 16px; flex: 1; }
|
||
.badge { background: #24414b; color: var(--accent); border-radius: 4px; padding: 1px 6px; font-size: 12px; }
|
||
.done { font-size: 12px; color: var(--muted); white-space: nowrap; }
|
||
video { background: #000; border-radius: 6px; aspect-ratio: 16 / 9; }
|
||
.links a, .asset { color: var(--accent); }
|
||
.muted { color: var(--muted); }
|
||
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
||
th, td { text-align: left; border-bottom: 1px solid var(--line); padding: 4px 6px; vertical-align: top; }
|
||
details { border-top: 1px solid var(--line); padding-top: 8px; font-size: 14px; }
|
||
.stills { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 8px; margin-top: 8px; }
|
||
.stills figure { margin: 0; background: #0d1318; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
|
||
.stills img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }
|
||
.stills figcaption { padding: 3px 6px; font-size: 11px; color: var(--muted); }
|
||
summary { cursor: pointer; color: var(--accent); }
|
||
ul { margin: 6px 0 6px 18px; padding: 0; }
|
||
@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } main { padding: 12px; } }
|
||
.hero { max-width: 1280px; margin: 18px auto 0; padding: 0 20px; }
|
||
.hero h2 { color: #73c6d4; font-size: 18px; margin: 0 0 10px; }
|
||
.hero-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
||
.hero figure { margin: 0; background: #17212b; border: 1px solid #2a3a45; border-radius: 10px; overflow: hidden; }
|
||
.hero img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: #0d1318; }
|
||
.hero figcaption { padding: 8px 12px; font-size: 13px; color: #93a6b3; }
|
||
.hero a { color: #73c6d4; }
|
||
@media (max-width: 760px) { .hero-grid { grid-template-columns: 1fr; } }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header class="top">
|
||
<h1>${esc(course.title)}</h1>
|
||
<div class="progress" aria-label="课程完成度"><span id="progress-bar"></span></div>
|
||
<p id="progress-text" class="muted" style="margin:6px 0 0;font-size:13px"></p>
|
||
</header>
|
||
<main>
|
||
${heroHtml}
|
||
${inventoryHtml}
|
||
${seasons}
|
||
</main>
|
||
<script>
|
||
const boxes = [...document.querySelectorAll('input[data-progress]')];
|
||
const key = 'line-course-progress-v1';
|
||
const load = () => { try { return JSON.parse(localStorage.getItem(key) || '{}'); } catch { return {}; } };
|
||
const save = value => localStorage.setItem(key, JSON.stringify(value));
|
||
const render = () => {
|
||
const done = boxes.filter(box => box.checked).length;
|
||
document.getElementById('progress-bar').style.width = boxes.length ? (100 * done / boxes.length) + '%' : '0';
|
||
document.getElementById('progress-text').textContent = done + ' / ' + boxes.length + ' 集已完成';
|
||
};
|
||
const state = load();
|
||
for (const box of boxes) {
|
||
box.checked = Boolean(state[box.dataset.progress]);
|
||
box.addEventListener('change', () => { const value = load(); value[box.dataset.progress] = box.checked; save(value); render(); });
|
||
}
|
||
render();
|
||
</script>
|
||
</body>
|
||
</html>
|
||
`;
|
||
|
||
fs.writeFileSync(outFile, html);
|
||
console.log(JSON.stringify({ ok: true, outFile, episodes: course.episodes.length }));
|