{
"packages": ["beautifulsoup4", "lxml", "six", "soupsieve", "./EbookLib-0.17.1-py3-none-any.whl"]
}
from bs4 import BeautifulSoup, NavigableString
from math import ceil
import ebooklib
from ebooklib import epub
from copy import copy
from js import document, FileReader, console, window, Object, localStorage
import io
def create_bold_tag(soup, str):
tag = soup.new_tag("b")
tag['class'] = 'b'
tag.string = str
return tag
def bolderize_str(soup, parent, child_text):
nodes = []
for word in child_text.split(" "):
if len(word) < 2:
nodes.append(create_bold_tag(soup, word + " "))
else:
end_point = ceil(len(word) / float(2))
to_bold = word[0:end_point]
rest = word[end_point:]
bolded_tag = create_bold_tag(soup, to_bold)
nodes.append(bolded_tag)
nodes.append(rest + " ")
return nodes
def process_node(soup, node):
new_node = copy(node)
new_node.clear()
for child in node.children:
if type(child) == str or type(child) == NavigableString:
nodes = bolderize_str(soup, node, child)
for node in nodes:
new_node.append(node)
else:
new_node.append(process_node(soup, child))
return new_node
def process_html(html):
soup = BeautifulSoup(html, 'html.parser')
soup.find_all('p')
for node in soup.find_all('p'):
new_node = process_node(soup, node)
node.replace_with(new_node)
"""
for node in soup.find_all(''):
process_string(soup, blah)
"""
# return soup.prettify(formatter='minimal')
return str(soup)
def process_epub(input_filename, output_filename):
book = epub.read_epub(input_filename)
for html_page in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
html = html_page.get_content()
updated_html = process_html(html)
html_page.set_content(updated_html.encode('utf-8'))
epub.write_epub(output_filename, book)
def process_file_data(file_data):
input_file = 'in.epub'
output_file = 'out.epub'
with open(input_file, 'wb') as in_file:
in_file.write(file_data)
process_epub(input_file, output_file)
return output_file
class ButtonState:
processing = 'processing'
processed = 'processed'
initial = ''
def set_state(state):
button_container = document.getElementById("button_container")
button_container.setAttribute("class", f"button-container {state}")
input_filename = ""
output_file_data = None
def set_filename(filename):
global input_filename
input_filename = filename
def get_filename():
return input_filename
async def process_file(file):
arrBuf = await file.to_py().arrayBuffer()
# print("Processing", arrBuf, dir(arrBuf.to_py()), dir(arrBuf));
output_file = process_file_data(arrBuf.to_bytes())
# print(output_file)
with open(output_file, "rb") as output:
return output.read()
async def file_select_event(event):
global output_file_data
try:
file_handles = event.target.files.to_py()
set_state(ButtonState.processing)
for file in file_handles:
# file = await file_handle.getFile()
set_filename(file.name)
file_data = await file.arrayBuffer()
output_filename = process_file_data(file_data.to_py())
with open(output_filename, "rb") as output_epub:
output_file_data = to_js(output_epub.read())
localStorage.setItem("output_file", output_file_data)
set_state(ButtonState.processed)
except Exception as e:
console.log('Exception: ' + str(e))
set_state(ButtonState.initial)
return
async def save_select_event(event):
suggested_name = input_filename
try:
options = {
"startIn": "downloads",
"suggestedName": suggested_name
}
file_handle = await window.showSaveFilePicker(Object.fromEntries(to_js(options)))
file = await file_handle.createWritable()
with open("out.epub", "rb") as output_epub:
await file.write(to_js(output_epub.read()))
await file.close()
except Exception as e:
console.log('Exception: ' + str(e))
return
"""
def setup_button():
file_open_event = create_proxy(file_select_event)
file_save_proxy = create_proxy(save_select_event)
document.getElementById("epub_input").addEventListener("change", file_open_event, False)
document.getElementById("save_button").addEventListener("click", function () {
console.log(PyScript.globals.get('output_file_data'));
})
setup_button()
"""