init_project.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 网文项目初始化脚本
  5. 目标:
  6. - 生成可运行的项目结构(webnovel-project)
  7. - 创建/更新 .webnovel/state.json(运行时真相)
  8. - 生成基础设定集与大纲模板文件(供 /webnovel-plan 与 /webnovel-write 使用)
  9. 说明:
  10. - 该脚本是命令 /webnovel-init 的“唯一允许的文件生成入口”(与命令文档保持一致)。
  11. - 生成的内容以“模板骨架”为主,便于 AI/作者后续补全;但保证所有关键文件存在。
  12. """
  13. from __future__ import annotations
  14. import argparse
  15. import json
  16. import subprocess
  17. import sys
  18. from datetime import datetime
  19. from pathlib import Path
  20. from runtime_compat import enable_windows_utf8_stdio
  21. from typing import Any, Dict, List
  22. import re
  23. # 安全修复:导入安全工具函数
  24. from security_utils import sanitize_commit_message, atomic_write_json, is_git_available
  25. # Windows 编码兼容性修复
  26. if sys.platform == "win32":
  27. enable_windows_utf8_stdio()
  28. def _read_text_if_exists(path: Path) -> str:
  29. if not path.exists():
  30. return ""
  31. return path.read_text(encoding="utf-8")
  32. def _write_text_if_missing(path: Path, content: str) -> None:
  33. path.parent.mkdir(parents=True, exist_ok=True)
  34. if path.exists():
  35. return
  36. path.write_text(content, encoding="utf-8")
  37. def _split_genre_keys(genre: str) -> list[str]:
  38. raw = (genre or "").strip()
  39. if not raw:
  40. return []
  41. # 支持复合题材:A+B / A+B / A、B / A与B
  42. raw = re.sub(r"[+/、]", "+", raw)
  43. raw = raw.replace("与", "+")
  44. parts = [p.strip() for p in raw.split("+") if p.strip()]
  45. return parts or [raw]
  46. def _normalize_genre_key(key: str) -> str:
  47. aliases = {
  48. "修仙/玄幻": "修仙",
  49. "玄幻修仙": "修仙",
  50. "玄幻": "修仙",
  51. "修真": "修仙",
  52. "都市修真": "都市异能",
  53. "都市高武": "高武",
  54. "都市奇闻": "都市脑洞",
  55. "古言脑洞": "古言",
  56. "游戏电竞": "电竞",
  57. "电竞文": "电竞",
  58. "直播": "直播文",
  59. "直播带货": "直播文",
  60. "主播": "直播文",
  61. "克系": "克苏鲁",
  62. "克系悬疑": "克苏鲁",
  63. }
  64. return aliases.get(key, key)
  65. def _apply_label_replacements(text: str, replacements: Dict[str, str]) -> str:
  66. if not text or not replacements:
  67. return text
  68. lines = text.splitlines()
  69. for i, line in enumerate(lines):
  70. stripped = line.lstrip()
  71. for label, value in replacements.items():
  72. if not value:
  73. continue
  74. prefix = f"- {label}:"
  75. if stripped.startswith(prefix):
  76. leading = line[: len(line) - len(stripped)]
  77. lines[i] = f"{leading}{prefix}{value}"
  78. return "\n".join(lines)
  79. def _parse_tier_map(raw: str) -> Dict[str, str]:
  80. result: Dict[str, str] = {}
  81. if not raw:
  82. return result
  83. for part in raw.split(";"):
  84. part = part.strip()
  85. if not part:
  86. continue
  87. if ":" in part:
  88. key, val = part.split(":", 1)
  89. result[key.strip()] = val.strip()
  90. return result
  91. def _render_team_rows(names: List[str], roles: List[str]) -> List[str]:
  92. rows = []
  93. for idx, name in enumerate(names):
  94. role = roles[idx] if idx < len(roles) else ""
  95. rows.append(f"| {name} | {role or '主线/副线'} | | | |")
  96. return rows
  97. def _ensure_state_schema(state: Dict[str, Any]) -> Dict[str, Any]:
  98. """确保 state.json 具备 v5.1 架构所需的字段集合(v5.4 沿用)。
  99. v5.1 变更:
  100. - entities_v3 和 alias_index 已迁移到 index.db,不再存储在 state.json
  101. - structured_relationships 已迁移到 index.db relationships 表
  102. - state.json 保持精简 (< 5KB)
  103. """
  104. state.setdefault("project_info", {})
  105. state.setdefault("progress", {})
  106. state.setdefault("protagonist_state", {})
  107. state.setdefault("relationships", {}) # update_state.py 需要此字段
  108. state.setdefault("disambiguation_warnings", [])
  109. state.setdefault("disambiguation_pending", [])
  110. state.setdefault("world_settings", {"power_system": [], "factions": [], "locations": []})
  111. state.setdefault("plot_threads", {"active_threads": [], "foreshadowing": []})
  112. state.setdefault("review_checkpoints", [])
  113. state.setdefault("chapter_meta", {})
  114. state.setdefault(
  115. "strand_tracker",
  116. {
  117. "last_quest_chapter": 0,
  118. "last_fire_chapter": 0,
  119. "last_constellation_chapter": 0,
  120. "current_dominant": "quest",
  121. "chapters_since_switch": 0,
  122. "history": [],
  123. },
  124. )
  125. # v5.1: entities_v3, alias_index, structured_relationships 已迁移到 index.db
  126. # 不再在 state.json 中初始化这些字段
  127. # progress schema evolution
  128. state["progress"].setdefault("current_chapter", 0)
  129. state["progress"].setdefault("total_words", 0)
  130. state["progress"].setdefault("last_updated", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
  131. state["progress"].setdefault("volumes_completed", [])
  132. state["progress"].setdefault("current_volume", 1)
  133. state["progress"].setdefault("volumes_planned", [])
  134. # protagonist schema evolution
  135. ps = state["protagonist_state"]
  136. ps.setdefault("name", "")
  137. ps.setdefault("power", {"realm": "", "layer": 1, "bottleneck": ""})
  138. ps.setdefault("location", {"current": "", "last_chapter": 0})
  139. ps.setdefault("golden_finger", {"name": "", "level": 1, "cooldown": 0, "skills": []})
  140. ps.setdefault("attributes", {})
  141. return state
  142. def _build_master_outline(target_chapters: int, *, chapters_per_volume: int = 50) -> str:
  143. volumes = (target_chapters - 1) // chapters_per_volume + 1 if target_chapters > 0 else 1
  144. lines: list[str] = [
  145. "# 总纲",
  146. "",
  147. "> 本文件为“总纲骨架”,用于 /webnovel-plan 细化为卷大纲与章纲。",
  148. "",
  149. "## 卷结构",
  150. "",
  151. ]
  152. for v in range(1, volumes + 1):
  153. start = (v - 1) * chapters_per_volume + 1
  154. end = min(v * chapters_per_volume, target_chapters)
  155. lines.extend(
  156. [
  157. f"### 第{v}卷(第{start}-{end}章)",
  158. "- 核心冲突:",
  159. "- 关键爽点:",
  160. "- 卷末高潮:",
  161. "- 主要登场角色:",
  162. "- 关键伏笔(埋/收):",
  163. "",
  164. ]
  165. )
  166. return "\n".join(lines).rstrip() + "\n"
  167. def _inject_volume_rows(template_text: str, target_chapters: int, *, chapters_per_volume: int = 50) -> str:
  168. """在总纲模板的卷表中注入卷行(若存在表头)。"""
  169. lines = template_text.splitlines()
  170. header_idx = None
  171. for i, line in enumerate(lines):
  172. if line.strip().startswith("| 卷号"):
  173. header_idx = i
  174. break
  175. if header_idx is None:
  176. return template_text
  177. insert_idx = header_idx + 2 if header_idx + 1 < len(lines) else len(lines)
  178. volumes = (target_chapters - 1) // chapters_per_volume + 1 if target_chapters > 0 else 1
  179. rows = []
  180. for v in range(1, volumes + 1):
  181. start = (v - 1) * chapters_per_volume + 1
  182. end = min(v * chapters_per_volume, target_chapters)
  183. rows.append(f"| {v} | | 第{start}-{end}章 | | |")
  184. # 避免重复插入(若模板已有数据行)
  185. existing = {line.strip() for line in lines}
  186. rows = [r for r in rows if r.strip() not in existing]
  187. return "\n".join(lines[:insert_idx] + rows + lines[insert_idx:])
  188. def init_project(
  189. project_dir: str,
  190. title: str,
  191. genre: str,
  192. *,
  193. protagonist_name: str = "",
  194. target_words: int = 2_000_000,
  195. target_chapters: int = 600,
  196. golden_finger_name: str = "",
  197. golden_finger_type: str = "",
  198. golden_finger_style: str = "",
  199. core_selling_points: str = "",
  200. protagonist_structure: str = "",
  201. heroine_config: str = "",
  202. heroine_names: str = "",
  203. heroine_role: str = "",
  204. co_protagonists: str = "",
  205. co_protagonist_roles: str = "",
  206. antagonist_tiers: str = "",
  207. world_scale: str = "",
  208. factions: str = "",
  209. power_system_type: str = "",
  210. social_class: str = "",
  211. resource_distribution: str = "",
  212. gf_visibility: str = "",
  213. gf_irreversible_cost: str = "",
  214. protagonist_desire: str = "",
  215. protagonist_flaw: str = "",
  216. protagonist_archetype: str = "",
  217. antagonist_level: str = "",
  218. target_reader: str = "",
  219. platform: str = "",
  220. currency_system: str = "",
  221. currency_exchange: str = "",
  222. sect_hierarchy: str = "",
  223. cultivation_chain: str = "",
  224. cultivation_subtiers: str = "",
  225. ) -> None:
  226. project_path = Path(project_dir).expanduser().resolve()
  227. if ".claude" in project_path.parts:
  228. raise SystemExit("Refusing to initialize a project inside .claude. Choose a different directory.")
  229. project_path.mkdir(parents=True, exist_ok=True)
  230. # 目录结构(同时兼容“卷目录”与后续扩展)
  231. directories = [
  232. ".webnovel/backups",
  233. ".webnovel/archive",
  234. ".webnovel/summaries",
  235. "设定集/角色库/主要角色",
  236. "设定集/角色库/次要角色",
  237. "设定集/角色库/反派角色",
  238. "设定集/物品库",
  239. "设定集/其他设定",
  240. "大纲",
  241. "正文/第1卷",
  242. "审查报告",
  243. ]
  244. for dir_path in directories:
  245. (project_path / dir_path).mkdir(parents=True, exist_ok=True)
  246. # state.json(创建或增量补齐)
  247. state_path = project_path / ".webnovel" / "state.json"
  248. if state_path.exists():
  249. try:
  250. state: Dict[str, Any] = json.loads(state_path.read_text(encoding="utf-8"))
  251. except json.JSONDecodeError:
  252. state = {}
  253. else:
  254. state = {}
  255. state = _ensure_state_schema(state)
  256. created_at = state.get("project_info", {}).get("created_at") or datetime.now().strftime("%Y-%m-%d")
  257. state["project_info"].update(
  258. {
  259. "title": title,
  260. "genre": genre,
  261. "created_at": created_at,
  262. "target_words": int(target_words),
  263. "target_chapters": int(target_chapters),
  264. # 下面字段属于“初始化元信息”,不影响运行时脚本
  265. "golden_finger_name": golden_finger_name,
  266. "golden_finger_type": golden_finger_type,
  267. "golden_finger_style": golden_finger_style,
  268. "core_selling_points": core_selling_points,
  269. "protagonist_structure": protagonist_structure,
  270. "heroine_config": heroine_config,
  271. "heroine_names": heroine_names,
  272. "heroine_role": heroine_role,
  273. "co_protagonists": co_protagonists,
  274. "co_protagonist_roles": co_protagonist_roles,
  275. "antagonist_tiers": antagonist_tiers,
  276. "world_scale": world_scale,
  277. "factions": factions,
  278. "power_system_type": power_system_type,
  279. "social_class": social_class,
  280. "resource_distribution": resource_distribution,
  281. "gf_visibility": gf_visibility,
  282. "gf_irreversible_cost": gf_irreversible_cost,
  283. "target_reader": target_reader,
  284. "platform": platform,
  285. "currency_system": currency_system,
  286. "currency_exchange": currency_exchange,
  287. "sect_hierarchy": sect_hierarchy,
  288. "cultivation_chain": cultivation_chain,
  289. "cultivation_subtiers": cultivation_subtiers,
  290. }
  291. )
  292. if protagonist_name:
  293. state["protagonist_state"]["name"] = protagonist_name
  294. gf_type_norm = (golden_finger_type or "").strip()
  295. if gf_type_norm in {"无", "无金手指", "none"}:
  296. state["protagonist_state"]["golden_finger"]["name"] = "无金手指"
  297. state["protagonist_state"]["golden_finger"]["level"] = 0
  298. state["protagonist_state"]["golden_finger"]["cooldown"] = 0
  299. elif golden_finger_name:
  300. state["protagonist_state"]["golden_finger"]["name"] = golden_finger_name
  301. # 确保 golden_finger 字段存在且可编辑
  302. if not state["protagonist_state"]["golden_finger"].get("name"):
  303. state["protagonist_state"]["golden_finger"]["name"] = "未命名金手指"
  304. state["progress"]["last_updated"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  305. state_path.parent.mkdir(parents=True, exist_ok=True)
  306. # 使用原子化写入(初始化不需要备份旧文件)
  307. atomic_write_json(state_path, state, use_lock=True, backup=False)
  308. # 读取内置模板(可选)
  309. script_dir = Path(__file__).resolve().parent
  310. templates_dir = script_dir.parent / "templates"
  311. output_templates_dir = templates_dir / "output"
  312. genre_key = (genre or "").strip()
  313. genre_keys = [_normalize_genre_key(k) for k in _split_genre_keys(genre_key)]
  314. genre_templates = []
  315. seen = set()
  316. for key in genre_keys:
  317. if not key or key in seen:
  318. continue
  319. seen.add(key)
  320. template_text = _read_text_if_exists(templates_dir / "genres" / f"{key}.md")
  321. if template_text:
  322. genre_templates.append(template_text.strip())
  323. genre_template = "\n\n---\n\n".join(genre_templates)
  324. golden_finger_templates = _read_text_if_exists(templates_dir / "golden-finger-templates.md")
  325. output_worldview = _read_text_if_exists(output_templates_dir / "设定集-世界观.md")
  326. output_power = _read_text_if_exists(output_templates_dir / "设定集-力量体系.md")
  327. output_protagonist = _read_text_if_exists(output_templates_dir / "设定集-主角卡.md")
  328. output_heroine = _read_text_if_exists(output_templates_dir / "设定集-女主卡.md")
  329. output_team = _read_text_if_exists(output_templates_dir / "设定集-主角组.md")
  330. output_golden_finger = _read_text_if_exists(output_templates_dir / "设定集-金手指.md")
  331. output_outline = _read_text_if_exists(output_templates_dir / "大纲-总纲.md")
  332. output_fusion = _read_text_if_exists(output_templates_dir / "复合题材-融合逻辑.md")
  333. output_antagonist = _read_text_if_exists(output_templates_dir / "设定集-反派设计.md")
  334. # 基础文件(只在缺失时生成,避免覆盖已有内容)
  335. now = datetime.now().strftime("%Y-%m-%d")
  336. worldview_content = output_worldview.strip() if output_worldview else ""
  337. if not worldview_content:
  338. worldview_content = "\n".join(
  339. [
  340. "# 世界观",
  341. "",
  342. f"> 项目:{title}|题材:{genre}|创建:{now}",
  343. "",
  344. "## 一句话世界观",
  345. "- (用一句话说明世界的核心规则与卖点)",
  346. "",
  347. "## 核心规则(设定即物理)",
  348. "- 规则1:",
  349. "- 规则2:",
  350. "- 规则3:",
  351. "",
  352. "## 势力与地理(简版)",
  353. "- 主要势力:",
  354. "- 关键地点:",
  355. "",
  356. "## 参考题材模板(可删/可改)",
  357. "",
  358. (genre_template.strip() + "\n") if genre_template else "(未找到对应题材模板,可自行补充)\n",
  359. ]
  360. ).rstrip() + "\n"
  361. else:
  362. worldview_content = _apply_label_replacements(
  363. worldview_content,
  364. {
  365. "大陆/位面数量": world_scale,
  366. "核心势力": factions,
  367. "社会阶层": social_class,
  368. "资源分配规则": resource_distribution,
  369. "宗门/组织层级": sect_hierarchy,
  370. "货币体系": currency_system,
  371. "兑换规则": currency_exchange,
  372. },
  373. )
  374. _write_text_if_missing(
  375. project_path / "设定集" / "世界观.md",
  376. worldview_content,
  377. )
  378. power_content = output_power.strip() if output_power else ""
  379. if not power_content:
  380. power_content = "\n".join(
  381. [
  382. "# 力量体系",
  383. "",
  384. f"> 项目:{title}|题材:{genre}|创建:{now}",
  385. "",
  386. "## 等级/境界划分",
  387. "- (列出从弱到强的等级,含突破条件与代价)",
  388. "",
  389. "## 技能/招式规则",
  390. "- 获得方式:",
  391. "- 成本与副作用:",
  392. "- 进阶与组合:",
  393. "",
  394. "## 禁止事项(防崩坏)",
  395. "- 未达等级不得使用高阶能力(设定即物理)",
  396. "- 新增能力必须申报并入库(发明需申报)",
  397. "",
  398. ]
  399. ).rstrip() + "\n"
  400. else:
  401. power_content = _apply_label_replacements(
  402. power_content,
  403. {
  404. "体系类型": power_system_type,
  405. "典型境界链(可选)": cultivation_chain,
  406. "小境界划分": cultivation_subtiers,
  407. },
  408. )
  409. _write_text_if_missing(
  410. project_path / "设定集" / "力量体系.md",
  411. power_content,
  412. )
  413. protagonist_content = output_protagonist.strip() if output_protagonist else ""
  414. if not protagonist_content:
  415. protagonist_content = "\n".join(
  416. [
  417. "# 主角卡",
  418. "",
  419. f"> 主角:{protagonist_name or '(待填写)'}|项目:{title}|创建:{now}",
  420. "",
  421. "## 三要素",
  422. f"- 欲望:{protagonist_desire or '(待填写)'}",
  423. f"- 弱点:{protagonist_flaw or '(待填写)'}",
  424. f"- 人设类型:{protagonist_archetype or '(待填写)'}",
  425. "",
  426. "## 初始状态(开局)",
  427. "- 身份:",
  428. "- 资源:",
  429. "- 约束:",
  430. "",
  431. "## 金手指概览",
  432. f"- 称呼:{golden_finger_name or '(待填写)'}",
  433. f"- 类型:{golden_finger_type or '(待填写)'}",
  434. f"- 风格:{golden_finger_style or '(待填写)'}",
  435. "- 成长曲线:",
  436. "",
  437. ]
  438. ).rstrip() + "\n"
  439. else:
  440. protagonist_content = _apply_label_replacements(
  441. protagonist_content,
  442. {
  443. "姓名": protagonist_name,
  444. "真正渴望(可能不自知)": protagonist_desire,
  445. "性格缺陷": protagonist_flaw,
  446. },
  447. )
  448. _write_text_if_missing(
  449. project_path / "设定集" / "主角卡.md",
  450. protagonist_content,
  451. )
  452. heroine_content = output_heroine.strip() if output_heroine else ""
  453. if heroine_content:
  454. heroine_content = _apply_label_replacements(
  455. heroine_content,
  456. {
  457. "姓名": heroine_names,
  458. "与主角关系定位(对手/盟友/共谋/牵制)": heroine_role,
  459. },
  460. )
  461. _write_text_if_missing(project_path / "设定集" / "女主卡.md", heroine_content)
  462. team_content = output_team.strip() if output_team else ""
  463. if team_content:
  464. names = [n.strip() for n in co_protagonists.split(",") if n.strip()] if co_protagonists else []
  465. roles = [r.strip() for r in co_protagonist_roles.split(",") if r.strip()] if co_protagonist_roles else []
  466. if names:
  467. lines = team_content.splitlines()
  468. new_rows = _render_team_rows(names, roles)
  469. replaced = False
  470. out_lines: List[str] = []
  471. for line in lines:
  472. if line.strip().startswith("| 主角A"):
  473. out_lines.extend(new_rows)
  474. replaced = True
  475. continue
  476. if replaced and line.strip().startswith("| 主角"):
  477. continue
  478. out_lines.append(line)
  479. team_content = "\n".join(out_lines)
  480. _write_text_if_missing(
  481. project_path / "设定集" / "主角组.md",
  482. team_content,
  483. )
  484. golden_finger_content = output_golden_finger.strip() if output_golden_finger else ""
  485. if not golden_finger_content:
  486. golden_finger_content = "\n".join(
  487. [
  488. "# 金手指设计",
  489. "",
  490. f"> 项目:{title}|题材:{genre}|创建:{now}",
  491. "",
  492. "## 选型",
  493. f"- 称呼:{golden_finger_name or '(待填写)'}",
  494. f"- 类型:{golden_finger_type or '(待填写)'}",
  495. f"- 风格:{golden_finger_style or '(待填写)'}",
  496. "",
  497. "## 规则(必须写清)",
  498. "- 触发条件:",
  499. "- 冷却/代价:",
  500. "- 上限:",
  501. "- 反噬/风险:",
  502. "",
  503. "## 成长曲线(章节规划)",
  504. "- Lv1:",
  505. "- Lv2:",
  506. "- Lv3:",
  507. "",
  508. "## 模板参考(可删/可改)",
  509. "",
  510. (golden_finger_templates.strip() + "\n") if golden_finger_templates else "(未找到金手指模板库)\n",
  511. ]
  512. ).rstrip() + "\n"
  513. else:
  514. golden_finger_content = _apply_label_replacements(
  515. golden_finger_content,
  516. {
  517. "类型": golden_finger_type,
  518. "读者可见度": gf_visibility,
  519. "不可逆代价": gf_irreversible_cost,
  520. },
  521. )
  522. _write_text_if_missing(
  523. project_path / "设定集" / "金手指设计.md",
  524. golden_finger_content,
  525. )
  526. fusion_content = output_fusion.strip() if output_fusion else ""
  527. if fusion_content:
  528. _write_text_if_missing(
  529. project_path / "设定集" / "复合题材-融合逻辑.md",
  530. fusion_content,
  531. )
  532. antagonist_content = output_antagonist.strip() if output_antagonist else ""
  533. if not antagonist_content:
  534. antagonist_content = "\n".join(
  535. [
  536. "# 反派设计",
  537. "",
  538. f"> 项目:{title}|创建:{now}",
  539. "",
  540. f"- 反派等级:{antagonist_level or '(待填写)'}",
  541. "- 动机:",
  542. "- 资源/势力:",
  543. "- 与主角的镜像关系:",
  544. "- 终局:",
  545. "",
  546. ]
  547. ).rstrip() + "\n"
  548. else:
  549. tier_map = _parse_tier_map(antagonist_tiers)
  550. if tier_map:
  551. lines = antagonist_content.splitlines()
  552. out_lines = []
  553. for line in lines:
  554. if line.strip().startswith("| 小反派"):
  555. name = tier_map.get("小反派", "")
  556. out_lines.append(f"| 小反派 | {name} | 前期 | | |")
  557. continue
  558. if line.strip().startswith("| 中反派"):
  559. name = tier_map.get("中反派", "")
  560. out_lines.append(f"| 中反派 | {name} | 中期 | | |")
  561. continue
  562. if line.strip().startswith("| 大反派"):
  563. name = tier_map.get("大反派", "")
  564. out_lines.append(f"| 大反派 | {name} | 后期 | | |")
  565. continue
  566. out_lines.append(line)
  567. antagonist_content = "\n".join(out_lines)
  568. _write_text_if_missing(project_path / "设定集" / "反派设计.md", antagonist_content)
  569. outline_content = output_outline.strip() if output_outline else ""
  570. if outline_content:
  571. outline_content = _inject_volume_rows(outline_content, int(target_chapters)).rstrip() + "\n"
  572. else:
  573. outline_content = _build_master_outline(int(target_chapters))
  574. _write_text_if_missing(project_path / "大纲" / "总纲.md", outline_content)
  575. _write_text_if_missing(
  576. project_path / "大纲" / "爽点规划.md",
  577. "\n".join(
  578. [
  579. "# 爽点规划",
  580. "",
  581. f"> 项目:{title}|题材:{genre}|创建:{now}",
  582. "",
  583. "## 核心卖点(来自初始化输入)",
  584. f"- {core_selling_points or '(待填写,建议 1-3 条,用逗号分隔)'}",
  585. "",
  586. "## 密度目标(建议)",
  587. "- 每章至少 1 个小爽点",
  588. "- 每 5 章至少 1 个大爽点",
  589. "",
  590. "## 分布表(示例,可改)",
  591. "",
  592. "| 章节范围 | 主导爽点类型 | 备注 |",
  593. "|---|---|---|",
  594. "| 1-5 | 金手指/打脸/反转 | 开篇钩子 + 立人设 |",
  595. "| 6-10 | 升级/收获 | 进入主线节奏 |",
  596. "",
  597. ]
  598. ),
  599. )
  600. # Git 初始化(仅当项目目录内尚无 .git 且 Git 可用)
  601. git_dir = project_path / ".git"
  602. if not git_dir.exists():
  603. if not is_git_available():
  604. print("\n⚠️ Git 不可用,跳过版本控制初始化")
  605. print("💡 如需启用 Git 版本控制,请安装 Git: https://git-scm.com/")
  606. else:
  607. print("\nInitializing Git repository...")
  608. try:
  609. subprocess.run(["git", "init"], cwd=project_path, check=True, capture_output=True, text=True)
  610. gitignore_file = project_path / ".gitignore"
  611. if not gitignore_file.exists():
  612. gitignore_file.write_text(
  613. """# Python
  614. __pycache__/
  615. *.py[cod]
  616. *.so
  617. # Temporary files
  618. *.tmp
  619. *.bak
  620. .DS_Store
  621. # IDE
  622. .vscode/
  623. .idea/
  624. # Don't ignore .webnovel (we need to track state.json)
  625. # But ignore cache files
  626. .webnovel/context_cache.json
  627. .webnovel/*.lock
  628. .webnovel/*.bak
  629. """,
  630. encoding="utf-8",
  631. )
  632. subprocess.run(["git", "add", "."], cwd=project_path, check=True, capture_output=True)
  633. # 安全修复:清理 title 防止命令注入
  634. safe_title = sanitize_commit_message(title)
  635. subprocess.run(
  636. ["git", "commit", "-m", f"初始化网文项目:{safe_title}"],
  637. cwd=project_path,
  638. check=True,
  639. capture_output=True,
  640. )
  641. print("Git initialized.")
  642. except subprocess.CalledProcessError as e:
  643. print(f"Git init failed (non-fatal): {e}")
  644. print(f"\nProject initialized at: {project_path}")
  645. print("Key files:")
  646. print(" - .webnovel/state.json")
  647. print(" - 设定集/世界观.md")
  648. print(" - 设定集/力量体系.md")
  649. print(" - 设定集/主角卡.md")
  650. print(" - 设定集/金手指设计.md")
  651. print(" - 大纲/总纲.md")
  652. print(" - 大纲/爽点规划.md")
  653. def main() -> None:
  654. parser = argparse.ArgumentParser(description="网文项目初始化脚本(生成项目结构 + state.json + 基础模板)")
  655. parser.add_argument("project_dir", help="项目目录(建议 ./webnovel-project)")
  656. parser.add_argument("title", help="小说标题")
  657. parser.add_argument(
  658. "genre",
  659. help="题材类型(可用“+”组合,如:都市脑洞+规则怪谈;示例:修仙/系统流/都市异能/古言/现实题材)",
  660. )
  661. parser.add_argument("--protagonist-name", default="", help="主角姓名")
  662. parser.add_argument("--target-words", type=int, default=2_000_000, help="目标总字数(默认 2000000)")
  663. parser.add_argument("--target-chapters", type=int, default=600, help="目标总章节数(默认 600)")
  664. parser.add_argument("--golden-finger-name", default="", help="金手指称呼/系统名(建议读者可见的代号)")
  665. parser.add_argument("--golden-finger-type", default="", help="金手指类型(如 系统流/鉴定流/签到流)")
  666. parser.add_argument("--golden-finger-style", default="", help="金手指风格(如 冷漠工具型/毒舌吐槽型)")
  667. parser.add_argument("--core-selling-points", default="", help="核心卖点(逗号分隔)")
  668. parser.add_argument("--protagonist-structure", default="", help="主角结构(单主角/多主角)")
  669. parser.add_argument("--heroine-config", default="", help="女主配置(无女主/单女主/多女主)")
  670. parser.add_argument("--heroine-names", default="", help="女主姓名(多个用逗号分隔)")
  671. parser.add_argument("--heroine-role", default="", help="女主定位(事业线/情感线/对抗线)")
  672. parser.add_argument("--co-protagonists", default="", help="多主角姓名(逗号分隔)")
  673. parser.add_argument("--co-protagonist-roles", default="", help="多主角定位(逗号分隔)")
  674. parser.add_argument("--antagonist-tiers", default="", help="反派分层(如 小反派:张三;中反派:李四;大反派:王五)")
  675. parser.add_argument("--world-scale", default="", help="世界规模")
  676. parser.add_argument("--factions", default="", help="势力格局/核心势力")
  677. parser.add_argument("--power-system-type", default="", help="力量体系类型")
  678. parser.add_argument("--social-class", default="", help="社会阶层")
  679. parser.add_argument("--resource-distribution", default="", help="资源分配")
  680. parser.add_argument("--gf-visibility", default="", help="金手指可见度(明牌/半明牌/暗牌)")
  681. parser.add_argument("--gf-irreversible-cost", default="", help="金手指不可逆代价")
  682. parser.add_argument("--currency-system", default="", help="货币体系")
  683. parser.add_argument("--currency-exchange", default="", help="货币兑换/面值规则")
  684. parser.add_argument("--sect-hierarchy", default="", help="宗门/组织层级")
  685. parser.add_argument("--cultivation-chain", default="", help="典型境界链")
  686. parser.add_argument("--cultivation-subtiers", default="", help="小境界划分(初/中/后/巅 等)")
  687. # 深度模式可选参数(用于预填模板)
  688. parser.add_argument("--protagonist-desire", default="", help="主角核心欲望(深度模式)")
  689. parser.add_argument("--protagonist-flaw", default="", help="主角性格弱点(深度模式)")
  690. parser.add_argument("--protagonist-archetype", default="", help="主角人设类型(深度模式)")
  691. parser.add_argument("--antagonist-level", default="", help="反派等级(深度模式)")
  692. parser.add_argument("--target-reader", default="", help="目标读者(深度模式)")
  693. parser.add_argument("--platform", default="", help="发布平台(深度模式)")
  694. args = parser.parse_args()
  695. init_project(
  696. args.project_dir,
  697. args.title,
  698. args.genre,
  699. protagonist_name=args.protagonist_name,
  700. target_words=args.target_words,
  701. target_chapters=args.target_chapters,
  702. golden_finger_name=args.golden_finger_name,
  703. golden_finger_type=args.golden_finger_type,
  704. golden_finger_style=args.golden_finger_style,
  705. core_selling_points=args.core_selling_points,
  706. protagonist_structure=args.protagonist_structure,
  707. heroine_config=args.heroine_config,
  708. heroine_names=args.heroine_names,
  709. heroine_role=args.heroine_role,
  710. co_protagonists=args.co_protagonists,
  711. co_protagonist_roles=args.co_protagonist_roles,
  712. antagonist_tiers=args.antagonist_tiers,
  713. world_scale=args.world_scale,
  714. factions=args.factions,
  715. power_system_type=args.power_system_type,
  716. social_class=args.social_class,
  717. resource_distribution=args.resource_distribution,
  718. gf_visibility=args.gf_visibility,
  719. gf_irreversible_cost=args.gf_irreversible_cost,
  720. protagonist_desire=args.protagonist_desire,
  721. protagonist_flaw=args.protagonist_flaw,
  722. protagonist_archetype=args.protagonist_archetype,
  723. antagonist_level=args.antagonist_level,
  724. target_reader=args.target_reader,
  725. platform=args.platform,
  726. currency_system=args.currency_system,
  727. currency_exchange=args.currency_exchange,
  728. sect_hierarchy=args.sect_hierarchy,
  729. cultivation_chain=args.cultivation_chain,
  730. cultivation_subtiers=args.cultivation_subtiers,
  731. )
  732. if __name__ == "__main__":
  733. main()