From 75492f910e6f7d18c69e64efccffb257181d755f Mon Sep 17 00:00:00 2001 From: Zhao Chen Date: Thu, 8 Oct 2026 11:36:08 +0800 Subject: [PATCH] =?UTF-8?q?EPUB=EF=BC=9A=E6=8A=8A=E8=B6=85=E9=95=BF?= =?UTF-8?q?=E7=9A=84=E6=94=B6=E7=9B=8A=E3=80=81=E5=A4=87=E6=B3=A8=E3=80=81?= =?UTF-8?q?=E6=9D=A5=E6=BA=90=E6=A0=8F=E6=8B=86=E6=88=90=E5=AD=90=E6=9D=A1?= =?UTF-8?q?=E7=9B=AE=EF=BC=8C=E6=96=B9=E4=BE=BF=E5=9C=A8=E9=98=85=E8=AF=BB?= =?UTF-8?q?=E5=99=A8=E9=87=8C=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 电子书里不少「收益」一条就是一整段一千多字。比如第 31 节第 1 条,把十二条路的 门槛(兵役法、公务员录用规定、消防员招录办法……)全写在一行里,阅读器里就是一整块, 找不到哪句讲的是哪条路。 新增 tools/lib/split-items.mjs,只在生成 EPUB 时处理,正文 md 一个字不动: - 收益、备注:超过 300 字的,在句号处、且下一句开始引用新的依据时断开 (「X看《…》」「《某条例》」「刑法第…条」「另一项试验」「案例二」「芬兰…研究」等)。 没有明确换话题点的长段不硬拆。 - 来源:一篇文献一行,按「<网址>;」后面接下一条来切。 - 计括号层级时跳过「」《》()和 里面的标点,「P<0.0001」这种不当成括号。 效果:742 条超过 300 字的收益/备注/来源里拆了 413 条。44 个源文件拆前拆后 去掉列表符号逐字比对一致(来源栏文献之间的「;」换成了换行)。epubcheck 5.4.0 无错误无警告。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01X6s254TjQjvF9YgR8uK1LE --- tools/epub/build.mjs | 5 ++- tools/lib/split-items.mjs | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tools/lib/split-items.mjs diff --git a/tools/epub/build.mjs b/tools/epub/build.mjs index 43e0722..65201dc 100644 --- a/tools/epub/build.mjs +++ b/tools/epub/build.mjs @@ -5,6 +5,7 @@ import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'; import { resolve, dirname, posix } from 'node:path'; import { deflateRawSync } from 'node:zlib'; import { Marked, Tokenizer } from 'marked'; +import { splitLongItems } from '../lib/split-items.mjs'; import { ROOT, REPO, SITE, TITLE, read, readBook, gitCommit, buildStamp, stripBackLink } from '../lib/book.mjs'; const OUT = resolve(ROOT, process.argv[2] ?? 'dist/HowToLiveBetter.epub'); @@ -26,8 +27,8 @@ const contentsMd = book.contentsMd.replace(/^## 目录/, '# 各节简介'); const pages = [ { file: 'front.xhtml', src: 'README.md', title: '前言', md: `# ${TITLE}\n\n${description}\n\n${frontMd}` }, { file: 'contents.xhtml', src: 'README.md', title: '各节简介', md: contentsMd }, - ...bookFiles.map((src, i) => ({ file: `ch${String(i + 1).padStart(2, '0')}.xhtml`, src, md: stripBackLink(read(src)) })), - ...docFiles.map((src, i) => ({ file: `doc${i + 1}.xhtml`, src, md: stripBackLink(read(src)) })), + ...bookFiles.map((src, i) => ({ file: `ch${String(i + 1).padStart(2, '0')}.xhtml`, src, md: splitLongItems(stripBackLink(read(src))) })), + ...docFiles.map((src, i) => ({ file: `doc${i + 1}.xhtml`, src, md: splitLongItems(stripBackLink(read(src))) })), { file: 'about.xhtml', src: 'README.md', title: '版本说明', md: aboutMd() }, ]; const pageByPath = new Map(pages.map(p => [p.src, p.file])); diff --git a/tools/lib/split-items.mjs b/tools/lib/split-items.mjs new file mode 100644 index 0000000..1ce1158 --- /dev/null +++ b/tools/lib/split-items.mjs @@ -0,0 +1,92 @@ +// 把一行写到底的超长「收益 / 备注 / 来源」列表项,在换话题、换出处的地方拆成子条目。 +// 只改版式,不动正文的字:拆出来的子条目按顺序拼回去,和原来那一行逐字相同(来源栏里文献之间那个「;」换成了换行)。 +// 起因是电子书里一条「收益」常常一整段一千多字(比如第 31 节第 1 条把十二条路的门槛写在一行里), +// 在阅读器里就是一整块,找不到哪句讲哪条路。目前只接在 EPUB 构建里;PDF 想用的话同样在 read 之后套一层。 + +const MIN_LEN = 300; // 短于这个长度的项不拆 +const MIN_CHUNK = 25; // 拆出来太短的块并回上一块 + +// 句号之后,下一句以这些开头,就当作换了一条依据 +const TOPIC_START = [ + /^[^,。「」《》:;()]{1,12}看(《|[^,。「」]{0,10}法[:「(第])/, // 当兵看兵役法:/ 考公务员看《…》 + /^《[^》]{2,40}》/, // 《某条例》…… + /^[一-龥]{1,12}法(第[一二三四五六七八九十百零]+条|「)/, // 刑法第…条 / 义务教育法「 + /^(另一|另有一|还有一|再一|又一)(项|篇)/, // 另一项试验 + /^(国内|国外|国内外)也有/, + /^案例[一二三四五六七八九十]/, + /^(同一天|同一试验|同一指南|同一研究)/, + /^(美国|英国|欧洲|世界卫生组织|WHO|中国)[^,。]{0,20}(指南|共识|建议|学会)/, + /^(先说|再说|最后说|先看|再看|最后看)/, + /^一(项|篇)[^,。]{0,20}(试验|研究|综述|分析|调查)/, + /^(芬兰|北欧|英国|美国|日本|韩国|瑞典|丹麦|挪威|德国|法国|澳大利亚|加拿大|荷兰|台湾|香港|上海|北京)[^,。]{0,25}(研究|数据库|试验|队列|调查)/, +]; + +function splitTopLevel(text, sep, keepSep) { + // 在不处于 「」《》()() <> 里面的位置按 sep 拆 + const out = []; + let depth = 0, start = 0; + const open = '「《((', close = '」》))'; + for (let i = 0; i < text.length; i++) { + const c = text[i]; + if (c === '<' && /^', i); + if (j > 0) { i = j; continue; } + } + if (open.includes(c)) depth++; + else if (close.includes(c)) depth = Math.max(0, depth - 1); + else if (c === sep && depth === 0) { + out.push(text.slice(start, keepSep ? i + 1 : i)); + start = i + 1; + } + } + out.push(text.slice(start)); + return out.filter(s => s.trim()); +} + +function mergeShort(chunks) { + const out = []; + for (const c of chunks) { + if (out.length && c.length < MIN_CHUNK) out[out.length - 1] += c; + else out.push(c); + } + // 第一块也太短就并到第二块前面 + if (out.length > 1 && out[0].length < MIN_CHUNK) out.splice(0, 2, out[0] + out[1]); + return out; +} + +function splitBenefit(body) { + const sentences = splitTopLevel(body, '。', true); + const chunks = []; + for (const s of sentences) { + const t = s.trimStart(); + if (chunks.length && TOPIC_START.some(re => re.test(t))) chunks.push(t); + else if (chunks.length) chunks[chunks.length - 1] += s; + else chunks.push(s); + } + return mergeShort(chunks); +} + +function splitSources(body) { + // 一条文献以 <网址> 收尾,后面跟「;」再接下一条 + const parts = splitTopLevel(body, ';', false); + const chunks = []; + for (const p of parts) { + if (chunks.length && / s.replace(/^(\d+)([.)])(\s)/, '$1\\$2$3'); + +export function splitLongItems(md) { + return md.split('\n').map(line => { + const m = line.match(/^(\s*)- (收益|备注|来源):(.*)$/); + if (!m || line.length < MIN_LEN) return line; + const [, indent, label, body] = m; + const chunks = label === '来源' ? splitSources(body) : splitBenefit(body); + if (chunks.length < 2) return line; + return `${indent}- ${label}:\n` + chunks.map(c => `${indent} - ${safe(c.trim())}`).join('\n'); + }).join('\n'); +}