pi-scenes 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/assets/scene-prompts/learning/notes.md +20 -0
- package/assets/scene-skills/learning/course-book/SKILL.md +157 -0
- package/assets/scene-skills/learning/course-book/annotate.js +522 -0
- package/assets/scene-skills/learning/course-book/assets/book.css +1167 -0
- package/assets/scene-skills/learning/course-book/assets/paging.js +234 -0
- package/assets/scene-skills/learning/course-book/book.css +1167 -0
- package/assets/scene-skills/learning/course-book/build.py +277 -0
- package/assets/scene-skills/learning/course-book/paging.js +234 -0
- package/assets/scene-skills/learning/eli5/SKILL.md +26 -0
- package/package.json +35 -35
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""course-book 组装器 + 书桌服务器。
|
|
3
|
+
|
|
4
|
+
用法:
|
|
5
|
+
python3 build.py # 组装 chapters/*.html → dist/book.html(单文件,内联 CSS+JS)
|
|
6
|
+
python3 build.py --serve # 书桌模式:http://127.0.0.1:8765
|
|
7
|
+
# GET / 书稿(chapters/ 变动自动重建+浏览器热刷新)
|
|
8
|
+
# GET /annotations 标注库 JSON
|
|
9
|
+
# POST /annotations 新建/更新标注(AI 侧亦直接编辑此文件)
|
|
10
|
+
# GET /poll 构建时间戳(前端轮询热刷新)
|
|
11
|
+
碎片约定:
|
|
12
|
+
00-cover.html <section class="cover">…</section>
|
|
13
|
+
NN-slug.html <section class="chapter" id="chN" data-status="draft|final">…</section>
|
|
14
|
+
标注协议:W3C TextQuoteSelector 风格(anchor.exact/prefix/suffix/start/end + chapter),
|
|
15
|
+
type: question|suggest|bug|discuss|answer;disposition: sidebar|rewrite|chat(answer 固定)
|
|
16
|
+
status: open|resolved;resolved 时 resolution={at,summary}。
|
|
17
|
+
"""
|
|
18
|
+
import datetime
|
|
19
|
+
import json
|
|
20
|
+
import pathlib
|
|
21
|
+
import re
|
|
22
|
+
import sys
|
|
23
|
+
import threading
|
|
24
|
+
import time
|
|
25
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
26
|
+
|
|
27
|
+
root = pathlib.Path(__file__).resolve().parent
|
|
28
|
+
chapters_dir = root / "chapters"
|
|
29
|
+
ann_file = root / "annotations.json"
|
|
30
|
+
anno_js = pathlib.Path(__file__).with_name("annotate.js") # 与 build.py 同目录(技能资产)
|
|
31
|
+
dist_html = root / "dist" / "book.html"
|
|
32
|
+
|
|
33
|
+
# ---------------- 组装 ----------------
|
|
34
|
+
|
|
35
|
+
def read_css() -> str:
|
|
36
|
+
return (root / "assets" / "book.css").read_text(encoding="utf-8")
|
|
37
|
+
|
|
38
|
+
def read_paging_js() -> str:
|
|
39
|
+
p = root / "assets" / "paging.js"
|
|
40
|
+
return p.read_text(encoding="utf-8") if p.exists() else ""
|
|
41
|
+
|
|
42
|
+
def read_js() -> str:
|
|
43
|
+
js = anno_js.read_text(encoding="utf-8")
|
|
44
|
+
# 书籍目录里 annotate.js 可能已被复制到根目录(bootstrap 副本),优先用同目录版
|
|
45
|
+
local = root / "annotate.js"
|
|
46
|
+
if local.exists():
|
|
47
|
+
js = local.read_text(encoding="utf-8")
|
|
48
|
+
return js
|
|
49
|
+
|
|
50
|
+
def chapter_entries(fragments):
|
|
51
|
+
"""提取 (cid, kicker_module, title, draft, [subsections]) 列表"""
|
|
52
|
+
entries = []
|
|
53
|
+
for f2 in fragments:
|
|
54
|
+
t2 = f2.read_text(encoding="utf-8")
|
|
55
|
+
m2 = re.search(r'<section[^>]*class="chapter[^"]*"[^>]*id="([^"]+)"[^>]*data-status="([^"]+)"', t2)
|
|
56
|
+
if not m2:
|
|
57
|
+
continue
|
|
58
|
+
cid = m2.group(1)
|
|
59
|
+
kicker_match = re.search(r'<p class="kicker">(.*?)</p>', t2, re.S)
|
|
60
|
+
kicker = re.sub(r"<[^>]+>", "", kicker_match.group(1)).strip() if kicker_match else ""
|
|
61
|
+
|
|
62
|
+
h1 = re.search(r"<h1[^>]*>(.*?)</h1>", t2, re.S)
|
|
63
|
+
title = re.sub(r"<[^>]+>", "", h1.group(1)).strip() if h1 else cid
|
|
64
|
+
|
|
65
|
+
# 抽取本章下所有的 h2 小节
|
|
66
|
+
subsections = []
|
|
67
|
+
for h2_match in re.finditer(r'<h2(?:[^>]*id="([^"]+)")?[^>]*>(.*?)</h2>', t2, re.S):
|
|
68
|
+
sub_id = h2_match.group(1) or ""
|
|
69
|
+
sub_title = re.sub(r"<[^>]+>", "", h2_match.group(2)).strip()
|
|
70
|
+
if "本章小结" in sub_title or "毕业项目架构全景小结" in sub_title:
|
|
71
|
+
continue
|
|
72
|
+
subsections.append((sub_id, sub_title))
|
|
73
|
+
|
|
74
|
+
entries.append((cid, kicker, title, m2.group(2) != "final", subsections))
|
|
75
|
+
return entries
|
|
76
|
+
|
|
77
|
+
def build() -> float:
|
|
78
|
+
fragments = sorted(chapters_dir.glob("*.html"))
|
|
79
|
+
entries = chapter_entries(fragments)
|
|
80
|
+
|
|
81
|
+
# 按照模块 (kicker) 进行分组聚合
|
|
82
|
+
modules = {}
|
|
83
|
+
for cid, kicker, title, draft, subs in entries:
|
|
84
|
+
mod_key = kicker if kicker else "前置说明"
|
|
85
|
+
if mod_key not in modules:
|
|
86
|
+
modules[mod_key] = []
|
|
87
|
+
modules[mod_key].append((cid, title, draft, subs))
|
|
88
|
+
|
|
89
|
+
sidebar_parts = []
|
|
90
|
+
toc_parts = []
|
|
91
|
+
|
|
92
|
+
for mod_name, ch_list in modules.items():
|
|
93
|
+
# 侧边栏模块标题
|
|
94
|
+
sidebar_parts.append(f'<li class="nav-module-title">{mod_name}</li>')
|
|
95
|
+
# 正式目录模块块
|
|
96
|
+
toc_module_chapters = []
|
|
97
|
+
|
|
98
|
+
for cid, title, draft, subs in ch_list:
|
|
99
|
+
sidebar_parts.append(f'<li class="nav-chapter-item"><a href="#{cid}">{title}</a></li>')
|
|
100
|
+
|
|
101
|
+
sub_html = ""
|
|
102
|
+
if subs:
|
|
103
|
+
sub_lines = []
|
|
104
|
+
for sid, stitle in subs:
|
|
105
|
+
href = f"#{sid}" if sid else f"#{cid}"
|
|
106
|
+
sub_lines.append(f'<li class="toc-sub"><a href="{href}"><span class="toc-title">{stitle}</span><span class="toc-leader"></span><span class="toc-page"></span></a></li>')
|
|
107
|
+
sub_html = f'<ul class="toc-sublist">\n' + "\n".join(sub_lines) + '\n</ul>'
|
|
108
|
+
|
|
109
|
+
toc_module_chapters.append(
|
|
110
|
+
f'<li class="toc-chapter"><a href="#{cid}"><span class="toc-title">{title}</span><span class="toc-leader"></span><span class="toc-page"></span></a>{sub_html}</li>'
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
toc_parts.append(
|
|
114
|
+
f'<li class="toc-module"><div class="toc-module-header">{mod_name}</div><ol class="toc-chapter-list">\n'
|
|
115
|
+
+ "\n".join(toc_module_chapters) + '\n</ol></li>'
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
sidebar = ('<nav class="book-nav" id="book-nav"><h2 class="book-nav-title">目录树导航</h2><ul class="nav-tree">\n'
|
|
119
|
+
+ "\n".join(sidebar_parts) + '\n</ul></nav>') if sidebar_parts else ""
|
|
120
|
+
|
|
121
|
+
body_parts, toc_done = [], False
|
|
122
|
+
for frag in fragments:
|
|
123
|
+
text = frag.read_text(encoding="utf-8")
|
|
124
|
+
m = re.search(r'<section[^>]*class="chapter[^"]*"[^>]*id="([^"]+)"[^>]*data-status="([^"]+)"', text)
|
|
125
|
+
if m and not toc_done:
|
|
126
|
+
body_parts.append('<section class="front-matter"><nav class="toc-full" id="toc"><h1 class="toc-heading">全书架构大纲 · Contents</h1><ol class="toc-module-list">\n'
|
|
127
|
+
+ "\n".join(toc_parts) + "\n</ol></nav></section>")
|
|
128
|
+
toc_done = True
|
|
129
|
+
body_parts.append(text)
|
|
130
|
+
|
|
131
|
+
stamp = datetime.date.today().isoformat()
|
|
132
|
+
doc = f"""<!doctype html>
|
|
133
|
+
<html lang="zh-CN">
|
|
134
|
+
<head>
|
|
135
|
+
<meta charset="utf-8">
|
|
136
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
137
|
+
<title>{root.name}</title>
|
|
138
|
+
<style>
|
|
139
|
+
{read_css()}
|
|
140
|
+
</style>
|
|
141
|
+
</head>
|
|
142
|
+
<body>
|
|
143
|
+
<div class="book">
|
|
144
|
+
<p class="build-bar">built {stamp} · course-book</p>
|
|
145
|
+
{sidebar}
|
|
146
|
+
{chr(10).join(body_parts)}
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
<div class="page-controller" id="pageController">
|
|
150
|
+
<button class="page-btn" id="prevPageBtn" title="上一页 (键盘 ←)">◀ 上一页</button>
|
|
151
|
+
<span class="page-info" id="pageInfo">加载中...</span>
|
|
152
|
+
<button class="page-btn" id="nextPageBtn" title="下一页 (键盘 →)">下一页 ▶</button>
|
|
153
|
+
<button class="page-mode-toggle" id="modeToggleBtn">切换为长卷轴</button>
|
|
154
|
+
</div>
|
|
155
|
+
|
|
156
|
+
<script>
|
|
157
|
+
{read_js()}
|
|
158
|
+
</script>
|
|
159
|
+
<script>
|
|
160
|
+
{read_paging_js()}
|
|
161
|
+
</script>
|
|
162
|
+
</body>
|
|
163
|
+
</html>
|
|
164
|
+
"""
|
|
165
|
+
dist_html.parent.mkdir(exist_ok=True)
|
|
166
|
+
tmp = dist_html.with_suffix(".tmp")
|
|
167
|
+
tmp.write_text(doc, encoding="utf-8")
|
|
168
|
+
tmp.replace(dist_html) # 原子替换,避免热刷新读到半成品
|
|
169
|
+
return len(fragments)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def status_line() -> str:
|
|
173
|
+
fragments = sorted(chapters_dir.glob("*.html"))
|
|
174
|
+
lines, n_draft = [], 0
|
|
175
|
+
for f in fragments:
|
|
176
|
+
text = f.read_text(encoding="utf-8")
|
|
177
|
+
m = re.search(r'data-status="([^"]+)"', text)
|
|
178
|
+
st = m.group(1) if m else "?"
|
|
179
|
+
n_draft += st not in ("final", "?")
|
|
180
|
+
h1 = re.search(r"<h1[^>]*>(.*?)</h1>", text, re.S)
|
|
181
|
+
title = re.sub(r"<[^>]+>", "", h1.group(1)).strip()[:30] if h1 else "(无标题)"
|
|
182
|
+
lines.append(f" {f.name:<28} {st:<6} {title}")
|
|
183
|
+
return f"✓ {len(fragments)} fragments -> {dist_html.relative_to(root)} (draft 章节: {n_draft})\n" + "\n".join(lines)
|
|
184
|
+
|
|
185
|
+
# ---------------- 标注库 ----------------
|
|
186
|
+
|
|
187
|
+
def load_annos() -> list:
|
|
188
|
+
if ann_file.exists():
|
|
189
|
+
return json.loads(ann_file.read_text(encoding="utf-8"))
|
|
190
|
+
return []
|
|
191
|
+
|
|
192
|
+
def save_annos(items: list) -> None:
|
|
193
|
+
tmp = ann_file.with_suffix(".tmp")
|
|
194
|
+
tmp.write_text(json.dumps(items, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
195
|
+
tmp.replace(ann_file)
|
|
196
|
+
|
|
197
|
+
# ---------------- 书桌服务器 ----------------
|
|
198
|
+
|
|
199
|
+
class Desk(BaseHTTPRequestHandler):
|
|
200
|
+
def log_message(self, *a): # 安静模式
|
|
201
|
+
pass
|
|
202
|
+
|
|
203
|
+
def _send(self, code: int, body: bytes, ctype: str):
|
|
204
|
+
self.send_response(code)
|
|
205
|
+
self.send_header("Content-Type", ctype)
|
|
206
|
+
self.send_header("Content-Length", str(len(body)))
|
|
207
|
+
self.send_header("Cache-Control", "no-store")
|
|
208
|
+
self.end_headers()
|
|
209
|
+
self.wfile.write(body)
|
|
210
|
+
|
|
211
|
+
def do_GET(self):
|
|
212
|
+
if self.path.startswith("/annotations"):
|
|
213
|
+
body = json.dumps(load_annos(), ensure_ascii=False).encode("utf-8")
|
|
214
|
+
self._send(200, body, "application/json; charset=utf-8")
|
|
215
|
+
elif self.path.startswith("/poll"):
|
|
216
|
+
self._send(200, json.dumps({"stamp": dist_html.stat().st_mtime}).encode(), "application/json")
|
|
217
|
+
elif self.path in ("/", "/index.html", "/book.html"):
|
|
218
|
+
self._send(200, dist_html.read_bytes(), "text/html; charset=utf-8")
|
|
219
|
+
else:
|
|
220
|
+
self._send(404, b"not found", "text/plain")
|
|
221
|
+
|
|
222
|
+
def do_POST(self):
|
|
223
|
+
if not self.path.startswith("/annotations"):
|
|
224
|
+
return self._send(404, b"not found", "text/plain")
|
|
225
|
+
n = int(self.headers.get("Content-Length", 0))
|
|
226
|
+
item = json.loads(self.rfile.read(n) or b"{}")
|
|
227
|
+
items = load_annos()
|
|
228
|
+
if not item.get("id"):
|
|
229
|
+
item["id"] = "a-" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S-") + f"{len(items)+1:03d}"
|
|
230
|
+
items.append(item)
|
|
231
|
+
else:
|
|
232
|
+
for i, x in enumerate(items):
|
|
233
|
+
if x.get("id") == item["id"]:
|
|
234
|
+
items[i] = {**x, **item}
|
|
235
|
+
item = items[i]
|
|
236
|
+
break
|
|
237
|
+
else:
|
|
238
|
+
items.append(item)
|
|
239
|
+
save_annos(items)
|
|
240
|
+
self._send(200, json.dumps(item, ensure_ascii=False).encode(), "application/json; charset=utf-8")
|
|
241
|
+
|
|
242
|
+
def watch_and_rebuild(stop: threading.Event):
|
|
243
|
+
while not stop.wait(0.7):
|
|
244
|
+
try:
|
|
245
|
+
newest = max(p.stat().st_mtime for p in
|
|
246
|
+
[*chapters_dir.glob("*.html"), root / "assets" / "book.css",
|
|
247
|
+
*( [root / "annotate.js"] if (root / "annotate.js").exists() else [] )])
|
|
248
|
+
if newest > dist_html.stat().st_mtime - 0.2:
|
|
249
|
+
if newest > getattr(watch_and_rebuild, "last", 0):
|
|
250
|
+
n = build()
|
|
251
|
+
watch_and_rebuild.last = newest
|
|
252
|
+
print(f"↻ 检测到章节/样式变动,已重建({n} fragments)")
|
|
253
|
+
except FileNotFoundError:
|
|
254
|
+
pass
|
|
255
|
+
|
|
256
|
+
def serve(port: int = 8765):
|
|
257
|
+
build()
|
|
258
|
+
stop = threading.Event()
|
|
259
|
+
threading.Thread(target=watch_and_rebuild, args=(stop,), daemon=True).start()
|
|
260
|
+
httpd = ThreadingHTTPServer(("127.0.0.1", port), Desk)
|
|
261
|
+
print(f"📖 书桌已开张 → http://127.0.0.1:{port}")
|
|
262
|
+
print(" 划选文字/双击段落即可备注;练习块点「在此作答」;Ctrl+C 收摊")
|
|
263
|
+
try:
|
|
264
|
+
httpd.serve_forever()
|
|
265
|
+
except KeyboardInterrupt:
|
|
266
|
+
stop.set()
|
|
267
|
+
print("\nbye")
|
|
268
|
+
|
|
269
|
+
if __name__ == "__main__":
|
|
270
|
+
if "--serve" in sys.argv:
|
|
271
|
+
port = 8765
|
|
272
|
+
if "--port" in sys.argv:
|
|
273
|
+
port = int(sys.argv[sys.argv.index("--port") + 1])
|
|
274
|
+
serve(port)
|
|
275
|
+
else:
|
|
276
|
+
build()
|
|
277
|
+
print(status_line())
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
|
|
2
|
+
// ==========================================================================
|
|
3
|
+
// 实体书真·A4 流式拆页与代码防跨页保护引擎 (True A4 Reflow & Anti-Split Engine)
|
|
4
|
+
// ==========================================
|
|
5
|
+
(function() {
|
|
6
|
+
const MAX_A4_BODY_HEIGHT = 970; // A4 净版心可用像素高度 (1123px - 页眉28px - 页脚25px - 留白)
|
|
7
|
+
let a4Pages = [];
|
|
8
|
+
let currentPageIdx = 0;
|
|
9
|
+
let isPagedMode = localStorage.getItem('course_book_paged_mode') !== 'false';
|
|
10
|
+
|
|
11
|
+
const prevBtn = document.getElementById('prevPageBtn');
|
|
12
|
+
const nextBtn = document.getElementById('nextPageBtn');
|
|
13
|
+
const pageInfo = document.getElementById('pageInfo');
|
|
14
|
+
const modeBtn = document.getElementById('modeToggleBtn');
|
|
15
|
+
const bookContainer = document.querySelector('.book');
|
|
16
|
+
|
|
17
|
+
// 创建一个空白的标准 A4 纸张 DOM
|
|
18
|
+
function createA4PageDOM(pageNum, moduleName, chapterTitle) {
|
|
19
|
+
const page = document.createElement('div');
|
|
20
|
+
page.className = `a4-page ${pageNum % 2 === 0 ? 'even-page' : 'odd-page'}`;
|
|
21
|
+
page.dataset.pageNum = pageNum;
|
|
22
|
+
|
|
23
|
+
// 页眉 (Running Header:单行精炼版)
|
|
24
|
+
// 模块名精炼:若带冒号,截取前半部分作为短模块名 (例如: 模块 5 · 上量部署)
|
|
25
|
+
let shortModule = moduleName || '随课生长深度实战';
|
|
26
|
+
if (shortModule.includes(':')) shortModule = shortModule.split(':')[0].trim();
|
|
27
|
+
else if (shortModule.includes(':')) shortModule = shortModule.split(':')[0].trim();
|
|
28
|
+
|
|
29
|
+
const header = document.createElement('header');
|
|
30
|
+
header.className = 'a4-header';
|
|
31
|
+
header.innerHTML = `
|
|
32
|
+
<span class="hdr-left"><span class="hdr-module">${shortModule}</span><span class="hdr-sep">•</span><span class="hdr-chapter">${chapterTitle || ''}</span></span>
|
|
33
|
+
<span class="hdr-right">随课生长实战系列 · Feng-H</span>
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
// 净版心内容区
|
|
37
|
+
const body = document.createElement('div');
|
|
38
|
+
body.className = 'a4-body';
|
|
39
|
+
|
|
40
|
+
// 页脚 (Running Footer)
|
|
41
|
+
const footer = document.createElement('footer');
|
|
42
|
+
footer.className = 'a4-footer';
|
|
43
|
+
footer.innerHTML = `
|
|
44
|
+
<span class="ftr-left">通过 Google Colab / Kaggle 学习模型微调</span>
|
|
45
|
+
<span class="ftr-page-num">— 第 ${pageNum} 页 —</span>
|
|
46
|
+
<span class="ftr-right">Feng-H 著</span>
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
page.appendChild(header);
|
|
50
|
+
page.appendChild(body);
|
|
51
|
+
page.appendChild(footer);
|
|
52
|
+
return { page, body };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 核心拆页排版主程序
|
|
56
|
+
function reflowBookIntoA4() {
|
|
57
|
+
const rawChapters = Array.from(document.querySelectorAll('.chapter'));
|
|
58
|
+
const covers = Array.from(document.querySelectorAll('.cover, .front-matter'));
|
|
59
|
+
|
|
60
|
+
// 如果已经拆过,不再重复拆
|
|
61
|
+
if (document.querySelectorAll('.a4-page').length > 0) return;
|
|
62
|
+
|
|
63
|
+
let globalPageNum = 1;
|
|
64
|
+
a4Pages = [];
|
|
65
|
+
|
|
66
|
+
// 1. 封面与扉页作为专属 A4 首页
|
|
67
|
+
covers.forEach(c => {
|
|
68
|
+
const a4 = document.createElement('div');
|
|
69
|
+
a4.className = `a4-page ${globalPageNum % 2 === 0 ? 'even-page' : 'odd-page'} front-a4-page`;
|
|
70
|
+
a4.dataset.pageNum = globalPageNum;
|
|
71
|
+
|
|
72
|
+
// 封面/扉页内嵌原始内容
|
|
73
|
+
const cloned = c.cloneNode(true);
|
|
74
|
+
cloned.style.display = 'block';
|
|
75
|
+
cloned.style.margin = '0';
|
|
76
|
+
cloned.style.border = 'none';
|
|
77
|
+
cloned.style.boxShadow = 'none';
|
|
78
|
+
cloned.style.padding = '0';
|
|
79
|
+
a4.appendChild(cloned);
|
|
80
|
+
|
|
81
|
+
// 隐藏原容器
|
|
82
|
+
c.style.display = 'none';
|
|
83
|
+
bookContainer.appendChild(a4);
|
|
84
|
+
|
|
85
|
+
a4Pages.push({
|
|
86
|
+
element: a4,
|
|
87
|
+
pageNum: globalPageNum,
|
|
88
|
+
title: c.querySelector('h1, h2')?.innerText?.trim() || '封面/扉页'
|
|
89
|
+
});
|
|
90
|
+
globalPageNum++;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// 2. 正文各章流式装订
|
|
94
|
+
rawChapters.forEach(chap => {
|
|
95
|
+
// 提取模块名与章节名
|
|
96
|
+
const moduleName = chap.querySelector('.kicker')?.innerText?.trim() || '';
|
|
97
|
+
const chapterTitle = chap.querySelector('h1')?.innerText?.trim() || '';
|
|
98
|
+
const chapId = chap.id;
|
|
99
|
+
|
|
100
|
+
// 提取所有顶层子元素
|
|
101
|
+
const children = Array.from(chap.children).filter(el => !el.classList.contains('kicker'));
|
|
102
|
+
|
|
103
|
+
// 初始化本章第一张 A4 纸
|
|
104
|
+
let currentA4 = createA4PageDOM(globalPageNum, moduleName, chapterTitle);
|
|
105
|
+
bookContainer.appendChild(currentA4.page);
|
|
106
|
+
a4Pages.push({
|
|
107
|
+
element: currentA4.page,
|
|
108
|
+
pageNum: globalPageNum,
|
|
109
|
+
title: chapterTitle,
|
|
110
|
+
chapterId: chapId
|
|
111
|
+
});
|
|
112
|
+
globalPageNum++;
|
|
113
|
+
|
|
114
|
+
// 逐个元素流式排版
|
|
115
|
+
children.forEach(child => {
|
|
116
|
+
// 判断是否为原子不可拆分块(代码块、公式卡片、图片、表格、练习盒)
|
|
117
|
+
const isAtomic = child.matches('pre, .example, .figure, .tablewrap, .formula-block, .lo, .summary, .callout');
|
|
118
|
+
const cloned = child.cloneNode(true);
|
|
119
|
+
|
|
120
|
+
currentA4.body.appendChild(cloned);
|
|
121
|
+
|
|
122
|
+
// 测量当前 A4 版心是否溢出
|
|
123
|
+
if (currentA4.body.scrollHeight > MAX_A4_BODY_HEIGHT) {
|
|
124
|
+
// 溢出了!从当前页移除
|
|
125
|
+
currentA4.body.removeChild(cloned);
|
|
126
|
+
|
|
127
|
+
// 开启新一张 A4 纸!
|
|
128
|
+
currentA4 = createA4PageDOM(globalPageNum, moduleName, chapterTitle);
|
|
129
|
+
bookContainer.appendChild(currentA4.page);
|
|
130
|
+
a4Pages.push({
|
|
131
|
+
element: currentA4.page,
|
|
132
|
+
pageNum: globalPageNum,
|
|
133
|
+
title: chapterTitle,
|
|
134
|
+
chapterId: chapId,
|
|
135
|
+
targetId: child.id || ''
|
|
136
|
+
});
|
|
137
|
+
globalPageNum++;
|
|
138
|
+
|
|
139
|
+
// 将该元素作为新页面的首个元素放入
|
|
140
|
+
currentA4.body.appendChild(cloned);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// 隐藏未拆排的原始长章节
|
|
145
|
+
chap.classList.add('chapter-raw-source');
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 3. 视图与翻页切换控制
|
|
150
|
+
function updateView() {
|
|
151
|
+
if (isPagedMode) {
|
|
152
|
+
document.body.classList.add('paged-mode');
|
|
153
|
+
a4Pages.forEach((p, idx) => {
|
|
154
|
+
if (idx === currentPageIdx) {
|
|
155
|
+
p.element.classList.add('active-page');
|
|
156
|
+
p.element.style.display = 'flex';
|
|
157
|
+
} else {
|
|
158
|
+
p.element.classList.remove('active-page');
|
|
159
|
+
p.element.style.display = 'none';
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if (prevBtn) prevBtn.disabled = currentPageIdx === 0;
|
|
164
|
+
if (nextBtn) nextBtn.disabled = currentPageIdx === a4Pages.length - 1;
|
|
165
|
+
|
|
166
|
+
const cur = a4Pages[currentPageIdx];
|
|
167
|
+
let displayTitle = cur ? cur.title : '';
|
|
168
|
+
if (displayTitle.length > 20) displayTitle = displayTitle.substring(0, 20) + '...';
|
|
169
|
+
|
|
170
|
+
if (pageInfo) {
|
|
171
|
+
pageInfo.innerText = `[${cur ? cur.pageNum : 1} / ${a4Pages.length} 页] ${displayTitle}`;
|
|
172
|
+
}
|
|
173
|
+
if (modeBtn) modeBtn.innerText = "切换为长卷轴";
|
|
174
|
+
window.scrollTo({ top: 0, behavior: 'instant' });
|
|
175
|
+
} else {
|
|
176
|
+
document.body.classList.remove('paged-mode');
|
|
177
|
+
a4Pages.forEach(p => {
|
|
178
|
+
p.element.classList.remove('active-page');
|
|
179
|
+
p.element.style.display = 'flex';
|
|
180
|
+
});
|
|
181
|
+
if (pageInfo) pageInfo.innerText = `长卷轴模式 (共 ${a4Pages.length} 页)`;
|
|
182
|
+
if (modeBtn) modeBtn.innerText = "切换为单页翻书";
|
|
183
|
+
if (prevBtn) prevBtn.disabled = true;
|
|
184
|
+
if (nextBtn) nextBtn.disabled = true;
|
|
185
|
+
}
|
|
186
|
+
localStorage.setItem('course_book_paged_mode', isPagedMode);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function goToPage(idx) {
|
|
190
|
+
if (idx < 0 || idx >= a4Pages.length) return;
|
|
191
|
+
currentPageIdx = idx;
|
|
192
|
+
updateView();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 4. 事件监听与键盘左右翻页
|
|
196
|
+
if (prevBtn) prevBtn.onclick = () => goToPage(currentPageIdx - 1);
|
|
197
|
+
if (nextBtn) nextBtn.onclick = () => goToPage(currentPageIdx + 1);
|
|
198
|
+
|
|
199
|
+
if (modeBtn) modeBtn.onclick = () => {
|
|
200
|
+
isPagedMode = !isPagedMode;
|
|
201
|
+
updateView();
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
window.addEventListener('keydown', (e) => {
|
|
205
|
+
if (!isPagedMode) return;
|
|
206
|
+
if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) return;
|
|
207
|
+
if (e.key === 'ArrowLeft' || e.key === 'PageUp') {
|
|
208
|
+
goToPage(currentPageIdx - 1);
|
|
209
|
+
} else if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') {
|
|
210
|
+
if (e.key === ' ') e.preventDefault();
|
|
211
|
+
goToPage(currentPageIdx + 1);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// 5. 目录与侧边栏锚点拦截:点击任意链接精准定位到具体的 A4 纸!
|
|
216
|
+
document.querySelectorAll('a[href^="#"]').forEach(a => {
|
|
217
|
+
a.addEventListener('click', (e) => {
|
|
218
|
+
const targetId = a.getAttribute('href').substring(1);
|
|
219
|
+
if (!targetId) return;
|
|
220
|
+
|
|
221
|
+
const pIdx = a4Pages.findIndex(p => p.chapterId === targetId || p.targetId === targetId);
|
|
222
|
+
if (pIdx !== -1) {
|
|
223
|
+
if (isPagedMode) {
|
|
224
|
+
e.preventDefault();
|
|
225
|
+
goToPage(pIdx);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// 执行 A4 流式拆页并渲染
|
|
232
|
+
reflowBookIntoA4();
|
|
233
|
+
updateView();
|
|
234
|
+
})();
|
|
@@ -33,3 +33,29 @@ license: MIT
|
|
|
33
33
|
- 类比链超过一层(用 A 比 B,再用 B 比 C,用户会在第二层丢失)
|
|
34
34
|
- 省略反例(没有反例的类比必然过度泛化)
|
|
35
35
|
- 「就是这样,记住就行」——记不住的,能记住的只有锚点
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## 🎯 工业级认知对齐:比喻三分法(终版铁律)
|
|
40
|
+
|
|
41
|
+
在技术教学与认知引导中,严禁“满篇胡乱打比方”造成理解失真,必须严格执行三阶段推进:
|
|
42
|
+
|
|
43
|
+
1. **第一阶段:抽象概念首次亮相(直觉建立)**
|
|
44
|
+
- 必须“生活化比喻 + 学术术语”并提并列;
|
|
45
|
+
- 作用:用已有的现实生活物理直觉(如吉他效果器踏板、乐队排练、餐馆后厨双缓冲)迅速拉近距离,击碎对黑盒的恐惧;
|
|
46
|
+
- 禁忌:严禁一词兼职两个不同物理对象(例如不可将 Hugging Face 既比作音乐大厅又比作谱子,必须拆解为市场与分轨数据)。
|
|
47
|
+
|
|
48
|
+
2. **第二阶段:深度机理推导与架构分析(理性归位)**
|
|
49
|
+
- 必须以正式术语为主(如梯度累加、信息熵贝叶斯下限、注意力头、低秩分解);
|
|
50
|
+
- 作用:把第一阶段建立的感性直觉,平稳移交到严肃的计算机科学与数学定义上,不再停留于比喻表面。
|
|
51
|
+
|
|
52
|
+
3. **第三阶段:代码实操、参数调试与异常复盘(平实直说)**
|
|
53
|
+
- 严格平实陈述操作逻辑、真实显存数值、命令行报错与配置文件;
|
|
54
|
+
- 严禁打哑谜或在代码注释中堆砌修辞,确保读者复制即可运行、排障一针见血。
|
|
55
|
+
|
|
56
|
+
## 🛡️ 诚实教学纪律(防翻车六诫)
|
|
57
|
+
1. 严禁假托「前文提过」打哈哈,没写过就是没写过;
|
|
58
|
+
2. 前置章节没教过就直接考核 = 重大教学事故;
|
|
59
|
+
3. 新增概念时,必须反向检查前序章节是否具备完整的术语引入链条;
|
|
60
|
+
4. 教学预估与理论落空时,诚实认账记录原因;
|
|
61
|
+
5. 全书公式语义化渲染,严禁裸露未编译 LaTeX 字符。
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "pi-scenes",
|
|
3
|
+
"version": "0.12.0",
|
|
4
|
+
"description": "Scene switcher for pi: bundle extensions, skills, and prompt templates into a common layer + per-scene layers. /scene hot-switches with live reload (project-scoped, zero settings residue); scene-scoped workflow templates arrive via resources_discover with no config writes; scene definitions self-evolve from real usage (tool-call attribution + session-end LLM reflection).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Feng-H",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/Feng-H/pi-scenes.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/Feng-H/pi-scenes/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/Feng-H/pi-scenes#readme",
|
|
19
|
+
"files": [
|
|
20
|
+
"extensions",
|
|
21
|
+
"assets",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "node --test test/"
|
|
27
|
+
},
|
|
28
|
+
"pi": {
|
|
29
|
+
"extensions": [
|
|
30
|
+
"./extensions"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
35
|
+
"@earendil-works/pi-ai": "*"
|
|
36
|
+
}
|
|
37
37
|
}
|