Repository navigation
Expand file tree
/
Copy pathcreate_headers_tool.py
More file actions
189 lines (173 loc) · 9.62 KB
/
Copy pathcreate_headers_tool.py
File metadata and controls
189 lines (173 loc) · 9.62 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
# יצירת כותרות לאוצריא
import flet as ft
import re
class CreateHeadersToolFlet:
def __init__(self, page, gematria, AddPageNumberToHeading, get_selected_file_name,refresh_view=None):
self.page = page
self.gematria = gematria
self.AddPageNumberToHeading = AddPageNumberToHeading
self.get_selected_file_name = get_selected_file_name
self.refresh_view = refresh_view
search_choices = ["דף", "עמוד", "פרק", "פסוק", "שאלה", "סימן", "סעיף", "הלכה", "הלכות", "סק", "ענף"]
self.level_dropdown = ft.Dropdown(
label="בחר מילה",
options=[ft.dropdown.Option(x) for x in search_choices],
value=search_choices[0],
width=130
)
self.level_textfield = ft.TextField(
label="או כתוב מילה ידנית",
value="",
width=130
)
self.end_var = ft.Dropdown(
label="מספר כותרת מקסימלית",
options=[ft.dropdown.Option(str(i)) for i in range(1, 1000)],
value="999",
width=130
)
self.heading_level_var = ft.Dropdown(
label="רמת כותרת",
options=[ft.dropdown.Option(str(i)) for i in range(2, 7)],
value="2",
width=130
)
self.explanation = ft.Text(
"בתיבת 'מילה לחפש' יש לבחור או להקליד את המילה בה אנו רוצים שתתחיל הכותרת.\n"
"לדוג': פרק/פסוק/סימן/סעיף/הלכה/שאלה/עמוד/סק/ענף\n\n"
"שים לב!\nאין להקליד רווח אחרי המילה, וכן אין להקליד את התו גרש (') או גרשיים (\"), וכן אין להקליד יותר ממילה אחת",
size=16,
text_align=ft.TextAlign.RIGHT
)
self.result_text = ft.Text("", size=16, color=ft.Colors.GREEN_800)
self.run_button = ft.ElevatedButton("הפעל", on_click=self.run_script)
self.dialog = ft.AlertDialog(title=ft.Text(""), content=ft.Text(""))
self.layout = ft.Column([
ft.Row([self.level_dropdown, self.level_textfield], spacing=12),
ft.Row([self.end_var, self.heading_level_var], spacing=12),
self.explanation,
self.run_button,
self.result_text
], spacing=16)
def show_custom_message(self, title, message_parts):
full_message = ""
for part in message_parts:
full_message += f"{part[0]}\n"
self.dialog.title = ft.Text(title)
self.dialog.content = ft.Text(full_message)
self.page.open(self.dialog)
self.page.update()
def ot(self, text, end):
remove = ["<b>", "</b>", "<big>", "</big>", ":", '"', ",", ";", "[", "]", "(", ")", "'", ".", "״", "‚", """, "'"]
aa = ["ק", "ר", "ש", "ת", "תק", "תר", "תש", "תת", "תתק", "יה", "יו", "קיה", "קיו", "ריה", "ריו", "שיה", "שיו", "תיה", "תיו", "תקיה", "תקיו", "תריה", "תריו", "תשיה", "תשיו", "תתיה", "תתיו", "תתקיה", "תתקיו"]
bb = ["ם", "ן", "ץ", "ף", "ך"]
cc = ["ראשון", "שני", "שלישי", "רביעי", "חמישי", "שישי", "ששי", "שביעי", "שמיני", "תשיעי", "עשירי", "יוד", "למד", "נון", "דש", "חי", "טל", "שדמ", "ער", "שדם", "תשדם", "תשדמ", "ערב", "ערה", "עדר", "רחצ"]
append_list = []
for i in aa:
for ot_sofit in bb:
append_list.append(i + ot_sofit)
for tage in remove:
text = text.replace(tage, "")
withaute_gershayim = [self.gematria.to_hebrew(i) for i in range(1, end)] + bb + cc + append_list + aa
return text in withaute_gershayim
def strip_html_tags(self, text):
html_tags = ["<b>", "</b>", "<big>", "</big>", ":", '"', ",", ";", "[", "]", "(", ")", "'", "״", ".", "‚", """, "'"]
for tag in html_tags:
text = text.replace(tag, "")
return text
def main(self, book_file, finde, end, level_num):
found = False
count_headings = 0
finde_cleaned = self.strip_html_tags(finde).strip()
with open(book_file, "r", encoding="utf-8") as file_input:
content = file_input.read().splitlines()
all_lines = content[0:2]
i = 2
while i < len(content):
line = content[i]
words = line.split()
try:
if len(words) >= 2 and self.strip_html_tags(words[0]) == finde_cleaned and self.ot(self.strip_html_tags(words[1]), end):
found = True
count_headings += 1
heading_line = f"<h{level_num}>{self.strip_html_tags(words[0])} {self.strip_html_tags(words[1])}</h{level_num}>"
all_lines.append(heading_line)
if words[2:]:
fix_2 = " ".join(words[2:])
all_lines.append(fix_2)
elif (len(words) == 1 and self.strip_html_tags(words[0]) == finde_cleaned and i + 1 < len(content)):
next_line = content[i + 1]
next_words = next_line.split()
if (len(next_words) >= 1 and self.ot(self.strip_html_tags(next_words[0]), end)):
found = True
count_headings += 1
heading_line = f"<h{level_num}>{self.strip_html_tags(words[0])} {self.strip_html_tags(next_words[0])}</h{level_num}>"
all_lines.append(heading_line)
if next_words[1:]:
fix_2 = " ".join(next_words[1:])
all_lines.append(fix_2)
i += 1
else:
all_lines.append(line)
else:
all_lines.append(line)
except IndexError:
all_lines.append(line)
i += 1
join_lines = "\n".join(all_lines)
with open(book_file, "w", encoding="utf-8") as autpoot:
autpoot.write(join_lines)
return found, count_headings
def run_script(self, e):
print ("play")
book_file = self.get_selected_file_name()
finde = self.level_textfield.value.strip() if self.level_textfield.value.strip() else self.level_dropdown.value
print(f"[DEBUG] book_file: {book_file}")
print(f"[DEBUG] finde: {finde}")
try:
end = int(self.end_var.value)
level_num = int(self.heading_level_var.value)
print(f"[DEBUG] end: {end}")
print(f"[DEBUG] level_num: {level_num}")
except ValueError:
print("[DEBUG] ValueError: end or level_num not valid")
self.show_custom_message(
"קלט לא תקין",
[("אנא הזן 'מספר כותרת מקסימלית' ו'רמת כותרת' תקינים", 12)]
)
return
if not book_file or not finde:
print("[DEBUG] book_file or finde missing")
self.show_custom_message(
"קלט לא תקין",
[("אנא מלא את כל השדות", 12)]
)
return
try:
print("[DEBUG] Calling main...")
found, count_headings = self.main(book_file, finde, end + 1, level_num)
print(f"[DEBUG] main returned: found={found}, count_headings={count_headings}")
if found and count_headings > 0:
detailed_message = [
("התוכנה רצה בהצלחה!", 12),
(f"נוצרו {count_headings} כותרות", 15, "bold"),
("כעת פתח את הספר בתוכנת 'אוצריא', והשינויים ישתקפו ב'ניווט' שבתפריט הצידי.", 11),
("אם ישנם טעויות או תיקונים, פתח את הספר בעורך טקסט, כגון פנקס רשימות, וורד, notepad או vs code, ותקן את הדרוש תיקון", 11),
("שים לב! אם הספר כבר פתוח ב'אוצריא', יש לסגור אותו ולפתוח אותו שוב, אך אין צורך להפעיל את התוכנה מחדש", 9),
]
if finde == "דף":
print("[DEBUG] Adding page numbers...")
add_page_number = self.AddPageNumberToHeading()
add_page_number.process_file(book_file, "נקודה ונקודותיים", show_message=False)
detailed_message.insert(2, ("בנוסף, הוכנסו מספרי העמודים לכותרות הדף כנקודה ונקודותיים", 11, "bold"))
self.show_custom_message("!מזל טוב", detailed_message)
else:
print("[DEBUG] No headings found to replace.")
self.show_custom_message("!שים לב", [("לא נמצא מה להחליף", 12)])
except Exception as e:
print(f"[DEBUG] Exception: {e}")
self.show_custom_message("שגיאה", [("אירעה שגיאה: " + str(e), 12)])
if self.refresh_view:
self.refresh_view()
def get_view(self):
return self.layout