-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbrec_v2.py
More file actions
85 lines (66 loc) · 2.85 KB
/
Copy pathbrec_v2.py
File metadata and controls
85 lines (66 loc) · 2.85 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
import os
import regex
import zipfile
import argparse
from bs4 import BeautifulSoup
from lxml import etree
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
def bionic_word(word):
if len(word) <= 1:
return word
elif len(word) <= 3:
return f"<b>{word[:1]}</b>{word[1:]}"
else:
midpoint = len(word) // 2
return f"<b>{word[:midpoint]}</b>{word[midpoint:]}"
def process_text(text):
word_pattern = regex.compile(r'\b[\p{L}\p{M}]+\b', regex.UNICODE)
def replace_word(match):
word = match.group(0)
return bionic_word(word)
return word_pattern.sub(replace_word, text)
def process_html_content(content):
soup = BeautifulSoup(content, 'lxml')
skip_tags = {'script', 'style', 'pre', 'code'}
for element in soup.find_all(text=True):
if element.parent.name not in skip_tags:
new_text = process_text(element.string)
new_element = BeautifulSoup(new_text, 'html.parser')
element.replace_with(new_element)
return str(soup)
def process_epub(input_path, output_path):
with zipfile.ZipFile(input_path, 'r') as zip_ref:
file_list = zip_ref.infolist()
total_files = len(file_list)
with zipfile.ZipFile(output_path, 'w') as zip_out:
with tqdm(total=total_files, desc="Processing files", unit="file") as pbar:
for file_info in file_list:
with zip_ref.open(file_info) as file:
content = file.read()
if file_info.filename.endswith(('.html', '.xhtml', '.htm')):
content = process_html_content(content)
elif file_info.filename.endswith('content.opf'):
# Do not modify content.opf
pass
zip_out.writestr(file_info, content)
pbar.update(1)
pbar.set_postfix(current_file=file_info.filename)
def main():
parser = argparse.ArgumentParser(description='Convert EPUB to Bionic Reading format')
parser.add_argument('input', help='Input EPUB file path')
parser.add_argument('output', help='Output EPUB file path')
args = parser.parse_args()
if not os.path.exists(args.input):
print(f"Error: Input file '{args.input}' does not exist.")
return
if os.path.exists(args.output):
print(f"Warning: Output file '{args.output}' already exists. It will be overwritten.")
try:
process_epub(args.input, args.output)
print(f"Conversion complete. Output saved to '{args.output}'")
except Exception as e:
print(f"Error occurred during processing: {str(e)}")
if __name__ == "__main__":
main()