Spaces:
Runtime error
Runtime error
Upload ebook_pipeline.py with huggingface_hub
Browse files- ebook_pipeline.py +27 -7
ebook_pipeline.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
from ebooklib import epub
|
| 3 |
from fpdf import FPDF
|
|
|
|
| 4 |
|
| 5 |
class BookBuilder:
|
| 6 |
-
def __init__(self, title, author, description=""):
|
| 7 |
self.title = title
|
| 8 |
self.author = author
|
| 9 |
self.description = description
|
|
|
|
| 10 |
self.chapters = []
|
| 11 |
# Fetch Business Metadata from Environment
|
| 12 |
self.business_name = os.environ.get("BUSINESS_NAME", "Fair Dinkum Publishing")
|
|
@@ -27,6 +29,14 @@ class BookBuilder:
|
|
| 27 |
book.add_metadata("DC", "rights", f"Copyright (c) 2026 {self.owner}. ABN: {self.abn}")
|
| 28 |
|
| 29 |
spine = ["nav"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Add Copyright Page
|
| 31 |
copy_page = epub.EpubHtml(title="Copyright", file_name="copyright.xhtml", lang="en")
|
| 32 |
copy_page.content = f"""
|
|
@@ -42,8 +52,9 @@ class BookBuilder:
|
|
| 42 |
|
| 43 |
for i, ch in enumerate(self.chapters):
|
| 44 |
chapter = epub.EpubHtml(title=ch["title"], file_name=f"chap_{i+1}.xhtml", lang="en")
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
book.add_item(chapter)
|
| 48 |
spine.append(chapter)
|
| 49 |
|
|
@@ -56,9 +67,14 @@ class BookBuilder:
|
|
| 56 |
def generate_pdf(self, output_path):
|
| 57 |
pdf = FPDF()
|
| 58 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 59 |
-
pdf.add_page()
|
| 60 |
|
| 61 |
# Title Page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
pdf.set_font("Arial", "B", 24)
|
| 63 |
pdf.cell(200, 40, self.title, ln=True, align="C")
|
| 64 |
pdf.set_font("Arial", "I", 16)
|
|
@@ -77,8 +93,12 @@ class BookBuilder:
|
|
| 77 |
pdf.set_font("Arial", "B", 18)
|
| 78 |
pdf.cell(0, 10, ch["title"], ln=True)
|
| 79 |
pdf.ln(10)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
pdf.set_font("Arial", "", 12)
|
| 81 |
-
pdf.
|
| 82 |
|
| 83 |
# Footer on every page
|
| 84 |
pdf.set_y(-15)
|
|
@@ -88,12 +108,12 @@ class BookBuilder:
|
|
| 88 |
pdf.output(output_path)
|
| 89 |
return output_path
|
| 90 |
|
| 91 |
-
def create_ebook_files(title, author, content_list, base_name="ebook"):
|
| 92 |
"""
|
| 93 |
Helper to create both formats.
|
| 94 |
content_list: List of dicts with 'title' and 'content'.
|
| 95 |
"""
|
| 96 |
-
builder = BookBuilder(title, author)
|
| 97 |
for item in content_list:
|
| 98 |
builder.add_chapter(item["title"], item["content"])
|
| 99 |
|
|
|
|
| 1 |
import os
|
| 2 |
from ebooklib import epub
|
| 3 |
from fpdf import FPDF
|
| 4 |
+
import markdown
|
| 5 |
|
| 6 |
class BookBuilder:
|
| 7 |
+
def __init__(self, title, author, description="", cover_image=None):
|
| 8 |
self.title = title
|
| 9 |
self.author = author
|
| 10 |
self.description = description
|
| 11 |
+
self.cover_image = cover_image
|
| 12 |
self.chapters = []
|
| 13 |
# Fetch Business Metadata from Environment
|
| 14 |
self.business_name = os.environ.get("BUSINESS_NAME", "Fair Dinkum Publishing")
|
|
|
|
| 29 |
book.add_metadata("DC", "rights", f"Copyright (c) 2026 {self.owner}. ABN: {self.abn}")
|
| 30 |
|
| 31 |
spine = ["nav"]
|
| 32 |
+
|
| 33 |
+
# Add Cover Image if provided
|
| 34 |
+
if self.cover_image and os.path.exists(self.cover_image):
|
| 35 |
+
with open(self.cover_image, 'rb') as img_file:
|
| 36 |
+
content = img_file.read()
|
| 37 |
+
img_ext = os.path.splitext(self.cover_image)[1][1:]
|
| 38 |
+
book.set_cover("cover.jpg", content)
|
| 39 |
+
|
| 40 |
# Add Copyright Page
|
| 41 |
copy_page = epub.EpubHtml(title="Copyright", file_name="copyright.xhtml", lang="en")
|
| 42 |
copy_page.content = f"""
|
|
|
|
| 52 |
|
| 53 |
for i, ch in enumerate(self.chapters):
|
| 54 |
chapter = epub.EpubHtml(title=ch["title"], file_name=f"chap_{i+1}.xhtml", lang="en")
|
| 55 |
+
# Convert Markdown to HTML
|
| 56 |
+
content_html = markdown.markdown(ch["content"])
|
| 57 |
+
chapter.content = f"<h1>{ch['title']}</h1>{content_html}"
|
| 58 |
book.add_item(chapter)
|
| 59 |
spine.append(chapter)
|
| 60 |
|
|
|
|
| 67 |
def generate_pdf(self, output_path):
|
| 68 |
pdf = FPDF()
|
| 69 |
pdf.set_auto_page_break(auto=True, margin=15)
|
|
|
|
| 70 |
|
| 71 |
# Title Page
|
| 72 |
+
pdf.add_page()
|
| 73 |
+
if self.cover_image and os.path.exists(self.cover_image):
|
| 74 |
+
# Try to fit cover image on first page
|
| 75 |
+
pdf.image(self.cover_image, x=10, y=10, w=190)
|
| 76 |
+
pdf.add_page()
|
| 77 |
+
|
| 78 |
pdf.set_font("Arial", "B", 24)
|
| 79 |
pdf.cell(200, 40, self.title, ln=True, align="C")
|
| 80 |
pdf.set_font("Arial", "I", 16)
|
|
|
|
| 93 |
pdf.set_font("Arial", "B", 18)
|
| 94 |
pdf.cell(0, 10, ch["title"], ln=True)
|
| 95 |
pdf.ln(10)
|
| 96 |
+
|
| 97 |
+
# Use write_html to handle basic markdown-to-html conversion for FPDF
|
| 98 |
+
# FPDF2 supports basic HTML tags with write_html
|
| 99 |
+
html_content = markdown.markdown(ch["content"])
|
| 100 |
pdf.set_font("Arial", "", 12)
|
| 101 |
+
pdf.write_html(html_content)
|
| 102 |
|
| 103 |
# Footer on every page
|
| 104 |
pdf.set_y(-15)
|
|
|
|
| 108 |
pdf.output(output_path)
|
| 109 |
return output_path
|
| 110 |
|
| 111 |
+
def create_ebook_files(title, author, content_list, base_name="ebook", cover_image=None):
|
| 112 |
"""
|
| 113 |
Helper to create both formats.
|
| 114 |
content_list: List of dicts with 'title' and 'content'.
|
| 115 |
"""
|
| 116 |
+
builder = BookBuilder(title, author, cover_image=cover_image)
|
| 117 |
for item in content_list:
|
| 118 |
builder.add_chapter(item["title"], item["content"])
|
| 119 |
|