import streamlit as st
import ebooklib
from ebooklib import epub
from PIL import Image
import io
import markdown
import uuid
from datetime import datetime
from collections import OrderedDict
# Initialize session state variables
if 'chapters' not in st.session_state:
st.session_state.chapters = OrderedDict({"Chapter 1": ""})
if 'metadata' not in st.session_state:
st.session_state.metadata = {
"description": "",
"publisher": "Self Published",
"publication_date": datetime.now().strftime("%Y-%m-%d"),
"rights": "All rights reserved"
}
# Helper functions
def get_chapter_num(chapter_title):
try:
return int(chapter_title.split()[1])
except:
return float('inf')
def add_chapter():
chapter_numbers = [get_chapter_num(title) for title in st.session_state.chapters.keys()]
next_number = max(chapter_numbers) + 1 if chapter_numbers else 1
new_chapters = OrderedDict()
existing_chapters = sorted(st.session_state.chapters.items(), key=lambda x: get_chapter_num(x[0]))
for title, content in existing_chapters:
new_chapters[title] = content
new_chapters[f"Chapter {next_number}"] = ""
st.session_state.chapters = new_chapters
def remove_chapter(chapter_title):
if chapter_title in st.session_state.chapters:
new_chapters = OrderedDict()
for title, content in st.session_state.chapters.items():
if title != chapter_title:
new_chapters[title] = content
st.session_state.chapters = new_chapters
def update_chapter_content(chapter_title, new_content):
if chapter_title in st.session_state.chapters:
st.session_state.chapters[chapter_title] = new_content
def update_chapter_title(old_title, new_title):
if old_title in st.session_state.chapters and old_title != new_title:
content = st.session_state.chapters[old_title]
new_chapters = OrderedDict()
for title, chapter_content in st.session_state.chapters.items():
if title == old_title:
new_chapters[new_title] = content
else:
new_chapters[title] = chapter_content
st.session_state.chapters = new_chapters
def convert_markdown_to_html(markdown_text):
return markdown.markdown(markdown_text, extensions=['tables', 'fenced_code', 'footnotes'])
def create_epub(title, author, cover_image, chapters_content, metadata):
book = epub.EpubBook()
# Set metadata
book.set_identifier(str(uuid.uuid4()))
book.set_title(title)
book.set_language('en')
book.add_author(author)
book.add_metadata('DC', 'description', metadata['description'])
book.add_metadata('DC', 'publisher', metadata['publisher'])
book.add_metadata('DC', 'date', metadata['publication_date'])
book.add_metadata('DC', 'rights', metadata['rights'])
# Handle cover image
cover_page = None
if cover_image is not None:
cover_image_bytes = io.BytesIO()
cover_image.save(cover_image_bytes, format='PNG')
cover_image_bytes = cover_image_bytes.getvalue()
cover_item = epub.EpubItem(
uid='cover_image',
file_name='images/cover.png',
media_type='image/png',
content=cover_image_bytes
)
book.add_item(cover_item)
# Create cover page
cover_page = epub.EpubHtml(title='Cover', file_name='cover.xhtml')
cover_page.content = f'''
'''
book.add_item(cover_page)
# Set the cover page as the book's cover
book.set_cover("images/cover.png", cover_image_bytes)
# Create table of contents page
toc_page = epub.EpubHtml(title='Table of Contents', file_name='toc.xhtml')
toc_content = ['