-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_ui.py
More file actions
373 lines (316 loc) · 14.5 KB
/
export_ui.py
File metadata and controls
373 lines (316 loc) · 14.5 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import os
import json
import re
import datetime
from typing import Dict
from ui_utils import ui
from project_data_manager import project_data_manager
from config import get_export_base_dir
from project_manager import project_manager
def get_novel_name():
"""Helper to get the current novel's name."""
dm = project_data_manager.get_data_manager()
if not dm: return "未命名小说"
data = dm.read_theme_one_line()
return data.get("novel_name", "未命名小说") if isinstance(data, dict) else "未命名小说"
def get_export_dir():
"""Gets the export directory for the current project."""
try:
export_base_dir = get_export_base_dir()
active_project = project_manager.get_active_project()
export_dir = export_base_dir / active_project if active_project else export_base_dir / "Default"
export_dir.mkdir(parents=True, exist_ok=True)
return export_dir
except Exception as e:
ui.print_warning(f"⚠️ 获取导出目录时出错,使用默认导出文件夹: {e}")
export_dir = "exports"
os.makedirs(export_dir, exist_ok=True)
return export_dir
_METADATA_KEYS = {
"patch_log",
"chapter_no",
"pov",
"value_shift",
"setups",
"payoffs",
"canon_alignment",
"scores",
"variants",
"multi_use_features",
"hazards",
"symbolism",
"uses",
"secrets",
"tells",
"exploitable_flaws",
"value_axis"
}
def _strip_trailing_metadata(content: str) -> str:
"""移除正文末尾附带的 JSON 元数据(如 patch_log)。"""
if not content:
return content or ""
stripped = content.rstrip()
while True:
match = re.search(r'\n\{[\s\S]*\}\s*$', stripped)
if not match:
break
candidate = stripped[match.start():].lstrip()
if not candidate.startswith("{"):
break
try:
parsed = json.loads(candidate)
except json.JSONDecodeError:
break
if isinstance(parsed, Dict) and any(key in parsed for key in _METADATA_KEYS):
stripped = stripped[:match.start()].rstrip()
continue
break
return stripped
def _get_clean_chapter_content(chapter_data: Dict) -> str:
"""获取清理后的章节正文内容。"""
raw_content = chapter_data.get('content', '') or ""
return _strip_trailing_metadata(raw_content)
def _compute_word_count(content: str) -> int:
"""计算正文字数(忽略空格、换行和制表符)。"""
return len(content.replace(' ', '').replace('\n', '').replace('\t', ''))
def handle_novel_export():
"""Main UI handler for exporting the novel."""
dm = project_data_manager.get_data_manager()
if not dm:
ui.print_warning("无活动项目,无法导出。")
ui.pause()
return
chapters = dm.read_chapter_outline()
novel_chapters = dm.read_novel_chapters()
if not novel_chapters:
ui.print_warning("\n当前没有小说正文可导出。")
ui.pause()
return
while True:
action = ui.display_menu("请选择导出操作:", ["导出完整小说", "导出单个章节", "导出章节范围", "返回"])
if action == '1':
export_complete_novel(chapters, novel_chapters)
elif action == '2':
export_single_chapter(chapters, novel_chapters)
elif action == '3':
export_chapter_range(chapters, novel_chapters)
elif action == '0':
break
def export_single_chapter(chapters, novel_chapters):
"""Exports a single chapter."""
chapter_map = {f"chapter_{i+1}": ch.get('title', f'第{i+1}章') for i, ch in enumerate(chapters)}
available_chapters = [title for key, title in chapter_map.items() if key in novel_chapters]
choice_str = ui.display_menu("请选择要导出的章节:", available_chapters + ["返回"])
# 优先处理返回选项
if choice_str == '0':
return
if choice_str.isdigit() and int(choice_str) <= len(available_chapters):
choice_index = int(choice_str) - 1
# Find the correct chapter key
selected_title = available_chapters[choice_index]
chapter_key = next(key for key, title in chapter_map.items() if title == selected_title)
export_dir = get_export_dir()
chapter_data = novel_chapters.get(chapter_key, {})
clean_content = _get_clean_chapter_content(chapter_data)
title = chapter_data.get('title', selected_title)
novel_name = get_novel_name()
timestamp = datetime.datetime.now()
timestamp_str = timestamp.strftime("%Y%m%d_%H%M%S")
display_timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
# 使用已有的字数数据,如果没有则重新计算
word_count = _compute_word_count(clean_content)
filename = f"{novel_name}_{title}_{timestamp_str}.txt"
try:
with open(os.path.join(export_dir, filename), 'w', encoding='utf-8') as f:
# 写入元数据头部
f.write(f"《{novel_name}》\n")
f.write("=" * 30 + "\n")
f.write(f"导出时间: {display_timestamp}\n")
# 获取章节号码
chapter_num = int(chapter_key.split('_')[1])
f.write(f"导出章节: 第{chapter_num}章 {title}\n")
f.write(f"字数: {word_count} 字\n")
f.write("=" * 30 + "\n\n")
# 写入章节标题
f.write(f"第{chapter_num}章 {title}\n")
f.write("=" * 30 + "\n\n")
# 写入正文内容(移除附加的JSON元数据)
f.write(clean_content)
# 添加AI生成作品说明
f.write("\n\n" + "=" * 30 + "\n")
f.write("这是 AI 生成的作品\n")
f.write("如有问题或建议\n")
f.write("请访问GitHub页面:\n")
f.write("https://github.com/hahagood/MetaNovel-Engine\n")
ui.print_success(f"章节 '{title}' 已导出到: {os.path.join(export_dir, filename)}\n")
except Exception as e:
ui.print_error(f"导出失败: {e}")
ui.pause()
def export_chapter_range(chapters, novel_chapters):
"""Exports a range of chapters."""
chapter_map = {f"chapter_{i+1}": ch.get('title', f'第{i+1}章') for i, ch in enumerate(chapters)}
available_chapters = [(key, title) for key, title in chapter_map.items() if key in novel_chapters]
if len(available_chapters) < 2:
ui.print_warning("需要至少2个章节才能使用范围导出功能。")
ui.pause()
return
ui.print_info("可用章节:")
for i, (key, title) in enumerate(available_chapters, 1):
ui.print_info(f"{i}. {title}")
try:
# 改进用户交互:支持范围格式如 "1-3" 或分别输入
range_input = ui.get_user_input(f"请输入章节范围 (如: 1-{len(available_chapters)} 或 1,3 表示第1到第{len(available_chapters)}章): ")
# 解析范围输入
start_idx = None
end_idx = None
# 支持 "1-3" 格式
if '-' in range_input:
try:
parts = range_input.split('-')
if len(parts) == 2:
start_idx = int(parts[0].strip()) - 1
end_idx = int(parts[1].strip()) - 1
except ValueError:
pass
# 支持 "1,3" 或 "1 3" 格式
elif ',' in range_input or ' ' in range_input:
try:
separator = ',' if ',' in range_input else ' '
parts = [p.strip() for p in range_input.split(separator) if p.strip()]
if len(parts) == 2:
start_idx = int(parts[0]) - 1
end_idx = int(parts[1]) - 1
except ValueError:
pass
# 支持单个数字(视为单章节)
elif range_input.isdigit():
idx = int(range_input) - 1
start_idx = end_idx = idx
# 验证输入
if (start_idx is None or end_idx is None or
start_idx < 0 or end_idx >= len(available_chapters) or start_idx > end_idx):
ui.print_warning("无效的章节范围。请使用格式如 '1-3' 或 '1,3' 来指定范围。")
ui.pause()
return
# 选择范围内的章节
selected_chapters = available_chapters[start_idx:end_idx + 1]
export_dir = get_export_dir()
novel_name = get_novel_name()
timestamp = datetime.datetime.now()
timestamp_str = timestamp.strftime("%Y%m%d_%H%M%S")
display_timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
# 使用已有的字数数据计算总字数和生成章节列表
total_word_count = 0
chapter_titles = []
clean_contents = {}
for key, title in selected_chapters:
chapter_data = novel_chapters[key]
clean_content = _get_clean_chapter_content(chapter_data)
clean_contents[key] = clean_content
total_word_count += _compute_word_count(clean_content)
chapter_titles.append(title)
chapters_str = "、".join(chapter_titles)
# 生成更清晰的文件名
if start_idx == end_idx:
filename = f"{novel_name}_{chapter_titles[0]}_{timestamp_str}.txt"
else:
filename = f"{novel_name}_第{start_idx+1}-{end_idx+1}章_{timestamp_str}.txt"
filepath = os.path.join(export_dir, filename)
with open(filepath, 'w', encoding='utf-8') as f:
# 写入元数据头部
f.write(f"《{novel_name}》\n")
f.write("=" * 30 + "\n")
f.write(f"导出时间: {display_timestamp}\n")
# 根据章节范围显示不同的导出信息
if start_idx == end_idx:
chapter_num = int(selected_chapters[0][0].split('_')[1])
f.write(f"导出章节: 第{chapter_num}章 {selected_chapters[0][1]}\n")
else:
start_num = int(selected_chapters[0][0].split('_')[1])
end_num = int(selected_chapters[-1][0].split('_')[1])
f.write(f"导出章节: 第{start_num}章到第{end_num}章\n")
f.write(f"字数: {total_word_count} 字\n")
f.write("=" * 30 + "\n\n")
# 直接写入章节内容,不重复作品名
for key, title in selected_chapters:
chapter_data = novel_chapters[key]
chapter_num = int(key.split('_')[1])
f.write(f"第{chapter_num}章 {title}\n")
f.write("=" * 30 + "\n\n")
clean_content = clean_contents.get(key)
if clean_content is None:
clean_content = _get_clean_chapter_content(chapter_data)
f.write(clean_content)
f.write("\n\n---\n\n")
# 添加AI生成作品说明
f.write("\n" + "=" * 30 + "\n")
f.write("这是 AI 生成的作品\n")
f.write("如有问题或建议\n")
f.write("请访问GitHub页面:\n")
f.write("https://github.com/hahagood/MetaNovel-Engine\n")
if start_idx == end_idx:
ui.print_success(f"章节 '{chapter_titles[0]}' 已导出到: {filepath}\n")
else:
ui.print_success(f"章节 {start_idx+1}-{end_idx+1} 已导出到: {filepath}\n")
except Exception as e:
ui.print_error(f"导出失败: {e}")
ui.pause()
def export_complete_novel(chapters, novel_chapters):
"""Exports the complete novel to a single file."""
export_dir = get_export_dir()
novel_name = get_novel_name()
timestamp = datetime.datetime.now()
timestamp_str = timestamp.strftime("%Y%m%d_%H%M%S")
display_timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
filename = f"{novel_name}_全本_{timestamp_str}.txt"
filepath = os.path.join(export_dir, filename)
# 使用已有的字数数据计算总字数
total_word_count = 0
sorted_keys = sorted(novel_chapters.keys(), key=lambda k: int(k.split('_')[1]))
chapter_titles = []
clean_contents = {}
for key in sorted_keys:
chapter_data = novel_chapters[key]
clean_content = _get_clean_chapter_content(chapter_data)
clean_contents[key] = clean_content
total_word_count += _compute_word_count(clean_content)
chapter_titles.append(chapter_data.get('title', '无标题'))
# 生成章节列表字符串(包含章节号)
chapters_with_numbers = []
for key in sorted_keys:
chapter_data = novel_chapters[key]
chapter_num = int(key.split('_')[1])
chapter_title = chapter_data.get('title', '无标题')
chapters_with_numbers.append(f"第{chapter_num}章 {chapter_title}")
chapters_str = "、".join(chapters_with_numbers)
try:
with open(filepath, 'w', encoding='utf-8') as f:
# 写入元数据头部
f.write(f"《{novel_name}》\n")
f.write("=" * 30 + "\n")
f.write(f"导出时间: {display_timestamp}\n")
f.write(f"导出章节: 全文导出\n")
f.write(f"字数: {total_word_count} 字\n")
f.write("=" * 30 + "\n\n")
# 直接写入章节内容,不重复作品名
for key in sorted_keys:
chapter_data = novel_chapters[key]
chapter_num = int(key.split('_')[1])
chapter_title = chapter_data.get('title', '无标题')
f.write(f"第{chapter_num}章 {chapter_title}\n")
f.write("=" * 30 + "\n\n")
clean_content = clean_contents.get(key)
if clean_content is None:
clean_content = _get_clean_chapter_content(chapter_data)
f.write(clean_content)
f.write("\n\n---\n\n")
# 添加AI生成作品说明
f.write("\n" + "=" * 30 + "\n")
f.write("这是 AI 生成的作品\n")
f.write("如有问题或建议\n")
f.write("请访问GitHub页面:\n")
f.write("https://github.com/hahagood/MetaNovel-Engine\n")
ui.print_success(f"完整小说已导出到: {filepath}\n")
except Exception as e:
ui.print_error(f"导出失败: {e}")
ui.pause()