p2md 0.2.0__py3-none-any.whl

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.
p2md/build_md.py ADDED
@@ -0,0 +1,1060 @@
1
+ # -*- coding: utf-8 -*-
2
+ """组装章节 Markdown,并产出可直接发布的 article 目录。
3
+
4
+ 章节边界从 blocks 的 heading 自动推导("第X章"/"第X编"/"参考文献")。
5
+
6
+ 产物(project/output/ 下):
7
+ md/ 传统章节 Markdown(保留便于复核)
8
+ content/article_N.md 每章一篇文章(Markdown 交付物)
9
+ content/images/ 裁图(article 内的图片引用统一改为 images/)
10
+ """
11
+
12
+ import json
13
+ import os
14
+ import re
15
+ import shutil
16
+
17
+ CN = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6,
18
+ "七": 7, "八": 8, "九": 9, "十": 10}
19
+
20
+ # 有序列表项:行首"数字+分隔符(半角点/全角点/顿号/括号)+内容",如 "1. 投多少?"。
21
+ # 分隔符后可直接接中文(全角.后常无空格);但不能接数字/小数点(排除 3.5倍、1.2节)。
22
+ # 全角点 . 不是标准 md 有序列表标记,识别后统一输出半角点。
23
+ OL_RE = re.compile(r"^\s*(\d{1,2})\s*[..、))]\s*([^0-9\s].*)$")
24
+ # 无序列表项:行首 bullet(半角 - / 全角 •·▪)
25
+ UL_RE = re.compile(r"^\s*[-•·▪●]\s+(.+)$")
26
+
27
+
28
+ def _is_list_item(t):
29
+ """识别一行是否列表项。返回 ('ol', 序号, 内容) / ('ul', None, 内容) / None。
30
+
31
+ 有序列表项需是**行首数字编号**(如 "1. xxx" / "3.xxx" / "2、xxx")。
32
+ 排除正文里以数字开头的普通行:正文行通常铺满版心且不以编号格式出现。
33
+ """
34
+ if not t:
35
+ return None
36
+ m = OL_RE.match(t)
37
+ if m:
38
+ return ("ol", int(m.group(1)), m.group(2).strip())
39
+ m = UL_RE.match(t)
40
+ if m:
41
+ return ("ul", None, m.group(1).strip())
42
+ return None
43
+
44
+
45
+ def cn2int(s):
46
+ if not s:
47
+ return 0
48
+ if len(s) == 1:
49
+ return CN.get(s, 0)
50
+ if s.startswith("十"):
51
+ return 10 + CN.get(s[1:], 0)
52
+ if s.endswith("十"):
53
+ return CN.get(s[0], 0) * 10
54
+ return 0
55
+
56
+
57
+ def slugify(t):
58
+ t = re.sub(r"[,。、;:!?()“”\"'\s·—…\-]+", "", t)
59
+ return t
60
+
61
+
62
+ # 长图文章标题定位:首片顶部区域内的中日韩文本行;不含中文的行(时间/电量等
63
+ # 状态栏、纯数字符号)与以冒号结尾的节标签("上证指数:")均排除,再取行高最大者。
64
+ _CJK_RE = re.compile(r"[\u4e00-\u9fff]")
65
+ _TITLE_TOP_ZONE = 0.35 # 标题候选只在首片顶部 35% 页高内寻找
66
+
67
+
68
+ def _find_long_image_title_line(blocks_dir, first_page):
69
+ """通用:在长图首片顶部定位"标题行",返回 (text, y_top, h)(归一化)或 None。
70
+
71
+ 判据(纯排版形态,与平台/项目文案无关):
72
+ 1) 位于首片顶部区域(y_top ≤ 35% 页高);
73
+ 2) 含中日韩文字 —— 天然排除状态栏/时间/电量等行("11:22"、"×11 85"、"•");
74
+ 3) 不以 ":" / ":" 结尾 —— 排除"上证指数:"这类节标签;
75
+ 4) 候选中取**最靠上**者(长图文章标题固定在首片顶部元信息之前/紧随其后,
76
+ 正文行即便行高略大也不能抢走标题),并列时取行高更大者。
77
+ """
78
+ pages_json = os.path.join(os.path.dirname(os.path.abspath(blocks_dir)),
79
+ "ocr", "pages_json", f"{first_page}.json")
80
+ if not os.path.isfile(pages_json):
81
+ return None
82
+ try:
83
+ with open(pages_json, encoding="utf-8") as f:
84
+ lines = json.load(f).get("lines", [])
85
+ except Exception:
86
+ return None
87
+ cands = []
88
+ for ln in lines:
89
+ t = (ln.get("text") or "").strip()
90
+ if not t or not _CJK_RE.search(t) or t[-1] in "::":
91
+ continue
92
+ try:
93
+ y_top = 1.0 - float(ln["y"]) - float(ln["h"])
94
+ h = float(ln["h"])
95
+ except (KeyError, TypeError, ValueError):
96
+ continue
97
+ if 0 < y_top <= _TITLE_TOP_ZONE and h > 0:
98
+ cands.append((t, y_top, h))
99
+ if not cands:
100
+ return None
101
+ # 标题在长图首片最顶部;正文行字号可能略大(如么瑞 p101:标题 h=0.0174、
102
+ # 正文 h=0.0176),按行高最大会误把正文当标题。改为优先 y_top 最小。
103
+ return min(cands, key=lambda c: (c[1], -c[2], -len(c[0])))
104
+
105
+
106
+ def _long_image_title_from_ocr(blocks_dir, first_page):
107
+ """通用:从首片**原始 OCR lines** 定位长图文章标题(不依赖平台/项目文案)。
108
+
109
+ 为什么需要它:detect 的 heading 判定会漏判(长图标题常只比正文大一点),且标题
110
+ 若落在图片检测区域内会被当"图内文字"剔除,blocks 里就没有这一行了——而长图首片
111
+ 顶部的原始 OCR 里标题始终存在。判据见 `_find_long_image_title_line`。
112
+
113
+ Returns:
114
+ 标题字符串;未找到返回 ""(由调用方回退 blocks 综合判断)。
115
+ """
116
+ hit = _find_long_image_title_line(blocks_dir, first_page)
117
+ return hit[0] if hit else ""
118
+
119
+
120
+ def _long_image_title_span_px(blocks_dir, first_page):
121
+ """标题行在首片的**像素** y 范围 (y0, y1, title_h);取不到返回 None。
122
+
123
+ 用途:丢弃与标题重复的"标题条截图"——长截图顶部那条"标题 + 作者"的横条常被
124
+ detect 判成 image block(裁成 p{n}_f1.png),若标题文字已提取为 `# 标题`,再
125
+ 渲染这张图就是图文重复(应留字去图)。页高取自 OCR 预处理切片图
126
+ `output/_ocr_tmp/p{n}.jpg`(与 detect 同源),保证像素坐标与 blocks 对齐。
127
+ """
128
+ hit = _find_long_image_title_line(blocks_dir, first_page)
129
+ if not hit:
130
+ return None
131
+ _, y_top, h = hit
132
+ jpg = os.path.join(os.path.dirname(os.path.abspath(blocks_dir)),
133
+ "_ocr_tmp", f"p{first_page}.jpg")
134
+ try:
135
+ from PIL import Image as _Image
136
+ with _Image.open(jpg) as im:
137
+ H = float(im.size[1])
138
+ except Exception:
139
+ return None
140
+ return (y_top * H, (y_top + h) * H, h * H)
141
+
142
+
143
+ def _long_image_title(blocks_dir, first_page):
144
+ """取长图文章标题。
145
+
146
+ 先走 `_long_image_title_from_ocr`(读首片原始 OCR,通用排版判据,最稳);取不到
147
+ 时回退到对第一片 blocks 顶部的**综合判断**:
148
+
149
+ - 位置:标题在第一片最顶部(正文/小节标签之前,或紧随顶部元信息之后);
150
+ - 字号:标题字号**不小于**页内正文行高中位(节标签/图表注字号明显更小);
151
+ - 加粗:常加粗(加分项,字号显著大时非加粗也认);
152
+ - 内容:不是时间戳(如"11:53 ③")、不是节标签(以 `:` 结尾)。
153
+
154
+ 图片上标题顺序固定,结果只依赖图片排版,与 OCR 拆行无关。
155
+ """
156
+ t = _long_image_title_from_ocr(blocks_dir, first_page)
157
+ if t:
158
+ return t
159
+ data = _load_blocks(blocks_dir, first_page)
160
+ if not data:
161
+ return ""
162
+ blocks = [b for b in data
163
+ if b.get("type") in ("text", "heading") and (b.get("text") or "").strip()]
164
+ if not blocks:
165
+ return ""
166
+ # 页内正文行高中位数基准:用宽行(非节标签、非图注)的中位 h。
167
+ body_hs = [b["y_bottom"] - b["y_top"] for b in blocks
168
+ if not (b.get("text") or "").strip().endswith(":")]
169
+ if not body_hs:
170
+ return ""
171
+ body_h = sorted(body_hs)[len(body_hs) // 2] # 中位
172
+
173
+ # 按阅读序(y 升序)遍历。先跳过时间戳,看页首第一个"内容块":
174
+ blocks.sort(key=lambda b: (b["y_top"], b.get("x0", 0)))
175
+ content = [b for b in blocks
176
+ if (b.get("text") or "").strip()
177
+ and not re.match(r"^\s*\d{1,2}:\d{2}",
178
+ (b.get("text") or "").strip())]
179
+ if not content:
180
+ return ""
181
+ first = content[0]
182
+ first_t = (first.get("text") or "").strip()
183
+ # 情形 B:标题是"居中短行"(公众号/长图顶部文章题常见样式:字号与正文相近、
184
+ # 但页面最顶部、居中、独立一行),不依赖大字/加粗。若首块居中且较短 → 即标题。
185
+ if first.get("centered") and 0 < len(first_t) <= 32 \
186
+ and not first_t.endswith(":"):
187
+ return first_t
188
+
189
+ # 情形 A:标题靠大字号/加粗。从页首起收集"标题行"(连续大字/加粗行)。
190
+ # 判定要点:
191
+ # - 首行显著大于正文中位(≥1.15×)才是大字标题;
192
+ # - 后续行须与首行字号接近(回落 ≤10%)才并入——否则说明是标题下方的
193
+ # 摘要/正文段(即使整段加粗,如"摘要:…"学术文摘),标题区就此结束。
194
+ # - 连续多行标题(排版拆行)行高接近,不受影响。
195
+ group = []
196
+ first_h = None
197
+ limit_lines = 1
198
+ for b in content:
199
+ t = (b.get("text") or "").strip()
200
+ if t.endswith(":"):
201
+ break # 到第一个节标签(正文开始),标题区结束
202
+ h = b["y_bottom"] - b["y_top"]
203
+ is_bold = bool(b.get("bold"))
204
+ big = h >= body_h * 0.95
205
+ if is_bold or big:
206
+ if group and (h < first_h * 0.9
207
+ or b["y_top"] - group[-1]["y_bottom"]
208
+ > 1.6 * (group[-1]["y_bottom"] - group[-1]["y_top"] or 1)):
209
+ break # 行高回落(标题与摘要/正文的分界) / 垂直间距拉大 → 停
210
+ if not group:
211
+ first_h = h
212
+ # 收首行后才决定"行数上限"
213
+ limit_lines = 3 if first_h >= body_h * 1.15 else 1
214
+ group.append(b)
215
+ if len(group) > limit_lines:
216
+ # 超过 1 行/3 行:前 N 行是标题,多余是正文
217
+ group.pop()
218
+ break
219
+ elif group:
220
+ break # 标题区结束:遇到既不加粗也不大的文本块
221
+ if not group:
222
+ return ""
223
+ # group 首行上方若已存在普通正文(非粗、非居中、非时间戳)→ group 不是
224
+ # 页面最顶部的标题,而是正文中途被误判的大字行 → 不给标题。
225
+ for pre in content:
226
+ if pre["y_top"] >= group[0]["y_top"]:
227
+ break
228
+ pre_t = (pre.get("text") or "").strip()
229
+ if not pre_t or pre.get("centered") or pre.get("bold"):
230
+ continue
231
+ return ""
232
+
233
+ # 组内按视觉位置输出:同行(y_top 相近)按 x0 从左到右,行间按 y 从上到下
234
+ row_tol = max((b["y_bottom"] - b["y_top"] for b in group), default=20) * 0.6
235
+ rows = []
236
+ for b in group:
237
+ placed = False
238
+ for r in rows:
239
+ if abs(b["y_top"] - r["y_top"]) <= row_tol:
240
+ r["items"].append(b)
241
+ placed = True
242
+ break
243
+ if not placed:
244
+ rows.append({"y_top": b["y_top"], "items": [b]})
245
+ rows.sort(key=lambda r: r["y_top"])
246
+ out = []
247
+ for r in rows:
248
+ r["items"].sort(key=lambda b: b.get("x0", 0))
249
+ out.append("".join(b.get("text", "").strip() for b in r["items"]))
250
+ return "".join(out)
251
+
252
+
253
+ def auto_chapters(blocks_dir):
254
+ """从每页 blocks 的 heading 自动推导章节边界。
255
+
256
+ 长图(滚动长截图)被切成多片后,本质是同一张图、没有真实章节标题——此时按
257
+ heading 切分只会把"图表标题"误当章节标题。每张长图独立成一个章节(无标题),
258
+ 不与其他内容混合;多张长图各占一章。
259
+ """
260
+ files = sorted([f for f in os.listdir(blocks_dir) if f.endswith(".json")],
261
+ key=lambda f: int(f[:-5]))
262
+ pages = [int(f[:-5]) for f in files]
263
+ minp, maxp = pages[0], pages[-1]
264
+
265
+ # 长图切分页标记:ocr.prepare_images 写入 {源文件名: [页号,...]},每张长图独立成章
266
+ out_dir = os.path.dirname(os.path.abspath(blocks_dir))
267
+ lip_file = os.path.join(out_dir, "long_image_pages.txt")
268
+ lip_groups = []
269
+ if os.path.isfile(lip_file):
270
+ try:
271
+ with open(lip_file, encoding="utf-8") as f:
272
+ for line in f:
273
+ line = line.rstrip("\n")
274
+ if "\t" in line:
275
+ name, nums = line.split("\t", 1)
276
+ ps = [int(x) for x in nums.replace(",", " ").split() if x.strip()]
277
+ if ps:
278
+ lip_groups.append((name, sorted(set(ps))))
279
+ else:
280
+ ps = [int(x) for x in line.split() if x.strip()]
281
+ if ps:
282
+ lip_groups.append((line, sorted(set(ps))))
283
+ except Exception:
284
+ lip_groups = []
285
+
286
+ # 每张长图:占全部页 → 各自独立一章。文章标题取长图第一片顶部检测到的粗体标题
287
+ # (detect 已把"(8.24~8.30)"/"8月周总结第3期"等粗体行判为 heading)。
288
+ long_image_pages = set()
289
+ for _name, _ps in lip_groups:
290
+ long_image_pages.update(_ps)
291
+ if long_image_pages and set(pages) <= long_image_pages:
292
+ arts = []
293
+ for i, (_name, _ps) in enumerate(lip_groups):
294
+ title = _long_image_title(blocks_dir, min(_ps))
295
+ arts.append(("custom", "", f"article_{i}.md", title, min(_ps), max(_ps)))
296
+ # 若多张长图之间页码有缝隙(非连续的 blocks),把空白页范围也收进最近章
297
+ arts.sort(key=lambda a: a[4])
298
+ print(f" (整本书是 {len(lip_groups)} 张长图切分,各自独立成章)")
299
+ return arts
300
+ anchors = []
301
+ # 记录每一页首个 heading(任意格式),用于"找不到第X章"时回退到"每章一节"
302
+ first_heading_per_page = {}
303
+ for n in pages:
304
+ with open(os.path.join(blocks_dir, f"{n}.json"), encoding="utf-8") as f:
305
+ data = json.load(f)
306
+ heads = [h for h in data["blocks"] if h["type"] == "heading"]
307
+ if not heads:
308
+ continue
309
+ merged = []
310
+ for h in sorted(heads, key=lambda h: h["y_top"]):
311
+ if merged and h["y_top"] - merged[-1]["y_bottom"] < 130 \
312
+ and merged[-1]["level"] == h["level"]:
313
+ merged[-1]["text"] += " " + h["text"]
314
+ merged[-1]["y_bottom"] = h["y_bottom"]
315
+ else:
316
+ merged.append(dict(h))
317
+ first_heading_per_page[n] = merged[0]
318
+ for h in merged:
319
+ t = h["text"].strip()
320
+ if re.match(r"^第[0-9]+章( |$)", t):
321
+ anchors.append((n, "chapter", t))
322
+ break
323
+ if re.match(r"^第[一二三四五六七八九十]+编", t):
324
+ anchors.append((n, "part_intro", t))
325
+ break
326
+ if t == "参考文献":
327
+ anchors.append((n, "references", t))
328
+ break
329
+ anchors.sort()
330
+ if not anchors:
331
+ # 回退:书的章节不是"第X章/第X编/参考文献"格式(如"悖论系列N"等自定义标题),
332
+ # 把每一页首个 heading 当一章,文件用 heading 文本命名。
333
+ # 优先 level 小的(更大标题);每页只取首个 heading。
334
+ sorted_pages = sorted(first_heading_per_page.items(),
335
+ key=lambda kv: (kv[1]["level"], kv[0]))
336
+ seen_titles = set()
337
+ for n, h in sorted_pages:
338
+ title = h["text"].strip()
339
+ # 跳过页内重复标题(如重复页码)
340
+ if title in seen_titles:
341
+ continue
342
+ seen_titles.add(title)
343
+ anchors.append((n, "custom", title))
344
+ anchors.sort()
345
+ if not anchors:
346
+ raise SystemExit("未找到任何章节标题页(blocks 里没有 heading),"
347
+ "检查 detect 是否检测到 heading block")
348
+ print(f" (未识别到第X章/第X编/参考文献,回退到按 heading 切分,共 {len(anchors)} 章)")
349
+
350
+ segs = []
351
+ first = anchors[0][0]
352
+ if minp <= first - 1:
353
+ segs.append(("front", minp, first - 1, "前言 · 版权 · 致谢"))
354
+ for i, (n, kind, title) in enumerate(anchors):
355
+ end = anchors[i + 1][0] - 1 if i + 1 < len(anchors) else maxp
356
+ segs.append((kind, n, end, title))
357
+
358
+ cn_num = {"一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
359
+ "六": "6", "七": "7", "八": "8", "九": "9", "十": "10"}
360
+ chapters = []
361
+ max_no = 0
362
+ for kind, a, b, title in segs:
363
+ if kind == "front":
364
+ slug, fname, no = "00_前言", "00_前言.md", 0
365
+ elif kind == "references":
366
+ no = max_no + 1
367
+ slug, fname = f"{no:02d}_参考文献", f"{no:02d}_参考文献.md"
368
+ elif kind == "chapter":
369
+ m = re.match(r"^第([0-9]+)章\s*(.*)$", title)
370
+ no = int(m.group(1)) if m else 0
371
+ name = m.group(2).strip() if m else ""
372
+ if not name:
373
+ name = title
374
+ slug = f"{no:02d}_第{no}章_{slugify(name)}"
375
+ fname = slug + ".md"
376
+ elif kind == "part_intro":
377
+ m = re.match(r"^第([一二三四五六七八九十]+)编\s*(.*)$", title)
378
+ no = cn2int(m.group(1)) if m else 0
379
+ name = m.group(2).strip() if m else ""
380
+ if not name:
381
+ name = title
382
+ cn_txt = m.group(1) if m else ""
383
+ slug = f"{no:02d}_第{cn_txt}编_{slugify(name)}" if m else f"{no:02d}_{slugify(name)}"
384
+ fname = slug + ".md"
385
+ else: # custom: 自定义标题("悖论系列N"等无第X章格式)
386
+ max_no += 1
387
+ no = max_no
388
+ slug = f"{no:02d}_{slugify(title)}"
389
+ fname = slug + ".md"
390
+ max_no = max(max_no, no)
391
+ chapters.append((kind, slug, fname, title, a, b))
392
+ return chapters
393
+
394
+
395
+ def _load_blocks(blocks_dir, n):
396
+ p = os.path.join(blocks_dir, f"{n}.json")
397
+ if not os.path.exists(p):
398
+ return None
399
+ with open(p, encoding="utf-8") as f:
400
+ return json.load(f)["blocks"]
401
+
402
+
403
+ def _fmt_line(text, bold, centered):
404
+ """按 detect 判定的 加粗/居中 格式化单行文本。
405
+ 用纯 HTML(<strong> / <div style="text-align:center">)避免 markdown 解析器
406
+ 在 HTML 块内不处理 `**` 的问题。"""
407
+ if bold:
408
+ text = f"<strong>{text}</strong>"
409
+ if centered:
410
+ return f'<div style="text-align:center">{text}</div>'
411
+ return text
412
+
413
+
414
+ def render_page(blocks_dir, n, skip_title=None, no_headings=False):
415
+ """渲染一页 blocks 为 Markdown。
416
+
417
+ skip_title: 若给出标题文本,渲染时跳过第一个与之相同的 heading(用于避免
418
+ 与文件头 `# {title}` 重复,如自定义章节标题书的一二级 heading 重复)。
419
+ no_headings: 长图切分的内容没有真实标题,把所有 heading 当普通文本输出。
420
+ 段落内每行 = 一个 OCR text block(对应原图的一行视觉文字),用 `\n` 连接,
421
+ 保留原图换行结构。
422
+ """
423
+ blocks = _load_blocks(blocks_dir, n)
424
+ if not blocks:
425
+ return ""
426
+ out = []
427
+ skip_done = skip_title is None or no_headings
428
+ sorted_blocks = sorted(blocks, key=lambda b: (b["y_top"], 1 if b["type"] == "image" else 0))
429
+ merged = []
430
+ for b in sorted_blocks:
431
+ if (b["type"] == "heading" and merged and merged[-1]["type"] == "heading"
432
+ and merged[-1]["level"] == b["level"]
433
+ and b["y_top"] - merged[-1]["y_bottom"] < 130):
434
+ merged[-1]["text"] += f" {b['text']}"
435
+ merged[-1]["y_bottom"] = b["y_bottom"]
436
+ else:
437
+ merged.append(b)
438
+ cur_para = []
439
+ last_y = None
440
+ for b in merged:
441
+ if b["type"] == "image":
442
+ if cur_para:
443
+ out.append("".join(cur_para) + "\n\n")
444
+ cur_para = []
445
+ cap = b.get("caption", "").strip()
446
+ alt = cap if cap else "插图"
447
+ out.append(f"![{alt}](../figs/{b['file']})\n\n")
448
+ last_y = b["y_bottom"]
449
+ elif b["type"] == "heading":
450
+ if not skip_done and b["text"].strip() == skip_title:
451
+ # 跳过与文件头重复的章节标题(只跳第一个)
452
+ skip_done = True
453
+ last_y = b["y_bottom"]
454
+ continue
455
+ if no_headings:
456
+ # 长图切分:标题当普通文本,但按几何聚类 para_start 分段落
457
+ _bold = bool(b.get("bold"))
458
+ _centered = bool(b.get("centered"))
459
+ if bool(b.get("para_start")) and cur_para:
460
+ out.append("".join(cur_para) + "\n\n")
461
+ cur_para = []
462
+ if _centered:
463
+ if cur_para:
464
+ out.append("".join(cur_para) + "\n\n")
465
+ cur_para = []
466
+ out.append(_fmt_line(b["text"], _bold, True) + "\n\n")
467
+ else:
468
+ cur_para.append(_fmt_line(b["text"], _bold, False))
469
+ last_y = b["y_bottom"]
470
+ continue
471
+ if cur_para:
472
+ out.append("".join(cur_para) + "\n\n")
473
+ cur_para = []
474
+ level = b.get("level", 3)
475
+ marker = "#" * level
476
+ out.append(f"{marker} {b['text']}\n\n")
477
+ last_y = b["y_bottom"]
478
+ else:
479
+ t = b["text"]
480
+ bold = bool(b.get("bold"))
481
+ centered = bool(b.get("centered"))
482
+ # 段落开始:detect 已按几何聚类算出 para_start;为 True 时开新段
483
+ if bool(b.get("para_start")) and cur_para:
484
+ out.append("".join(cur_para) + "\n\n")
485
+ cur_para = []
486
+ if centered:
487
+ # 居中行独占一段(HTML div 居中),加粗一并应用
488
+ if cur_para:
489
+ out.append("".join(cur_para) + "\n\n")
490
+ cur_para = []
491
+ out.append(_fmt_line(t, bold, centered) + "\n\n")
492
+ else:
493
+ cur_para.append(_fmt_line(t, bold, False))
494
+ last_y = b["y_bottom"]
495
+ if cur_para:
496
+ out.append("".join(cur_para) + "\n\n")
497
+ return "".join(out)
498
+
499
+
500
+ def _is_title_bar_block(page, block, drop_img_span):
501
+ """image block 是否是"标题条截图"(应留字去图的那张)。
502
+
503
+ drop_img_span = (page, y0, y1, title_h):标题行的页号与像素 y 范围/行高。
504
+ 判据以"与标题行重叠"为主(长截图顶部的"标题+作者"横条必然压着标题行),
505
+ 以"图片高度 ≤ 3×标题行高(同量级)"为辅——避免把恰好与标题行有交叠的
506
+ 正文大图误删。
507
+ """
508
+ if not drop_img_span:
509
+ return False
510
+ p, y0, y1, title_h = drop_img_span
511
+ if page != p:
512
+ return False
513
+ b_top = block.get("y_top")
514
+ b_bot = block.get("y_bottom")
515
+ if b_top is None or b_bot is None:
516
+ return False
517
+ if not (b_top < y1 and b_bot > y0):
518
+ return False # 与标题行无重叠 → 正常内容图
519
+ img_h = b_bot - b_top
520
+ return title_h <= 0 or img_h <= 3 * title_h
521
+
522
+
523
+ def _render_pages_cross_page(blocks_dir, pages, skip_title=None, no_headings=False,
524
+ skip_pages=(), drop_img_span=None):
525
+ """跨页渲染:把 pages 列表里所有页的 blocks 合在一起按 y 排序,
526
+ 段落状态 cur_para 跨页延续——detect 标 para_start=False 的首行(切片边界
527
+ 续行)自动并入上一段,消除长图切片造成的"段中换页=两段"伪断裂。
528
+
529
+ skip_pages: 这些页整页不参与渲染(沿用旧的 front 跳过第10/11/12页逻辑)。
530
+ drop_img_span: (page, y0, y1, title_h),把该页与标题行重叠的"标题条截图"丢弃
531
+ ——标题文字已写在文件头 `# 标题`,同一张图再插一次即图文重复(留字去图)。
532
+ """
533
+ out = []
534
+ all_blocks = []
535
+ for n in pages:
536
+ if n in skip_pages:
537
+ continue
538
+ b = _load_blocks(blocks_dir, n)
539
+ if not b:
540
+ continue
541
+ for bb in b:
542
+ all_blocks.append((n, bb))
543
+ if not all_blocks:
544
+ return ""
545
+ # 跨页按 y 排序:同一页内按 y_top,跨页时不要把不同页的 y 混在一起直接比
546
+ # (不同页坐标系独立),但这里 OCR y_top 是 detect 用当前页 H 算出的像素,
547
+ # 不同页 H 相近且长图切片是按 y 顺序切的,按 (n, y_top) 排序天然就是阅读顺序。
548
+ all_blocks.sort(key=lambda nb: (nb[0], nb[1]["y_top"]))
549
+
550
+ # 标题合并:按**视觉位置**分组排序(先 y_top 行、同行内按 x0 从左到右),
551
+ # 使标题文本顺序与图片一致,与 OCR 拆行无关;拼接不加空格。
552
+ merged = []
553
+ for n, b in all_blocks:
554
+ # 仅合并"同层级的连续标题行":标题(通常居左/非居中)与其后独立的作者署名行
555
+ # (居中"知乎:…")不合并——否则标题文本会被署名污染, 无法与文件头标题做 skip。
556
+ _same_align = (merged[-1][1].get("centered") == b.get("centered")) \
557
+ if (merged and merged[-1][1]["type"] == "heading") else False
558
+ if (b["type"] == "heading" and merged and merged[-1][1]["type"] == "heading"
559
+ and merged[-1][1]["level"] == b["level"]
560
+ and b["y_top"] - merged[-1][1]["y_bottom"] < 130
561
+ and _same_align):
562
+ # 并入当前 heading 组
563
+ cur = merged[-1][1]
564
+ cur["_group"].append(b)
565
+ cur["y_bottom"] = max(cur["y_bottom"], b["y_bottom"])
566
+ else:
567
+ nb = dict(b)
568
+ if nb.get("type") == "heading":
569
+ nb["_group"] = []
570
+ merged.append((n, nb))
571
+ # 对每个 heading 组按**视觉位置**排序拼接:同行(y_top 相近)按 x0 从左到右,
572
+ # 行间按 y 从上到下——与 _long_image_title 一致,保证正文标题与文件头一致可 skip。
573
+ for n, b in merged:
574
+ if b.get("type") == "heading":
575
+ g = [b] + b.pop("_group", [])
576
+ row_tol = max((hh.get("y_bottom", 0) - hh.get("y_top", 0) for hh in g),
577
+ default=20) * 0.6
578
+ rows = []
579
+ for hh in g:
580
+ placed = False
581
+ for r in rows:
582
+ if abs(hh["y_top"] - r["y_top"]) <= row_tol:
583
+ r["items"].append(hh)
584
+ placed = True
585
+ break
586
+ if not placed:
587
+ rows.append({"y_top": hh["y_top"], "items": [hh]})
588
+ rows.sort(key=lambda r: r["y_top"])
589
+ parts = []
590
+ for r in rows:
591
+ r["items"].sort(key=lambda hh: hh.get("x0", 0))
592
+ parts.append("".join(hh.get("text", "").strip() for hh in r["items"]))
593
+ b["text"] = "".join(parts)
594
+ b["y_bottom"] = max(hh["y_bottom"] for hh in g)
595
+ b["bold"] = any(hh.get("bold") for hh in g)
596
+ b["centered"] = all(hh.get("centered") for hh in g)
597
+
598
+ # skip_done 只在"没有标题要跳过"时直接置 True;no_headings 时仍允许
599
+ # 跳过与文件头 # {title} 重复的标题(长图标题已在文件头写 #)。
600
+ skip_done = skip_title is None
601
+ cur_para = []
602
+ SENT_END = ("。", "!", "?")
603
+ # 跨页权威段落判定:基于合并后的阅读顺序,用"文本标点 + 同页垂直间距"重算段首。
604
+ # - 文本标点:上一行以句末标点结尾 / 本行是节标签(以:结尾) → 段首。
605
+ # - 垂直间距(仅长图 no_headings 用,且只在**同一页内**相邻文本之间可比):
606
+ # 相邻文本块 gap/h 明显大于段内行距(OCR 拆行 gap/h≈0.2~0.6,段间距≈1.0+)
607
+ # → 段首。detect per-page 输出对切片首行无上下文(i=0 默认 False),
608
+ # 故跨页处用"上一行是否句号"续判。
609
+ # 追踪"上一个同页文本行"的位置用于 gap/h;换页/图片打断后失效。
610
+ prev_text = ""
611
+ prev_pg = None
612
+ prev_bot = None
613
+ prev_h = None
614
+
615
+ def _page_gap_is_para(n, b):
616
+ """同页且垂直间距明显(≥0.75×本行高) → 真段间距。仅长图模式启用。"""
617
+ if not no_headings or prev_pg is None or n != prev_pg:
618
+ return False
619
+ h = b["y_bottom"] - b["y_top"]
620
+ if h <= 0 or prev_bot is None:
621
+ return False
622
+ gap = b["y_top"] - prev_bot
623
+ return gap >= 0.75 * h
624
+
625
+ def _reset_pos():
626
+ nonlocal prev_pg, prev_bot, prev_h
627
+ prev_pg = None
628
+ prev_bot = None
629
+ prev_h = None
630
+
631
+ def _update_pos(n, b):
632
+ nonlocal prev_pg, prev_bot, prev_h
633
+ prev_pg = n
634
+ prev_bot = b["y_bottom"]
635
+ prev_h = b["y_bottom"] - b["y_top"]
636
+
637
+ # 通用列表识别状态:检测"连续编号行/同 bullet 行"并把它们输出为 markdown 列表。
638
+ # 有序项(1./2./3. 或 1.2.3. 或 1、2、3)与无序项(- • ·)都会识别;
639
+ # 编号标点统一为半角(`3.`→`3.`),markdown 预览缩进一致。
640
+ pending_list = []
641
+ pending_kind = None # 'ol' / 'ul'
642
+ pending_prev_num = None
643
+
644
+ def _flush_pending_list():
645
+ nonlocal pending_list, pending_kind, pending_prev_num
646
+ if pending_list:
647
+ out.append("\n".join(pending_list) + "\n\n")
648
+ pending_list = []
649
+ pending_kind = None
650
+ pending_prev_num = None
651
+
652
+ def _emit_list_or_para(plain, styled_html):
653
+ """把一段文本输出:若它是列表项则并入 pending_list(连续同类项同列表),
654
+ 否则先冲刷 pending_list 再按普通段落输出。styled_html 为 None 表示纯文本。"""
655
+ nonlocal pending_kind, pending_prev_num
656
+ s = (plain or "").strip()
657
+ item = None
658
+ m = OL_RE.match(s) if s else None
659
+ if m and styled_html is None:
660
+ item = ("ol", int(m.group(1)), m.group(2).strip())
661
+ else:
662
+ m = UL_RE.match(s) if s else None
663
+ if m and styled_html is None:
664
+ item = ("ul", None, m.group(1).strip())
665
+ if item is None:
666
+ _flush_pending_list()
667
+ if styled_html is None:
668
+ out.append(s + "\n\n")
669
+ else:
670
+ out.append(styled_html + "\n\n")
671
+ return
672
+ kind, num, content = item
673
+ if kind == "ol":
674
+ line = f"{num}. {content}"
675
+ else:
676
+ line = f"- {content}"
677
+ # 与上一个列表项同类且(有序编号连续 or 无序) → 并入同一列表
678
+ if pending_kind == kind and (
679
+ kind == "ul" or (num == (pending_prev_num or 0) + 1)):
680
+ pending_list.append(line)
681
+ pending_prev_num = num
682
+ return
683
+ # 不连续(编号跳跃 / 类型变化)→ 新起一段列表
684
+ _flush_pending_list()
685
+ pending_kind = kind
686
+ pending_list.append(line)
687
+ pending_prev_num = num
688
+
689
+ def _push_text_segments(b, txt, bold, use_strip=True):
690
+ """把一段文本(可能行内含多色 color_spans)切分后追加进 cur_para。
691
+
692
+ 行内混色(detect 的 color_spans=[{s,e,color_rgb,color_name}],s/e 为字符
693
+ 区间):黑段 col=None、彩色段用其标准色,逐段 append;_flush_para 合并
694
+ 相邻同色段,最终输出"行内多个 <span>"而非整行一色。
695
+ """
696
+ spans = b.get("color_spans") or []
697
+ if not spans:
698
+ _use = txt.strip() if use_strip else txt
699
+ if b.get("color_hue") is not None and b.get("color_rgb"):
700
+ col = (b["color_rgb"], b.get("color_name"))
701
+ else:
702
+ col = None
703
+ if _use:
704
+ cur_para.append([_use, bold, col])
705
+ return
706
+ # 有 spans:不 strip(保证字符索引与 spans 对齐)
707
+ segs = []
708
+ prev = 0
709
+ n = len(txt)
710
+ for sp in sorted(spans, key=lambda x: x["s"]):
711
+ s = max(0, int(sp.get("s", 0)))
712
+ e = min(n - 1, int(sp.get("e", 0)))
713
+ if s > prev:
714
+ segs.append((txt[prev:s], None))
715
+ segs.append((txt[s:e + 1],
716
+ (sp["color_rgb"], sp.get("color_name"))))
717
+ prev = e + 1
718
+ if prev < n:
719
+ segs.append((txt[prev:], None))
720
+ for seg, col in segs:
721
+ seg = seg.strip()
722
+ if seg:
723
+ cur_para.append([seg, bold, col])
724
+
725
+ def _flush_para():
726
+ """把 cur_para(元素 (text, bold, color_rgb))组装成一段。
727
+ 连续同加粗且同色的行合并为一个 run;run 整体按 bold/颜色上样
728
+ (<strong> / <span style="color:#xxxxxx">),避免 OCR 把同一视觉行拆成
729
+ 多块时输出 <strong>a</strong><strong>b</strong>(如"注:…板/块。")。
730
+
731
+ 若该段是"列表项"(行首编号/bullet)且为纯文本 → 走 _emit_list_or_para
732
+ 归入通用列表;带样式(加粗/彩色)的段落按普通文本输出(保留样式)。
733
+ """
734
+ if not cur_para:
735
+ return
736
+ runs = []
737
+ for txt, bld, col in cur_para:
738
+ if runs and runs[-1][1] == bld and runs[-1][2] == col:
739
+ runs[-1][0] += txt
740
+ else:
741
+ runs.append([txt, bld, col])
742
+ has_style = any(bld or col for _, bld, col in runs)
743
+ parts = []
744
+ for t, bld, col in runs:
745
+ s = t
746
+ if col:
747
+ # col=(rgb, name)。语义标准色优先输出名称(如 red/pink);无则 hex
748
+ _val = col[1] if col[1] else "#%02x%02x%02x" % tuple(col[0][:3])
749
+ s = f'<span style="color:{_val}">{s}</span>'
750
+ if bld:
751
+ s = f"<strong>{s}</strong>"
752
+ parts.append(s)
753
+ plain = "".join(txt for txt, _, _ in cur_para)
754
+ _emit_list_or_para(plain, None if not has_style else "".join(parts))
755
+ cur_para.clear()
756
+
757
+ def _md_table(b):
758
+ rows = b.get("rows") or []
759
+ if not rows:
760
+ return ""
761
+ ncol = max(len(r) for r in rows)
762
+ def _cell_text(c):
763
+ t = (c.get("text") or "").strip() if isinstance(c, dict) else str(c).strip()
764
+ col = c.get("color_rgb") if isinstance(c, dict) else None
765
+ if col:
766
+ nm = c.get("color_name") if isinstance(c, dict) else None
767
+ _val = nm if nm else "#%02x%02x%02x" % tuple(col[:3])
768
+ t = f'<span style="color:{_val}">{t}</span>'
769
+ t = t.replace("|", "\\|")
770
+ return t
771
+ def _line(cells):
772
+ cells = cells + [""] * (ncol - len(cells))
773
+ return "| " + " | ".join(_cell_text(c) for c in cells) + " |"
774
+ lines = [_line(rows[0])]
775
+ lines.append("| " + " | ".join("---" for _ in range(ncol)) + " |")
776
+ for r in rows[1:]:
777
+ lines.append(_line(r))
778
+ return "\n".join(lines) + "\n\n"
779
+
780
+ for n, b in merged:
781
+ if b["type"] == "image":
782
+ if _is_title_bar_block(n, b, drop_img_span):
783
+ continue # 标题条截图:标题文字已在文件头,留字去图
784
+ _flush_para()
785
+ _flush_pending_list()
786
+ cap = b.get("caption", "").strip()
787
+ alt = cap if cap else "插图"
788
+ out.append(f"![{alt}](../figs/{b['file']})\n\n")
789
+ prev_text = ""
790
+ _reset_pos()
791
+ continue
792
+ if b["type"] == "table":
793
+ _flush_para()
794
+ _flush_pending_list()
795
+ _mdt = _md_table(b)
796
+ if _mdt:
797
+ out.append(_mdt)
798
+ prev_text = ""
799
+ _reset_pos()
800
+ continue
801
+ if b["type"] == "heading":
802
+ if not skip_done and b["text"].strip() == skip_title:
803
+ skip_done = True
804
+ prev_text = b["text"].rstrip()
805
+ continue
806
+ if no_headings:
807
+ _bold = bool(b.get("bold"))
808
+ _centered = bool(b.get("centered"))
809
+ _t = b["text"]
810
+ # skip_title 匹配——若合并后 heading 文本以标题开头, 剥除前缀输出剩余;
811
+ # 解决 heading 合并把"居中标题+居中节标签"粘成 "标题写在前面" 而跳不过的 bug。
812
+ if not skip_done and _t.startswith(skip_title):
813
+ rest = _t[len(skip_title):].lstrip()
814
+ skip_done = True
815
+ if not rest:
816
+ prev_text = _t.rstrip()
817
+ _update_pos(n, b)
818
+ continue
819
+ # 把剩余部分当成独立 heading 输出(保留居中/加粗)
820
+ if _centered:
821
+ _flush_para()
822
+ _flush_pending_list()
823
+ out.append(_fmt_line(rest, _bold, True) + "\n\n")
824
+ else:
825
+ _push_text_segments(b, rest, _bold, use_strip=True)
826
+ prev_text = rest.rstrip()
827
+ _update_pos(n, b)
828
+ continue
829
+ _ci = _is_list_item(_t)
830
+ _is_label_h = _t.strip().endswith(":")
831
+ # 段首判定:与 text 分支一致——以 detect 的 para_start 为主(标点+节标签
832
+ # 文本驱动), 跨页/切片首行用 prev_text 句末标点补判。
833
+ para_start = (bool(b.get("para_start", False))
834
+ or _is_label_h
835
+ or (prev_text.rstrip().endswith(":") and _ci is not None)
836
+ or (prev_text.endswith(SENT_END) if prev_text else False))
837
+ if para_start and cur_para:
838
+ _flush_para()
839
+ _flush_pending_list()
840
+ if _centered:
841
+ _flush_para()
842
+ _flush_pending_list()
843
+ out.append(_fmt_line(_t, _bold, True) + "\n\n")
844
+ _update_pos(n, b)
845
+ else:
846
+ # 仅真正彩色(有明确色相)上色;黑/灰正文不上;
847
+ # 行内混色(color_spans)由 _push_text_segments 按段处理
848
+ _push_text_segments(b, _t, _bold, use_strip=True)
849
+ _update_pos(n, b)
850
+ prev_text = _t.rstrip()
851
+ continue
852
+ _flush_para()
853
+ _flush_pending_list()
854
+ level = b.get("level", 3)
855
+ marker = "#" * level
856
+ out.append(f"{marker} {b['text']}\n\n")
857
+ prev_text = b["text"].rstrip()
858
+ _reset_pos()
859
+ continue
860
+ t = b["text"]
861
+ # 长图文章标题已作为 # 写入文件头:正文首个与标题相同的 text(标题行不是
862
+ # heading 而是普通加粗 text)也应跳过,避免 `# 标题` 后又重复一行标题。
863
+ if not skip_done and t.strip() == skip_title:
864
+ skip_done = True
865
+ prev_text = t.rstrip()
866
+ _update_pos(n, b)
867
+ continue
868
+ bold = bool(b.get("bold"))
869
+ centered = bool(b.get("centered"))
870
+ is_label = t.strip().endswith(":")
871
+ _cur_item = _is_list_item(t)
872
+ # 段首判定:优先用 detect 的 para_start(标点+节标签文本驱动, 在长图
873
+ # 行距大(gap/h≈0.94)的截图上仍可靠——而旧版的 gap>=0.75h 阈值会把
874
+ # 每行当段首造成"每行独立段"伪断裂)。跨页/切片首行 detect 拿不到上行
875
+ # 上下文默认 False, 用 prev_text 句末标点补判。
876
+ para_start = (bool(b.get("para_start", False))
877
+ or is_label
878
+ or (prev_text.rstrip().endswith(":") and _cur_item is not None)
879
+ or (prev_text.endswith(SENT_END) if prev_text else False))
880
+ if para_start:
881
+ _flush_para()
882
+ if centered:
883
+ _flush_para()
884
+ _flush_pending_list()
885
+ out.append(_fmt_line(t, bold, centered) + "\n\n")
886
+ else:
887
+ # 仅真正彩色(有明确色相)上色;黑/灰正文不上。
888
+ # 行内混色(color_spans)由 _push_text_segments 按段处理;
889
+ # 无混色时仍保持原 append(t) 语义(不 strip,保留 OCR 原文)。
890
+ _push_text_segments(b, t, bold, use_strip=False)
891
+ prev_text = t.rstrip()
892
+ _update_pos(n, b)
893
+ _flush_para()
894
+ _flush_pending_list()
895
+ return "".join(out)
896
+
897
+
898
+ def _write_chapter(blocks_dir, md_tmp, spec):
899
+ kind, slug, fname, title, start, end = spec
900
+ p = os.path.join(md_tmp, fname)
901
+ # 空标题(如长图切分合并的单篇)不写 `#`,避免无意义的空标题
902
+ head = ([f"# {title}\n\n"] if title else [])
903
+ if kind == "front":
904
+ body = _render_pages_cross_page(
905
+ blocks_dir, list(range(start, end + 1)),
906
+ skip_pages=(10, 11, 12))
907
+ elif kind == "part_intro":
908
+ body = _render_pages_cross_page(blocks_dir, [start])
909
+ else:
910
+ # 长图切分(slug 为空):文章标题已从长图顶部粗体取到,正文里其余
911
+ # heading(图表标题等)仍当普通文本,只在文件头写 # 标题、并跳过正文
912
+ # 里与标题相同的首个 heading 避免重复。普通书则按 heading 正常渲染。
913
+ is_long_image = (slug == "")
914
+ drop_img_span = None
915
+ if is_long_image:
916
+ # 长截图顶部"标题+作者"横条常被 detect 裁成一张图;标题文字既已提取为
917
+ # 文件头 `# 标题`,就丢弃这张"标题条截图",做到留字去图。
918
+ span = _long_image_title_span_px(blocks_dir, start)
919
+ if span:
920
+ drop_img_span = (start,) + tuple(span)
921
+ body = _render_pages_cross_page(
922
+ blocks_dir, list(range(start, end + 1)),
923
+ skip_title=title, no_headings=is_long_image or not bool(title),
924
+ drop_img_span=drop_img_span)
925
+ with open(p, "w", encoding="utf-8") as f:
926
+ f.write("".join(head) + body)
927
+ return p
928
+
929
+
930
+ def _mirror_dir(src_dir, dst_dir):
931
+ """把 src_dir 的内容镜像同步到 dst_dir:文件复制覆盖,目录递归同步,
932
+ 并删除 dst_dir 中 src_dir 没有的多余项(避免历史残留累积,如 images/images)。"""
933
+ os.makedirs(dst_dir, exist_ok=True)
934
+ src_names = set(os.listdir(src_dir))
935
+ for f in src_names:
936
+ s = os.path.join(src_dir, f)
937
+ d = os.path.join(dst_dir, f)
938
+ if os.path.isdir(s):
939
+ _mirror_dir(s, d)
940
+ else:
941
+ shutil.copy2(s, d)
942
+ for f in os.listdir(dst_dir):
943
+ if f not in src_names:
944
+ p = os.path.join(dst_dir, f)
945
+ if os.path.isdir(p):
946
+ shutil.rmtree(p)
947
+ else:
948
+ os.remove(p)
949
+
950
+
951
+ def _swap_in(tmp, dst):
952
+ """把 tmp 内容移入 dst(覆盖同名),并删除 dst 中多出的旧文件/目录。
953
+
954
+ 支持子目录(如 images/):用镜像同步递归合并,历史残留(如 images/images)会被清掉。
955
+ """
956
+ os.makedirs(dst, exist_ok=True)
957
+ _mirror_dir(tmp, dst)
958
+ try:
959
+ shutil.rmtree(tmp)
960
+ except OSError:
961
+ pass
962
+
963
+
964
+ def _rewrite_fig_refs(md_text):
965
+ """把 article 内的 `../figs/` 图片引用改为 `images/`。"""
966
+ return md_text.replace("../figs/", "images/")
967
+
968
+
969
+ def _clean_stale_tmp(output_dir):
970
+ """清理 output 下历史遗留的临时目录(.md.tmp.* / .article.tmp.* 等)。
971
+
972
+ 正常流程里临时目录在 _swap_in 中会被清理;若某次运行中途失败会残留。
973
+ 这里在每次 build 前清除它们,避免越积越多。
974
+ """
975
+ out = str(output_dir)
976
+ if not os.path.isdir(out):
977
+ return
978
+ for name in os.listdir(out):
979
+ p = os.path.join(out, name)
980
+ if os.path.isdir(p) and name.startswith((".md.tmp.", ".article.tmp.",
981
+ ".blocks.tmp.", ".figs.tmp.")):
982
+ shutil.rmtree(p, ignore_errors=True)
983
+ print(f" 清理历史临时目录: {p}")
984
+
985
+
986
+ def build(project, chapters=None):
987
+ """组装章节 Markdown,并产出 article 目录(每章一文件 + images/)。
988
+
989
+ Args:
990
+ project: Project 实例
991
+ chapters: 可选人工章节结构(list of dict,含 kind/slug/file/title/start/end)
992
+ Returns:
993
+ (md_dir, article_dir, chapters)
994
+ """
995
+ _clean_stale_tmp(project.output_dir)
996
+ blocks_dir = str(project.get_blocks_dir())
997
+ figs_dir = str(project.get_figs_dir())
998
+ md_dir = str(project.get_md_dir())
999
+ article_dir = str(project.get_article_dir())
1000
+ images_out = str(project.get_images_out_dir())
1001
+
1002
+ if not os.path.isdir(blocks_dir):
1003
+ raise SystemExit(f"错误:未找到 blocks 目录 {blocks_dir}\n"
1004
+ f" 请先运行布局分析:python main.py detect -p <project>")
1005
+ if not os.path.isdir(figs_dir):
1006
+ raise SystemExit(f"错误:未找到 figs 目录 {figs_dir}\n"
1007
+ f" 请先运行布局分析:python main.py detect -p <project>")
1008
+
1009
+ md_tmp = os.path.join(project.output_dir, f".md.tmp.{os.getpid()}")
1010
+ os.makedirs(md_tmp, exist_ok=True)
1011
+
1012
+ if chapters is None:
1013
+ chapters = auto_chapters(blocks_dir)
1014
+ print("检测到的章节结构(自动推导,可人工复核):")
1015
+ for c in chapters:
1016
+ print(f" {c[0]:10} {c[4]:>4}-{c[5]:<4} {c[1]}")
1017
+ else:
1018
+ print("使用人工章节结构")
1019
+
1020
+ written = []
1021
+ for spec in chapters:
1022
+ if isinstance(spec, dict):
1023
+ spec = (spec["kind"], spec["slug"], spec["file"], spec["title"],
1024
+ spec["start"], spec["end"])
1025
+ p = _write_chapter(blocks_dir, md_tmp, spec)
1026
+ written.append(spec)
1027
+ print(f" {os.path.basename(p)} ({os.path.getsize(p) // 1024} KB)")
1028
+ _swap_in(md_tmp, md_dir)
1029
+
1030
+ # ---- 产出 article 目录(每章一个 md + images/) ----
1031
+ # 每章 → article_N.md;图片引用 ../figs/ → images/;裁图复制到 content/images/
1032
+ article_tmp = os.path.join(project.output_dir, f".article.tmp.{os.getpid()}")
1033
+ images_tmp = os.path.join(article_tmp, "images")
1034
+ os.makedirs(article_tmp, exist_ok=True)
1035
+ os.makedirs(images_tmp, exist_ok=True)
1036
+
1037
+ # 复制裁图
1038
+ if os.path.isdir(figs_dir):
1039
+ for f in os.listdir(figs_dir):
1040
+ if f.endswith(".png"):
1041
+ shutil.copyfile(os.path.join(figs_dir, f), os.path.join(images_tmp, f))
1042
+
1043
+ # 每章写为 article_N.md(按章节顺序编号)
1044
+ for i, spec in enumerate(written):
1045
+ # 从 md_dir 读取对应章节文件
1046
+ fname = spec[2]
1047
+ src_path = os.path.join(md_dir, fname)
1048
+ if not os.path.exists(src_path):
1049
+ continue
1050
+ with open(src_path, encoding="utf-8") as f:
1051
+ content = f.read()
1052
+ content = _rewrite_fig_refs(content)
1053
+ with open(os.path.join(article_tmp, f"article_{i}.md"), "w", encoding="utf-8") as f:
1054
+ f.write(content)
1055
+ _swap_in(article_tmp, article_dir)
1056
+
1057
+ n_articles = len(written)
1058
+ n_imgs = len([f for f in os.listdir(images_out) if f.endswith(".png")]) if os.path.isdir(images_out) else 0
1059
+ print(f"\n产出 {n_articles} 章 article + {n_imgs} 张图 → {article_dir}/")
1060
+ return md_dir, article_dir, written