chapter_outline_loader.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import annotations
  4. import json
  5. import re
  6. from pathlib import Path
  7. from typing import Any, Dict
  8. try:
  9. from chapter_paths import volume_num_for_chapter
  10. except ImportError: # pragma: no cover
  11. from scripts.chapter_paths import volume_num_for_chapter
  12. _CHAPTER_RANGE_RE = re.compile(r"^\s*(\d+)\s*-\s*(\d+)\s*$")
  13. def _parse_chapters_range(value: object) -> tuple[int, int] | None:
  14. if not isinstance(value, str):
  15. return None
  16. match = _CHAPTER_RANGE_RE.match(value)
  17. if not match:
  18. return None
  19. try:
  20. start = int(match.group(1))
  21. end = int(match.group(2))
  22. except ValueError:
  23. return None
  24. if start <= 0 or end <= 0 or start > end:
  25. return None
  26. return start, end
  27. def volume_num_for_chapter_from_state(project_root: Path, chapter_num: int) -> int | None:
  28. state_path = project_root / ".webnovel" / "state.json"
  29. if not state_path.exists():
  30. return None
  31. try:
  32. state = json.loads(state_path.read_text(encoding="utf-8"))
  33. except Exception:
  34. return None
  35. if not isinstance(state, dict):
  36. return None
  37. progress = state.get("progress")
  38. if not isinstance(progress, dict):
  39. return None
  40. volumes_planned = progress.get("volumes_planned")
  41. if not isinstance(volumes_planned, list):
  42. return None
  43. best: tuple[int, int] | None = None
  44. for item in volumes_planned:
  45. if not isinstance(item, dict):
  46. continue
  47. volume = item.get("volume")
  48. if not isinstance(volume, int) or volume <= 0:
  49. continue
  50. parsed = _parse_chapters_range(item.get("chapters_range"))
  51. if not parsed:
  52. continue
  53. start, end = parsed
  54. if start <= chapter_num <= end:
  55. candidate = (start, volume)
  56. if best is None or candidate[0] > best[0] or (candidate[0] == best[0] and candidate[1] < best[1]):
  57. best = candidate
  58. return best[1] if best else None
  59. def _find_split_outline_file(outline_dir: Path, chapter_num: int) -> Path | None:
  60. patterns = [
  61. f"第{chapter_num}章*.md",
  62. f"第{chapter_num:02d}章*.md",
  63. f"第{chapter_num:03d}章*.md",
  64. f"第{chapter_num:04d}章*.md",
  65. ]
  66. for pattern in patterns:
  67. matches = sorted(outline_dir.glob(pattern))
  68. if matches:
  69. return matches[0]
  70. return None
  71. def _find_volume_outline_file(project_root: Path, chapter_num: int) -> Path | None:
  72. outline_dir = project_root / "大纲"
  73. volume_num = volume_num_for_chapter_from_state(project_root, chapter_num) or volume_num_for_chapter(chapter_num)
  74. candidates = [
  75. outline_dir / f"第{volume_num}卷-详细大纲.md",
  76. outline_dir / f"第{volume_num}卷 - 详细大纲.md",
  77. outline_dir / f"第{volume_num}卷 详细大纲.md",
  78. ]
  79. return next((path for path in candidates if path.exists()), None)
  80. def _extract_outline_section(content: str, chapter_num: int) -> str | None:
  81. patterns = [
  82. rf"###\s*第\s*{chapter_num}\s*章[::]\s*(.+?)(?=###\s*第\s*\d+\s*章|##\s|$)",
  83. rf"###\s*第{chapter_num}章[::]\s*(.+?)(?=###\s*第\d+章|##\s|$)",
  84. ]
  85. for pattern in patterns:
  86. match = re.search(pattern, content, re.DOTALL)
  87. if match:
  88. return match.group(0).strip()
  89. return None
  90. def load_chapter_outline(project_root: Path, chapter_num: int, max_chars: int | None = 1500) -> str:
  91. outline_dir = project_root / "大纲"
  92. split_outline = _find_split_outline_file(outline_dir, chapter_num)
  93. if split_outline is not None:
  94. return split_outline.read_text(encoding="utf-8")
  95. volume_outline = _find_volume_outline_file(project_root, chapter_num)
  96. if volume_outline is None:
  97. return f"⚠️ 大纲文件不存在:第 {chapter_num} 章"
  98. outline = _extract_outline_section(volume_outline.read_text(encoding="utf-8"), chapter_num)
  99. if outline is None:
  100. return f"⚠️ 未找到第 {chapter_num} 章的大纲"
  101. if max_chars and len(outline) > max_chars:
  102. return outline[:max_chars] + "\n...(已截断)"
  103. return outline
  104. _PLOT_SECTION_FIELD_MAP = {
  105. "cbn": "cbn",
  106. "cpns": "cpns",
  107. "cen": "cen",
  108. "必须覆盖节点": "mandatory_nodes",
  109. "本章禁区": "prohibitions",
  110. }
  111. def _clean_plot_line(line: str) -> str:
  112. text = str(line or "").strip()
  113. text = re.sub(r"^[\-\*•]+\s*", "", text)
  114. text = re.sub(r"^\d+[\.、]\s*", "", text)
  115. return text.strip()
  116. def _append_plot_value(target: Dict[str, Any], field: str, value: str) -> None:
  117. value = _clean_plot_line(value)
  118. if not value:
  119. return
  120. if field in {"cpns", "mandatory_nodes", "prohibitions"}:
  121. target.setdefault(field, [])
  122. candidates = [value]
  123. if field in {"mandatory_nodes", "prohibitions"}:
  124. split_values = [part.strip() for part in re.split(r"[、,,;;|]+", value) if part.strip()]
  125. if split_values:
  126. candidates = split_values
  127. for item in candidates:
  128. if item not in target[field]:
  129. target[field].append(item)
  130. return
  131. if field not in target:
  132. target[field] = value
  133. def parse_chapter_plot_structure(outline_text: str) -> Dict[str, Any]:
  134. text = str(outline_text or "")
  135. if not text or text.startswith("⚠️"):
  136. return {}
  137. structure: Dict[str, Any] = {}
  138. current_field = ""
  139. for raw_line in text.splitlines():
  140. stripped = raw_line.strip()
  141. if not stripped:
  142. current_field = ""
  143. continue
  144. if re.match(r"^#{1,6}\s*第\s*\d+\s*章", stripped):
  145. current_field = ""
  146. continue
  147. cleaned = _clean_plot_line(stripped)
  148. matched_field = ""
  149. matched_value = ""
  150. for label, field in _PLOT_SECTION_FIELD_MAP.items():
  151. match = re.match(rf"^{re.escape(label)}\s*[::]\s*(.*)$", cleaned, re.IGNORECASE)
  152. if match:
  153. matched_field = field
  154. matched_value = match.group(1).strip()
  155. break
  156. if matched_field:
  157. current_field = matched_field
  158. _append_plot_value(structure, matched_field, matched_value)
  159. continue
  160. if current_field:
  161. _append_plot_value(structure, current_field, cleaned)
  162. cpns = structure.get("cpns") or []
  163. mandatory_nodes = structure.get("mandatory_nodes") or []
  164. prohibitions = structure.get("prohibitions") or []
  165. if not any([structure.get("cbn"), cpns, structure.get("cen"), mandatory_nodes, prohibitions]):
  166. return {}
  167. return {
  168. "cbn": str(structure.get("cbn") or "").strip(),
  169. "cpns": cpns,
  170. "cen": str(structure.get("cen") or "").strip(),
  171. "mandatory_nodes": mandatory_nodes,
  172. "prohibitions": prohibitions,
  173. "source": "chapter_outline",
  174. }
  175. def load_chapter_plot_structure(project_root: Path, chapter_num: int) -> Dict[str, Any]:
  176. outline = load_chapter_outline(project_root, chapter_num, max_chars=None)
  177. return parse_chapter_plot_structure(outline)