-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_page_b_headers_tool.py
More file actions
121 lines (104 loc) · 5.09 KB
/
Copy pathreplace_page_b_headers_tool.py
File metadata and controls
121 lines (104 loc) · 5.09 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
import flet as ft
import re
# החלפת כותרות עמוד ב
class ReplacePageBHeadersToolFlet:
def __init__(self, page, get_selected_file_name,refresh_view=None):
self.page = page
self.get_selected_file_name = get_selected_file_name
self.refresh_view = refresh_view
self.explanation = ft.Text(
"שים לב! התוכנה פועלת רק אם הדפים והעמודים הוגדרו ככותרות (כגון: <h3>עמוד ב</h3>)\n"
"זהירות! בדוק שלא פספסת כותרת 'דף' - אחרת 'עמוד ב' שאחריו יהפוך לכותרת שגויה",
size=12,
text_align=ft.TextAlign.CENTER
)
self.replace_type = ft.Dropdown(
label="בחר את סוג ההחלפה",
options=[
ft.dropdown.Option("נקודותיים"),
ft.dropdown.Option('ע"ב')
],
value="נקודותיים",
width=160
)
self.examples = ft.Text(
"דוגמאות:\nנקודותיים: דף ב: דף ג: דף ד:\n"
'ע"ב: דף ב ע"ב דף ג ע"ב דף ד ע"ב',
size=12,
text_align=ft.TextAlign.CENTER
)
self.run_button = ft.ElevatedButton("בצע החלפה", on_click=self.run_script)
self.result_text = ft.Text("", size=16, color=ft.Colors.GREEN_800)
self.dialog = ft.AlertDialog(title=ft.Text(""), content=ft.Text(""))
self.layout = ft.Column([
self.explanation,
self.replace_type,
self.examples,
self.run_button,
self.result_text
], spacing=12)
def show_custom_message(self, title, message):
self.dialog.title = ft.Text(title)
self.dialog.content = ft.Text(message)
self.page.open(self.dialog)
self.page.update()
def update_file(self, file_path, replace_type):
if not file_path or not file_path.lower().endswith('.txt'):
self.show_custom_message("קלט לא תקין", "סוג הקובץ אינו נתמך\nבחר קובץ טקסט [בסיומת TXT.]")
return
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
previous_title = ""
previous_level = ""
replacements_made = 0
def replace_match(match):
nonlocal previous_title, previous_level, replacements_made
level = match.group(1)
title = match.group(2)
# בדיקה אם הכותרת היא "דף"
if re.match(r"דף \S+\.?", title):
previous_title = title.strip()
previous_level = level
return match.group(0)
# בדיקה אם הכותרת היא "עמוד ב"
elif title == "עמוד ב":
replacements_made += 1
if replace_type == "נקודותיים":
return f'<h{previous_level}>{previous_title.rstrip(".")}:</h{previous_level}>'
elif replace_type == "ע\"ב":
modified_title = re.sub(r'( ע\"א| עמוד א)', '', previous_title)
return f'<h{previous_level}>{modified_title.rstrip(".")} ע\"ב</h{previous_level}>'
return match.group(0)
content = re.sub(r'<h([1-9])>(.*?)</h\1>', replace_match, content)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
if replacements_made == 0:
snack = ft.SnackBar(
ft.Text("אין מה להחליף בקובץ זה", text_align=ft.TextAlign.CENTER),
bgcolor=ft.Colors.GREY_400,
duration=1500
)
self.page.open(snack)
else:
snack = ft.SnackBar(
ft.Text(f"הקובץ עודכן בהצלחה!\nבוצעו {replacements_made} החלפות", text_align=ft.TextAlign.CENTER),
bgcolor=ft.Colors.GREEN_400,
duration=1500
)
self.page.open(snack)
self.page.update()
except FileNotFoundError:
self.show_custom_message("קלט לא תקין", "הקובץ לא נמצא")
except UnicodeDecodeError:
self.show_custom_message("קלט לא תקין", "קידוד הקובץ אינו נתמך. יש להשתמש בקידוד UTF-8.")
except Exception as e:
self.show_custom_message("קלט לא תקין", f"שגיאה בפתיחת קובץ: {e}")
def run_script(self, e):
file_path = self.get_selected_file_name()
replace_type = self.replace_type.value
self.update_file(file_path, replace_type)
if self.refresh_view:
self.refresh_view()
def get_view(self):
return self.layout