Skip to content

Commit

Permalink
fix(epub): repeated cover pages when repacking
Browse files Browse the repository at this point in the history
this requires excluding the cover from the spine. it also assumes the EPUB images are in alphanumeric order, but that's applicable to EPUBs in general.
  • Loading branch information
avalonv committed Dec 7, 2022
1 parent 5495131 commit eaa2541
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/reCBZ/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,17 @@ def _write_mobi(self, savepath):
# TODO not implemented
pass

def _create_pages_from_path(self, fp):
# TODO make public as add_page, append to self._pages
return Page(fp)
def add_page(self, fp, index=-1):
try:
assert Path(fp).exists()
Page(fp)
except AssertionError:
raise ValueError(f"can't open {fp}")
# we want the IndexError
self._index.insert(index, fp)

def remove_page(self, index):
return self._index.pop(index)

@worker_sigint_CTRL_C
def _convert_page(self, source:Page, savedir=None, format=None): #-> None | Str:
Expand Down
27 changes: 22 additions & 5 deletions src/reCBZ/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from reCBZ.util import mylog
from reCBZ.config import Config

POP_COVER = True


def single_chapter_epub(name:str, pages:list) -> str:
book = epub.EpubBook()
Expand All @@ -39,11 +41,15 @@ def single_chapter_epub(name:str, pages:list) -> str:
book.set_language('en')
book.set_identifier(str(uuid4()))

cover = pages[0]
# repacking the same file many times over would lead to additional copies
# if we did include it
if POP_COVER:
cover = pages.pop(0)
else:
cover = pages[0]
covert_ops = f'cover{cover.fmt.ext[0]}'
book.set_cover(covert_ops, open(cover.fp, 'rb').read())

# one chapter = one page = one image = lotsa bytes
spine = []
for page_i, page in enumerate(pages, start=1):
static_dest = f'static/{page_i}{page.fmt.ext[0]}'
Expand Down Expand Up @@ -77,9 +83,17 @@ def single_chapter_epub(name:str, pages:list) -> str:
book.toc.append(spine[0])

# add navigation files
# never ask
# a woman
# her age
# a man
# his salary
# a programmer
# what is a Ncx file
# 2009derp.jpeg
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ['cover', 'nav', *(page for page in spine)]
book.spine = (page for page in spine)

if Config.ebook_profile is not None:
for prop in Config.ebook_profile.epub_properties:
Expand All @@ -105,7 +119,10 @@ def multi_chapter_epub(name:str, chapters:list) -> str:
book.set_language('en')
book.set_identifier(str(uuid4()))

cover = chapters[0][0]
if POP_COVER:
cover = chapters[0].pop(0)
else:
cover = chapters[0][0]
covert_ops = f'cover{cover.fmt.ext[0]}'
book.set_cover(covert_ops, open(cover.fp, 'rb').read())

Expand Down Expand Up @@ -147,7 +164,7 @@ def multi_chapter_epub(name:str, chapters:list) -> str:

book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ['cover', 'nav', *(page for page in spine)]
book.spine = ['nav', *(page for page in spine)]

if Config.ebook_profile is not None:
for prop in Config.ebook_profile.epub_properties:
Expand Down

0 comments on commit eaa2541

Please sign in to comment.