From fc72cacc4ab650e9fbe84ccab23f15ca68d81e1e Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Sat, 20 Oct 2018 23:31:46 +0100 Subject: [PATCH] Storing data into epub. --- gragir/prepare_epub.py | 11 ++- gragir/save_epub.py | 151 ++++++----------------------------------- 2 files changed, 30 insertions(+), 132 deletions(-) diff --git a/gragir/prepare_epub.py b/gragir/prepare_epub.py index d3505fe..1003a5e 100644 --- a/gragir/prepare_epub.py +++ b/gragir/prepare_epub.py @@ -33,12 +33,14 @@ class PrepareEpub(object): elif mime == 'text/html': local_url = cls._createLocalName(book,item,'') - else: local_url = cls._createLocalName(book,item,mime.split("/")[0]) cls._moveTo(item,local_url) + if mime == 'text/html': + cls._setXmlContent(item) + @classmethod def _createLocalName(cls, book, item, category): @@ -96,3 +98,10 @@ class PrepareEpub(object): else: logger.info("Renaming {} -> {}".format(item.url, local_url)) item.url = local_url + + @classmethod + def _setXmlContent(cls, item): + logger = logging.getLogger(__name__) + logger.info("Createing XML for {}".format(item.url)) + + item.payload = item.soup.prettify("utf-8") diff --git a/gragir/save_epub.py b/gragir/save_epub.py index 08a43ca..3045721 100644 --- a/gragir/save_epub.py +++ b/gragir/save_epub.py @@ -10,8 +10,10 @@ class SaveEpub(object): @classmethod def save(cls, book): logger = logging.getLogger(__name__) + logger.info("Saving EPUB.") ebook = ebooklib.EpubBook() + cls.fillEpubBook(ebook, book) cls.writeEpubBook(book.file_name, ebook) @@ -56,7 +58,7 @@ class SaveEpub(object): return 'html' else: - local_url = cls._createLocalName(book,item,mime.split("/")[0]) + return mime.split("/")[0] @classmethod @@ -70,143 +72,30 @@ class SaveEpub(object): def fillEpubBook(cls, ebook, book): logger = logging.getLogger(__name__) - for item in book.content: + for item in book.content.values(): + logger.info("EPUB: Storing {}".format(item.url)) item_type = cls.getType(item) if item_type == 'image': - eitem = ebooklib.EpubImage() - if item_type == 'image': + if hasattr(item, 'cover'): + eitem = ebooklib.EpubCover() + else: + eitem = ebooklib.EpubImage() + elif item_type == 'html': + eitem = ebooklib.EpubHtml() + # elif item_type == 'cover': + # eitem = ebooklib.EpubCoverHtml() + else: eitem = ebooklib.EpubItem() + eitem.media_type = item.content_type + eitem.file_name = item.url + eitem.content = item.payload - local_url = cls._createLocalName(book,item,'css') + ebook.add_item(eitem) + item.id = eitem.get_id() - elif mime == 'application/font-woff' \ - or mime == 'application/font-woff2': - local_url = cls._createLocalName(book,item,'font') - - elif mime == 'text/html': - local_url = cls._createLocalName(book,item,'') - - else: - local_url = cls._createLocalName(book,item,mime.split("/")[0]) - - if it.content_type == 'text/html': - html = createEpubHtml(it) - ebook.add_item(html) - elif it.content_type == 'image/html': - html = createEpubHtml(it) - ebook.add_item(html) - - if item_type == 'html': - eitem.set_content(item.soup.prettify("utf-8")) - else: - eitem.set_content(item.payload) - - - - # class EpubImage(EpubItem): # class EpubNav(EpubHtml): - # class EpubCoverHtml(EpubHtml): - # class EpubHtml(EpubItem): - # class EpubCover(EpubItem): # class EpubNcx(EpubItem): - # class EpubItem(object): - # class EpubException(Exception): # class Link(object): - # class Section(object): - - - # def set_identifier(self, uid) - # def set_title(self, title) - # def set_language(self, lang) - # def set_cover(self, file_name, content, create_page=True): - # """ - # Set cover and create cover document if needed. - - # :Args: - # - file_name: file name of the cover page - # - content: Content for the cover image - # - create_page: Should cover page be defined. Defined as bool value (optional). Default value is True. - # """ - - # def add_author(self, author, file_as=None, role=None, uid='creator'): - # def add_metadata(self, namespace, name, value, others=None): - # def set_unique_metadata(self, namespace, name, value, others=None): - # "Add metadata if metadata with this identifier does not already exist, otherwise update existing metadata." - # def add_item(self, item): - - # def get_metadata(self, namespace, name): - # def get_item_with_id(self, uid): - # """ - # Returns item for defined UID. - - # >>> book.get_item_with_id('image_001') - - # :Args: - # - uid: UID for the item - - # :Returns: - # Returns item object. Returns None if nothing was found. - # """ - - # def get_item_with_href(self, href): - # """ - # Returns item for defined HREF. - - # >>> book.get_item_with_href('EPUB/document.xhtml') - - # :Args: - # - href: HREF for the item we are searching for - - # :Returns: - # Returns item object. Returns None if nothing was found. - # """ - - # def get_items(self): - # def get_items_of_media_type(self, media_type): - # def get_items_of_type(self, item_type): - # """ - # Returns all items of specified type. - - # >>> book.get_items_of_type(epub.ITEM_IMAGE) - - # :Args: - # - item_type: Type for items we are searching for - - # :Returns: - # Returns found items as tuple. - # """ - # return (item for item in self.items if item.get_type() == item_type) - - - # def get_template(self, name): - # def set_template(self, name, value): - # """ - # Defines templates which are used to generate certain types of pages. When defining new value for the template - # we have to use content of type 'str' (Python 2) or 'bytes' (Python 3). - - # At the moment we use these templates: - # - ncx - # - nav - # - chapter - # - cover - - # :Args: - # - name: Name for the template - # - value: Content for the template - # """ - - - # def add_prefix(self, name, uri): - # """ - # Appends custom prefix to be added to the content.opf document - - # >>> epub_book.add_prefix('bkterms', 'http://booktype.org/') - - # :Args: - # - name: namespave name - # - uri: URI for the namespace - # """ - - return book + # class Section(object): \ No newline at end of file