-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
332 lines (275 loc) · 12.5 KB
/
plugin.py
File metadata and controls
332 lines (275 loc) · 12.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
import os
import sys
import json
import shutil
import re
import tkinter as tk
import traceback
from tkinter import messagebox, scrolledtext, filedialog
# --- 基礎環境設定 ---
plugin_dir = os.path.dirname(os.path.realpath(__file__))
if plugin_dir not in sys.path:
sys.path.insert(0, plugin_dir)
CONFIG_FILE = os.path.join(plugin_dir, "config.json")
DICT_DIR = os.path.join(plugin_dir, "dictionary")
if not os.path.exists(DICT_DIR):
os.makedirs(DICT_DIR)
try:
from opencc import OpenCC
except ImportError:
print("錯誤:找不到 'opencc'。")
sys.exit(1)
# --- 核心處理器 (極致優化 Trie 字典樹版) ---
class UltraConverter:
def __init__(self, final_dict, mode='s2twp'):
self.cc = OpenCC(mode)
# 保護完整的 style/script 區塊
self.block_re = re.compile(r'<(script|style)[^>]*>.*?</\1>', re.IGNORECASE | re.DOTALL)
self.tag_re = re.compile(r'(<[^>]+>|&[a-zA-Z#0-9]+;)')
self.place_re = re.compile(r'\uE000(\d+)\uE001')
self.char_map = str.maketrans({'“': '「', '”': '」', '‘': '『', '’': '』'})
self.trie = {}
if final_dict:
for key, val in final_dict.items():
node = self.trie
for char in key:
node = node.setdefault(char, {})
node['VALUE'] = val
def process(self, data):
if not data: return data
tag_storage = []
def protect(m):
tag_storage.append(m.group(0))
return f"\uE000{len(tag_storage)-1}\uE001"
# 1. 先保護 script 與 style 區塊,避免裡面的 JS/CSS 被轉換
protected = self.block_re.sub(protect, data)
# 2. 再保護一般 HTML 標籤與實體字元
protected = self.tag_re.sub(protect, protected)
text = self.cc.convert(protected)
text = text.translate(self.char_map)
if self.trie:
text = self.fast_trie_replace(text)
text = re.sub(r'"([^"]+)"', r'「\1」', text)
text = re.sub(r"'([^']+)'", r'『\1』', text)
return self.place_re.sub(lambda m: tag_storage[int(m.group(1))], text)
def fast_trie_replace(self, text):
res = []
i = 0
length = len(text)
trie = self.trie
while i < length:
best_match_val = None
best_match_len = 0
curr_node = trie
for j in range(i, length):
char = text[j]
if char not in curr_node: break
curr_node = curr_node[char]
if 'VALUE' in curr_node:
best_match_val = curr_node['VALUE']
best_match_len = j - i + 1
if best_match_val is not None:
res.append(best_match_val)
i += best_match_len
else:
res.append(text[i])
i += 1
return "".join(res)
# --- GUI 字典管理類別 ---
class MultiDictManager:
def __init__(self, dict_dir):
self.root = tk.Tk()
self.root.title("MultiDictOpenCC")
self.root.geometry("850x750")
self.dict_dir = dict_dir
self.dict_contents = {} # 延遲載入:初始為空
self.dict_order = []
self.dict_enabled = {}
self.current_file = None
self.success = False
self.saved_prefs = self.load_prefs()
self.setup_gui()
def load_prefs(self):
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception:
return {}
return {}
def setup_gui(self):
# 左側面板
left = tk.LabelFrame(self.root, text="1. 字典管理")
left.pack(side="left", fill="y", padx=10, pady=5)
self.listbox = tk.Listbox(left, width=35, height=10)
self.listbox.pack(padx=5, pady=5)
self.listbox.bind('<<ListboxSelect>>', self.on_select_file)
f1 = tk.Frame(left)
f1.pack(fill="x")
tk.Button(f1, text="➕ 新增", command=self.add_new_dict).pack(side="left", expand=True, fill="x")
tk.Button(f1, text="▲", command=self.move_up).pack(side="left", expand=True, fill="x")
tk.Button(f1, text="▼", command=self.move_down).pack(side="left", expand=True, fill="x")
self.btn_run = tk.Button(left, text="★ 執行 ★", command=self.on_run, bg="#0078d7", fg="white", font=("Arial", 12, "bold"), pady=10)
self.btn_run.pack(fill="x", padx=5, pady=10)
self.check_container = tk.Frame(left)
self.check_container.pack(fill="both", expand=True)
self.init_check_list()
self.refresh_file_list()
# 右側面板
right = tk.Frame(self.root)
right.pack(side="right", fill="both", expand=True, padx=10, pady=5)
# 右側頂部控制列
top_ctrl = tk.Frame(right)
top_ctrl.pack(fill="x", pady=2)
self.lbl_editing = tk.Label(top_ctrl, text="請選擇左側字典進行編輯...", fg="blue", font=("Arial", 10, "bold"))
self.lbl_editing.pack(side="left")
# 💾 手動存檔按鈕
self.btn_save_dict = tk.Button(top_ctrl, text="💾 儲存當前字典修改", command=self.manual_save_dict, bg="#ffc107")
self.btn_save_dict.pack(side="right")
self.text_area = scrolledtext.ScrolledText(right, height=40)
self.text_area.pack(fill="both", expand=True)
def init_check_list(self):
self.canvas = tk.Canvas(self.check_container, width=220)
self.scrollbar = tk.Scrollbar(self.check_container, command=self.canvas.yview)
self.scroll_frame = tk.Frame(self.canvas)
self.canvas.create_window((0,0), window=self.scroll_frame, anchor="nw")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both", expand=True)
self.scrollbar.pack(side="right", fill="y")
self.scroll_frame.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
def refresh_file_list(self):
# 清空舊的
self.listbox.delete(0, tk.END)
for widget in self.scroll_frame.winfo_children():
widget.destroy()
files = sorted([f for f in os.listdir(self.dict_dir) if f.endswith('.txt')])
self.dict_order = files
for f in files:
self.listbox.insert(tk.END, f)
if f not in self.dict_enabled:
self.dict_enabled[f] = tk.BooleanVar(value=self.saved_prefs.get(f, False))
# 延遲載入優化:此處不讀取檔案內容
tk.Checkbutton(self.scroll_frame, text=f, variable=self.dict_enabled[f]).pack(anchor="w")
def _ensure_file_loaded(self, file_name):
"""輔助方法:確保特定字典檔的內容已被載入記憶體"""
if file_name not in self.dict_contents:
file_path = os.path.join(self.dict_dir, file_name)
try:
with open(file_path, 'r', encoding='utf-8') as obj:
self.dict_contents[file_name] = obj.read()
except Exception as e:
print(f"讀取字典檔 {file_name} 失敗: {e}")
self.dict_contents[file_name] = ""
def on_select_file(self, e):
self.auto_save_current_edit() # 切換前,先把舊的暫存
sel = self.listbox.curselection()
if sel:
self.current_file = self.listbox.get(sel[0])
self.lbl_editing.config(text=f"正在編輯: {self.current_file}")
# 點擊時才真正載入檔案內容
self._ensure_file_loaded(self.current_file)
self.text_area.delete("1.0", tk.END)
self.text_area.insert(tk.END, self.dict_contents[self.current_file])
def auto_save_current_edit(self):
"""僅更新記憶體中的內容快取"""
if self.current_file:
self.dict_contents[self.current_file] = self.text_area.get("1.0", tk.END)
def manual_save_dict(self):
"""實體寫入硬碟 .txt 檔案"""
if not self.current_file:
messagebox.showinfo("提示", "請先選擇一個字典檔進行修改。")
return
# 讀取當前畫面輸入
current_text = self.text_area.get("1.0", tk.END).strip()
self.dict_contents[self.current_file] = current_text
file_path = os.path.join(self.dict_dir, self.current_file)
try:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(current_text)
messagebox.showinfo("成功", f"字典檔 【{self.current_file}】 已成功儲存!")
except Exception as e:
messagebox.showerror("錯誤", f"儲存檔案失敗:{e}")
def add_new_dict(self):
p = filedialog.askopenfilename(filetypes=[("Text", "*.txt")])
if p:
shutil.copy(p, os.path.join(self.dict_dir, os.path.basename(p)))
self.refresh_file_list()
def move_up(self):
i = self.listbox.curselection()
if i and i[0] > 0:
idx = i[0]
self.dict_order[idx], self.dict_order[idx-1] = self.dict_order[idx-1], self.dict_order[idx]
v = self.listbox.get(idx)
self.listbox.delete(idx)
self.listbox.insert(idx-1, v)
self.listbox.select_set(idx-1)
def move_down(self):
i = self.listbox.curselection()
if i and i[0] < self.listbox.size()-1:
idx = i[0]
self.dict_order[idx], self.dict_order[idx+1] = self.dict_order[idx+1], self.dict_order[idx]
v = self.listbox.get(idx)
self.listbox.delete(idx)
self.listbox.insert(idx+1, v)
self.listbox.select_set(idx+1)
def on_run(self):
self.auto_save_current_edit()
# 1. 儲存勾選偏好 (config.json)
try:
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump({f: v.get() for f, v in self.dict_enabled.items()}, f, ensure_ascii=False, indent=4)
except Exception as e:
print(f"儲存設定檔失敗: {e}")
# 2. 一次性把所有記憶體中「有修改過/載入過」的持久寫入到實體檔案中 (全自動存檔)
for f, c in self.dict_contents.items():
try:
with open(os.path.join(self.dict_dir, f), 'w', encoding='utf-8') as obj:
obj.write(c.strip())
except Exception as e:
print(f"自動存檔 {f} 失敗: {e}")
self.success = True
self.root.destroy()
def show(self):
self.root.mainloop()
return self.success
# --- Sigil 進入點 ---
def run(bc):
gui = MultiDictManager(DICT_DIR)
if not gui.show():
return 0
print("MultiDictOpenCC: 開始處理電子書...")
cc_main = OpenCC('s2twp')
final_dict = {}
# 建立最終字典
for f_name in gui.dict_order:
if gui.dict_enabled[f_name].get():
# 若使用者執行前未點擊過該檔案,需在此處載入
gui._ensure_file_loaded(f_name)
line_count = 0
for line in gui.dict_contents[f_name].splitlines():
line_count += 1
stripped_line = line.strip()
if not stripped_line:
continue
parts = stripped_line.split('\t')
if len(parts) >= 2:
k = cc_main.convert(parts[0])
final_dict[k] = parts[1].split()[0]
else:
# 防呆驗證:有內容卻沒有 Tab 分隔
print(f"⚠️ 警告:字典檔 [{f_name}] 第 {line_count} 行格式錯誤,缺少 Tab 分隔符號 -> '{line}',已略過。")
processor = UltraConverter(final_dict, 's2twp')
# 處理電子書內容
for entry in bc.manifest_iter():
fid, href, mtype = entry[0], entry[1], entry[2]
mtype_l = mtype.lower()
if any(x in mtype_l for x in ['xhtml', 'xml', 'ncx']) or 'nav' in fid.lower():
try:
raw = bc.readfile(fid)
if raw:
bc.writefile(fid, processor.process(raw))
except Exception as e:
print(f"❌ 錯誤:處理檔案 {fid} ({href}) 時發生例外!")
traceback.print_exc()
print("MultiDictOpenCC: 處理完成!")
return 0