-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathchapter_editor.py
More file actions
357 lines (294 loc) · 12.5 KB
/
chapter_editor.py
File metadata and controls
357 lines (294 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
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
#!/usr/bin/env python3
# encoding: utf-8
import argparse
import re
import tkinter as tk
from tkinter import messagebox
import html
from html.parser import HTMLParser
import yaml
from pymysql import connect
class _HTMLStripper(HTMLParser):
def __init__(self):
super().__init__()
self._parts = []
def handle_data(self, data):
self._parts.append(data)
def handle_starttag(self, tag, attrs):
if tag in ("p", "br", "div", "li", "tr", "h1", "h2", "h3", "h4"):
self._parts.append("\n")
def get_text(self):
return html.unescape("".join(self._parts))
def strip_html(text):
s = _HTMLStripper()
s.feed(text or "")
return s.get_text()
def load_config(path):
with open(path) as f:
return yaml.safe_load(f)
def get_connection(cfg):
return connect(
host=cfg["db_host"],
user=cfg["db_user"],
password=cfg.get("db_password") or "",
database=cfg["output_database"],
charset="utf8mb4",
use_unicode=True,
autocommit=False,
)
def fetch_chapter(conn, chapter_id):
with conn.cursor() as cur:
cur.execute("SELECT * FROM chapters WHERE id = %s", (chapter_id,))
cols = [d[0] for d in cur.description]
row = cur.fetchone()
if row is None:
raise ValueError(f"Chapter {chapter_id} not found in output database")
return dict(zip(cols, row))
def db_update_chapter_text(cur, chapter_id, text):
cur.execute("UPDATE chapters SET text = %s WHERE id = %s", (text, chapter_id))
def db_update_chapter_text_and_title(cur, chapter_id, text, title):
cur.execute(
"UPDATE chapters SET text = %s, title = %s WHERE id = %s",
(text, title, chapter_id),
)
def db_shift_later_chapters(cur, story_id, after_position):
cur.execute(
"UPDATE chapters SET position = position + 1 WHERE story_id = %s AND position > %s",
(story_id, after_position),
)
def db_insert_chapter(cur, story_id, position, title, author_id, text, date, notes, url):
cur.execute(
"""INSERT INTO chapters (position, title, author_id, text, date, story_id, notes, url)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",
(position, title, author_id, text, date, story_id, notes, url),
)
def db_trim_chapter(conn, chapter_id, trimmed_text):
with conn.cursor() as cur:
db_update_chapter_text(cur, chapter_id, trimmed_text)
conn.commit()
def db_split_chapter(conn, chapter, before_text, after_text, title_part1, title_part2):
story_id = chapter["story_id"]
orig_position = chapter["position"]
with conn.cursor() as cur:
db_shift_later_chapters(cur, story_id, orig_position)
db_update_chapter_text_and_title(cur, chapter["id"], before_text, title_part1)
db_insert_chapter(
cur,
story_id=story_id,
position=orig_position + 1,
title=title_part2,
author_id=chapter["author_id"],
text=after_text,
date=chapter["date"],
notes=chapter["notes"],
url=chapter["url"],
)
conn.commit()
class SplitChapterApp:
def __init__(self, root, conn, chapter):
self.root = root
self.conn = conn
self.chapter = chapter
self.split_index = None
root.title(f"Split Chapter: {chapter['title']}")
info = tk.Frame(root)
info.pack(fill=tk.X, padx=10, pady=5)
tk.Label(
info,
text=f"Chapter ID: {chapter['id']} | Story ID: {chapter['story_id']} | Position: {chapter['position']}",
).pack(side=tk.LEFT)
text_frame = tk.Frame(root)
text_frame.pack(fill=tk.BOTH, expand=True, padx=10)
scrollbar = tk.Scrollbar(text_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.text_widget = tk.Text(
text_frame,
wrap=tk.WORD,
yscrollcommand=scrollbar.set,
width=100,
height=40,
cursor="ibeam",
)
self.text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=self.text_widget.yview)
# Display stripped text; keep a mapping from display offset → raw HTML offset
self._display_text, self._offset_map = self._build_display(chapter["text"] or "")
self.text_widget.insert(tk.END, self._display_text)
self.text_widget.config(state=tk.DISABLED)
self.text_widget.bind("<Button-1>", self.on_click)
self.text_widget.bind("<ButtonRelease-1>", self.on_release)
self.status_var = tk.StringVar(value="Click in the text to set a split point.")
tk.Label(root, textvariable=self.status_var, fg="blue").pack(pady=4)
counts_frame = tk.Frame(root)
counts_frame.pack()
self.before_var = tk.StringVar(value="Before: —")
self.after_var = tk.StringVar(value="After: —")
tk.Label(counts_frame, textvariable=self.before_var, width=30).pack(side=tk.LEFT, padx=10)
tk.Label(counts_frame, textvariable=self.after_var, width=30).pack(side=tk.LEFT, padx=10)
btn_frame = tk.Frame(root)
btn_frame.pack(pady=10)
self.split_btn = tk.Button(
btn_frame,
text="Split at click point",
state=tk.DISABLED,
command=self.do_split,
padx=20,
)
self.split_btn.pack(side=tk.LEFT, padx=10)
self.trim_btn = tk.Button(
btn_frame,
text="Trim to selection",
state=tk.DISABLED,
command=self.do_trim,
padx=20,
)
self.trim_btn.pack(side=tk.LEFT, padx=10)
tk.Button(btn_frame, text="Cancel", command=root.quit, padx=20).pack(side=tk.LEFT, padx=10)
def _build_display(self, raw_html):
"""
Returns (display_text, offset_map) where offset_map[display_idx] = raw_idx.
Strips HTML tags, converts block-level tags to newlines, unescapes entities.
"""
display_chars = []
offset_map = [] # display position i → raw HTML position
raw = raw_html
i = 0
block_tags = {"p", "br", "div", "li", "tr", "h1", "h2", "h3", "h4", "hr"}
while i < len(raw):
if raw[i] == "<":
end = raw.find(">", i)
if end == -1:
display_chars.append(raw[i])
offset_map.append(i)
i += 1
continue
tag_content = raw[i + 1:end].strip().lower().lstrip("/").split()[0] if raw[i + 1:end].strip() else ""
if tag_content in block_tags:
display_chars.append("\n")
offset_map.append(i)
i = end + 1
elif raw[i] == "&":
end = raw.find(";", i)
if end == -1 or end - i > 10:
display_chars.append(raw[i])
offset_map.append(i)
i += 1
else:
entity = raw[i:end + 1]
decoded = html.unescape(entity)
for ch in decoded:
display_chars.append(ch)
offset_map.append(i)
i = end + 1
else:
display_chars.append(raw[i])
offset_map.append(i)
i += 1
return "".join(display_chars), offset_map
def on_click(self, event):
idx = self.text_widget.index(f"@{event.x},{event.y}")
display_offset = self._tk_index_to_char_offset(idx, self._display_text)
# Map display offset back to raw HTML offset
if display_offset < len(self._offset_map):
raw_offset = self._offset_map[display_offset]
else:
raw_offset = len(self.chapter["text"] or "")
self.split_index = raw_offset
full_text = self.chapter["text"] or ""
before_len = len(full_text[:raw_offset])
after_len = len(full_text[raw_offset:])
self.before_var.set(f"Before: {before_len:,} chars")
self.after_var.set(f"After: {after_len:,} chars")
self.status_var.set(f"Split point at raw HTML offset {raw_offset:,}. Click 'Split' to confirm.")
self.split_btn.config(state=tk.NORMAL)
self.text_widget.config(state=tk.NORMAL)
self.text_widget.tag_remove("split", "1.0", tk.END)
self.text_widget.tag_add("split", idx)
self.text_widget.tag_config("split", background="yellow")
self.text_widget.config(state=tk.DISABLED)
def _tk_index_to_char_offset(self, idx, text):
line, col = map(int, idx.split("."))
lines = text.split("\n")
offset = sum(len(lines[i]) + 1 for i in range(line - 1)) # +1 for each \n
return offset + col
def on_release(self, event):
try:
sel_start = self.text_widget.index(tk.SEL_FIRST)
sel_end = self.text_widget.index(tk.SEL_LAST)
if sel_start != sel_end:
self.trim_btn.config(state=tk.NORMAL)
self.status_var.set("Text selected. Click 'Trim to selection' to keep only the selected text.")
return
except tk.TclError:
pass
self.trim_btn.config(state=tk.DISABLED)
def do_trim(self):
try:
sel_start = self.text_widget.index(tk.SEL_FIRST)
sel_end = self.text_widget.index(tk.SEL_LAST)
except tk.TclError:
messagebox.showwarning("No selection", "Please select the text you want to keep.")
return
start_display = self._tk_index_to_char_offset(sel_start, self._display_text)
end_display = self._tk_index_to_char_offset(sel_end, self._display_text)
raw = self.chapter["text"] or ""
raw_start = self._offset_map[start_display] if start_display < len(self._offset_map) else 0
raw_end = self._offset_map[end_display] if end_display < len(self._offset_map) else len(raw)
trimmed = raw[raw_start:raw_end]
if not trimmed.strip():
messagebox.showwarning("Empty selection", "The selected text is empty.")
return
if not messagebox.askyesno(
"Confirm trim",
f"This will replace the chapter text with the selected {len(trimmed):,} characters.\n\nThis cannot be undone. Continue?",
):
return
try:
db_trim_chapter(self.conn, self.chapter["id"], trimmed)
messagebox.showinfo("Done", f"Chapter {self.chapter['id']} trimmed to {len(trimmed):,} characters.")
self.root.quit()
except Exception as e:
self.conn.rollback()
messagebox.showerror("Error", str(e))
def do_split(self):
if self.split_index is None:
return
full_text = self.chapter["text"] or ""
before_text = full_text[: self.split_index]
after_text = full_text[self.split_index :]
if not before_text.strip() or not after_text.strip():
messagebox.showwarning("Invalid split", "Both parts must have content.")
return
base_title = re.sub(r"\s+Part \d+$", "", self.chapter["title"] or "").strip()
title_part1 = f"{base_title} Part 1"
title_part2 = f"{base_title} Part 2"
try:
db_split_chapter(self.conn, self.chapter, before_text, after_text, title_part1, title_part2)
messagebox.showinfo(
"Done",
f"Chapter split successfully.\n\n"
f"Chapter {self.chapter['id']} → '{title_part1}'\n"
f"New chapter (position {self.chapter['position'] + 1}) → '{title_part2}'",
)
self.root.quit()
except Exception as e:
self.conn.rollback()
messagebox.showerror("Error", str(e))
def main():
import getpass
parser = argparse.ArgumentParser(description="Split a chapter in the Open Doors output database")
parser.add_argument("-p", "--properties_file", required=True, help="Path to yml config file")
parser.add_argument("--chapter_id", required=True, type=int, help="ID of the chapter to split")
args = parser.parse_args()
cfg = load_config(args.properties_file)
if not cfg.get("db_password"):
cfg["db_password"] = getpass.getpass(f"MySQL password for {cfg['db_user']}@{cfg['db_host']}: ")
conn = get_connection(cfg)
chapter = fetch_chapter(conn, args.chapter_id)
root = tk.Tk()
root.geometry("1000x700")
SplitChapterApp(root, conn, chapter)
root.mainloop()
conn.close()
if __name__ == "__main__":
main()