chapisha/create/create.py
def build(self):
        """
        Automatically build the creative work as a standards compliant EPUB3. Save to the root directory.
        """
        if not self.work_name or not self.metadata:
            e = "`set_metadata` before building creative work."
            raise PermissionError(e)
        epub_path = self.directory.parent / F"{self.work_name}.epub"
        # Generate the initial creative content using Pandoc
        # pypandoc can't handle PosixPaths ...
        if self.stateless:
            pypandoc.convert_file(str(self.directory / F"{self.work_name}.docx"), 
                                  format="docx",
                                  to="epub3",
                                  outputfile=str(epub_path))
        else:
            # Maybe one day Pandoc can return an epub object and we won't save the interim file
            pypandoc.convert_text(self.work, 
                                  format="docx",
                                  to="epub3",
                                  outputfile=str(epub_path))
        # Generate the epub version
        with UpdateZipFile(epub_path, "a") as w:
            # REMOVES
            REMOVES = ["EPUB/styles/stylesheet1.css", "EPUB/text/title_page.xhtml", "EPUB/nav.xhtml"]
            # DEFAULT COMPONENTS
            DEFAULT = [(self.source_path / "css" / "core.css", "EPUB/css/core.css"),
                    (self.source_path / "images" / "logo.svg", "EPUB/images/logo.svg"),
                    (self.source_path / "xhtml" / "onix.xml", "EPUB/onix.xml"),
                    (self.source_path / "xhtml" / "container.xml", "META-INF/container.xml")]
            for default_file, write_file in DEFAULT:
                w.write(default_file, write_file)
            # DEFAULT FONTS
            for f in os.listdir(self.source_path / "fonts"):
                w.write(self.source_path / "fonts" / f, F"EPUB/fonts/{f}")
            # ADD titlepage.xhtml
            w.writestr("EPUB/text/titlepage.xhtml", pages.create_titlepage_xhtml(self.metadata))
            # ADD colophon.xhtml
            w.writestr("EPUB/text/colophon.xhtml", pages.create_colophon_xhtml(self.metadata))
            # ADD cover.img
            if self.stateless:
                for image_path in [self.directory / F"cover.{t}" for t in ["jpg", "jpeg", "png", "gif", "svg"]]:
                    if image_path.exists():
                        w.write(image_path, F"EPUB/images/{image_path.name}")
            elif self.cover:
                t = filetype.guess(self.cover).extension
                w.writestr(F"EPUB/images/cover.{t}", self.cover)
            # GET DEDICATION and CHAPTERS
            spine = []
            # check if the path to dedication exists, if it does, add it to the work and spine
            if (self.directory / "dedication.xhtml").exists() or self.dedication:
                if self.dedication: 
                    w.writestr("EPUB/text/dedication.xhtml", self.dedication)
                else:
                    w.write(self.directory / "dedication.xhtml", "EPUB/text/dedication.xhtml")
                spine = [Matter(partition="frontmatter", content="dedication", title="Dedication")]
            CHAPTERS = [f for f in w.namelist() if f.startswith("EPUB/text/ch")]
            CHAPTERS.sort()
            self.metadata.word_count = 0
            for chapter in CHAPTERS:
                file_as = F"EPUB/text/chapter-{chapter.split('.')[0][-1]}.xhtml"
                try:
                    chapter_xml = w.read(chapter)
                except KeyError:
                    continue
                if file_as != chapter:
                    # If delete and then re-add same file, causes ZipFile confusion
                    REMOVES.append(chapter)
                # Restructure chapter xml into standard format
                chapter_xml = pages.restructure_chapter(chapter_xml)
                chapter_title = chapter_xml.title.string
                # Count the words (XHTML and HTML treated differently by BeautifulSoup, so first extract `section`)
                words = BeautifulSoup(str(chapter_xml.section), "lxml").get_text()
                self.metadata.word_count += len(words.replace("\n", " ").replace("  ", " ").strip().split())
                w.writestr(file_as, str(chapter_xml))
                spine.append(Matter(partition=MatterPartition.body, title=chapter_title))
            # PANDOC MAY STILL ADD IMAGES FOUND IN THE WORK WHICH WE NEED TO DISCOVER AND ADD TO THE MANIFEST
            # NOTE, these are not only to be added to the manifest, but the folder renamed as well
            image_manifest = [f.replace("EPUB/", "") for f in w.namelist() if f.startswith("EPUB/images/")]
            for img in [f for f in w.namelist() if f.startswith("EPUB/media/")]:
                REMOVES.append(img)
                new_img = img.replace("/media/", "/images/")
                try:
                    old_img = w.read(img)
                    w.writestr(new_img, old_img)
                except KeyError:
                    continue
                image_manifest.append(new_img.replace("EPUB/", ""))
            # ADD content.opf
            w.writestr("EPUB/content.opf", pages.create_content_opf(self.metadata, image_manifest, spine))
            # ADD toc.ncx
            w.writestr("EPUB/toc.ncx", pages.create_toc_ncx(self.metadata, spine))
            # ADD toc.xhtml
            w.writestr("EPUB/toc.xhtml", pages.create_toc_xhtml(self.metadata, spine))
            # PERFORM REMOVES
            for remove in REMOVES:
                try:
                    w.remove_file(remove)
                except KeyError:
                    continue

        Automatically build the creative work as a standards compliant EPUB3. Save to the root directory.
        
Automatically build the creative work as a standards compliant EPUB3. Save to the root directory.
Automatically build the creative work as a standards compliant EPUB3 . Save to the root directory .
def build(self):
        if not self.work_name or not self.metadata:
            e = "`set_metadata` before building creative work."
            raise PermissionError(e)
        epub_path = self.directory.parent / F"{self.work_name}.epub"
        if self.stateless:
            pypandoc.convert_file(str(self.directory / F"{self.work_name}.docx"), 
                                  format="docx",
                                  to="epub3",
                                  outputfile=str(epub_path))
        else:
            pypandoc.convert_text(self.work, 
                                  format="docx",
                                  to="epub3",
                                  outputfile=str(epub_path))
        with UpdateZipFile(epub_path, "a") as w:
            REMOVES = ["EPUB/styles/stylesheet1.css", "EPUB/text/title_page.xhtml", "EPUB/nav.xhtml"]
            DEFAULT = [(self.source_path / "css" / "core.css", "EPUB/css/core.css"),
                    (self.source_path / "images" / "logo.svg", "EPUB/images/logo.svg"),
                    (self.source_path / "xhtml" / "onix.xml", "EPUB/onix.xml"),
                    (self.source_path / "xhtml" / "container.xml", "META-INF/container.xml")]
            for default_file, write_file in DEFAULT:
                w.write(default_file, write_file)
            for f in os.listdir(self.source_path / "fonts"):
                w.write(self.source_path / "fonts" / f, F"EPUB/fonts/{f}")
            w.writestr("EPUB/text/titlepage.xhtml", pages.create_titlepage_xhtml(self.metadata))
            w.writestr("EPUB/text/colophon.xhtml", pages.create_colophon_xhtml(self.metadata))
            if self.stateless:
                for image_path in [self.directory / F"cover.{t}" for t in ["jpg", "jpeg", "png", "gif", "svg"]]:
                    if image_path.exists():
                        w.write(image_path, F"EPUB/images/{image_path.name}")
            elif self.cover:
                t = filetype.guess(self.cover).extension
                w.writestr(F"EPUB/images/cover.{t}", self.cover)
            spine = []
            if (self.directory / "dedication.xhtml").exists() or self.dedication:
                if self.dedication: 
                    w.writestr("EPUB/text/dedication.xhtml", self.dedication)
                else:
                    w.write(self.directory / "dedication.xhtml", "EPUB/text/dedication.xhtml")
                spine = [Matter(partition="frontmatter", content="dedication", title="Dedication")]
            CHAPTERS = [f for f in w.namelist() if f.startswith("EPUB/text/ch")]
            CHAPTERS.sort()
            self.metadata.word_count = 0
            for chapter in CHAPTERS:
                file_as = F"EPUB/text/chapter-{chapter.split('.')[0][-1]}.xhtml"
                try:
                    chapter_xml = w.read(chapter)
                except KeyError:
                    continue
                if file_as != chapter:
                    REMOVES.append(chapter)
                chapter_xml = pages.restructure_chapter(chapter_xml)
                chapter_title = chapter_xml.title.string
                words = BeautifulSoup(str(chapter_xml.section), "lxml").get_text()
                self.metadata.word_count += len(words.replace("\n", " ").replace("  ", " ").strip().split())
                w.writestr(file_as, str(chapter_xml))
                spine.append(Matter(partition=MatterPartition.body, title=chapter_title))
            image_manifest = [f.replace("EPUB/", "") for f in w.namelist() if f.startswith("EPUB/images/")]
            for img in [f for f in w.namelist() if f.startswith("EPUB/media/")]:
                REMOVES.append(img)
                new_img = img.replace("/media/", "/images/")
                try:
                    old_img = w.read(img)
                    w.writestr(new_img, old_img)
                except KeyError:
                    continue
                image_manifest.append(new_img.replace("EPUB/", ""))
            w.writestr("EPUB/content.opf", pages.create_content_opf(self.metadata, image_manifest, spine))
            w.writestr("EPUB/toc.ncx", pages.create_toc_ncx(self.metadata, spine))
            w.writestr("EPUB/toc.xhtml", pages.create_toc_xhtml(self.metadata, spine))
            for remove in REMOVES:
                try:
                    w.remove_file(remove)
                except KeyError:
                    continue
def build ( self ) : if not self . work_name or not self . metadata : e = "`set_metadata` before building creative work." raise PermissionError ( e ) epub_path = self . directory . parent / F"{self.work_name}.epub" if self . stateless : pypandoc . convert_file ( str ( self . directory / F"{self.work_name}.docx" ) , format = "docx" , to = "epub3" , outputfile = str ( epub_path ) ) else : pypandoc . convert_text ( self . work , format = "docx" , to = "epub3" , outputfile = str ( epub_path ) ) with UpdateZipFile ( epub_path , "a" ) as w : REMOVES = [ "EPUB/styles/stylesheet1.css" , "EPUB/text/title_page.xhtml" , "EPUB/nav.xhtml" ] DEFAULT = [ ( self . source_path / "css" / "core.css" , "EPUB/css/core.css" ) , ( self . source_path / "images" / "logo.svg" , "EPUB/images/logo.svg" ) , ( self . source_path / "xhtml" / "onix.xml" , "EPUB/onix.xml" ) , ( self . source_path / "xhtml" / "container.xml" , "META-INF/container.xml" ) ] for default_file , write_file in DEFAULT : w . write ( default_file , write_file ) for f in os . listdir ( self . source_path / "fonts" ) : w . write ( self . source_path / "fonts" / f , F"EPUB/fonts/{f}" ) w . writestr ( "EPUB/text/titlepage.xhtml" , pages . create_titlepage_xhtml ( self . metadata ) ) w . writestr ( "EPUB/text/colophon.xhtml" , pages . create_colophon_xhtml ( self . metadata ) ) if self . stateless : for image_path in [ self . directory / F"cover.{t}" for t in [ "jpg" , "jpeg" , "png" , "gif" , "svg" ] ] : if image_path . exists ( ) : w . write ( image_path , F"EPUB/images/{image_path.name}" ) elif self . cover : t = filetype . guess ( self . cover ) . extension w . writestr ( F"EPUB/images/cover.{t}" , self . cover ) spine = [ ] if ( self . directory / "dedication.xhtml" ) . exists ( ) or self . dedication : if self . dedication : w . writestr ( "EPUB/text/dedication.xhtml" , self . dedication ) else : w . write ( self . directory / "dedication.xhtml" , "EPUB/text/dedication.xhtml" ) spine = [ Matter ( partition = "frontmatter" , content = "dedication" , title = "Dedication" ) ] CHAPTERS = [ f for f in w . namelist ( ) if f . startswith ( "EPUB/text/ch" ) ] CHAPTERS . sort ( ) self . metadata . word_count = 0 for chapter in CHAPTERS : file_as = F"EPUB/text/chapter-{chapter.split('.')[0][-1]}.xhtml" try : chapter_xml = w . read ( chapter ) except KeyError : continue if file_as != chapter : REMOVES . append ( chapter ) chapter_xml = pages . restructure_chapter ( chapter_xml ) chapter_title = chapter_xml . title . string words = BeautifulSoup ( str ( chapter_xml . section ) , "lxml" ) . get_text ( ) self . metadata . word_count += len ( words . replace ( "\n" , " " ) . replace ( "  " , " " ) . strip ( ) . split ( ) ) w . writestr ( file_as , str ( chapter_xml ) ) spine . append ( Matter ( partition = MatterPartition . body , title = chapter_title ) ) image_manifest = [ f . replace ( "EPUB/" , "" ) for f in w . namelist ( ) if f . startswith ( "EPUB/images/" ) ] for img in [ f for f in w . namelist ( ) if f . startswith ( "EPUB/media/" ) ] : REMOVES . append ( img ) new_img = img . replace ( "/media/" , "/images/" ) try : old_img = w . read ( img ) w . writestr ( new_img , old_img ) except KeyError : continue image_manifest . append ( new_img . replace ( "EPUB/" , "" ) ) w . writestr ( "EPUB/content.opf" , pages . create_content_opf ( self . metadata , image_manifest , spine ) ) w . writestr ( "EPUB/toc.ncx" , pages . create_toc_ncx ( self . metadata , spine ) ) w . writestr ( "EPUB/toc.xhtml" , pages . create_toc_xhtml ( self . metadata , spine ) ) for remove in REMOVES : try : w . remove_file ( remove ) except KeyError : continue
Automatically build the creative work as a standards compliant EPUB3.
Automatically build the creative work as a standards compliant EPUB3 .
"""
        Automatically build the creative work as a standards compliant EPUB3. Save to the root directory.
        """ # Generate the initial creative content using Pandoc # pypandoc can't handle PosixPaths ... # Maybe one day Pandoc can return an epub object and we won't save the interim file # Generate the epub version # REMOVES # DEFAULT COMPONENTS # DEFAULT FONTS # ADD titlepage.xhtml # ADD colophon.xhtml # ADD cover.img # GET DEDICATION and CHAPTERS # check if the path to dedication exists, if it does, add it to the work and spine # If delete and then re-add same file, causes ZipFile confusion # Restructure chapter xml into standard format # Count the words (XHTML and HTML treated differently by BeautifulSoup, so first extract `section`) # PANDOC MAY STILL ADD IMAGES FOUND IN THE WORK WHICH WE NEED TO DISCOVER AND ADD TO THE MANIFEST # NOTE, these are not only to be added to the manifest, but the folder renamed as well # ADD content.opf # ADD toc.ncx # ADD toc.xhtml # PERFORM REMOVES
self