Example #1
0
    def _get_opf_xmlstring(self, toc_path):

        package = Element('package', prefixes={OPF_NS: None}, attributes={'version': '3.0'})

        package.attributes[(URI_XML, 'lang')] = 'en'

        # unique - identifier = "pub-id"
        if self._find_unique_id():
            package.attributes['unique-identifier'] = self._find_unique_id()

        # Metadata
        package.children.append(self._make_metadata_element())

        # manifest
        manifest = self._make_manifest_element()
        package.children.append(manifest)
        self._process_items_properties(manifest)

        for item in manifest.children:
            if item.attributes[(None, 'href')] == toc_path:
                item.attributes[(None, 'properties')] = 'nav'

            if item.attributes[(None, 'href')] == self.cover_image:
                item.attributes[(None, 'properties')] = 'cover-image'

        # Find ncx id for spine
        toc_ncx_item_e_id = self._find_ncx_id(manifest.children)

        # Spine
        spine = self._make_spine_element()
        package.children.append(spine)
        spine.attributes['toc'] = toc_ncx_item_e_id

        return Xl(root=pretty_insert(package, dont_do_when_one_child=True)).string()
Example #2
0
    def write(self, filename):

        # put nav to temp files
        nav_xmlstring = pretty_insert(self._make_nav_element(), dont_do_when_one_child=True).string()
        toc_nav_path = self._get_unused_filename(None, 'nav.xhtml')
        self._temp_files[toc_nav_path] = File(nav_xmlstring.encode(), mime='application/xhtml+xml')

        # put ncx to temp files
        ncx_xmlstring = pretty_insert(self._make_ncx_element(), dont_do_when_one_child=True).string()
        toc_ncx_filename = self._get_unused_filename(None, 'toc.ncx')
        self._temp_files[toc_ncx_filename] = File(ncx_xmlstring.encode(), mime='application/x-dtbncx+xml')

        # get opf name & data
        opf_data = self._get_opf_xmlstring(toc_nav_path).encode()
        opf_filename = self._get_unused_filename(None, 'package.opf')

        # get container data
        container_data = self._get_container_xmlstring(ROOT_OF_OPF + '/' + opf_filename).encode()

        # make zipfile
        z = zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_DEFLATED)

        # write mimetype as first file in zip
        z.writestr('mimetype', 'application/epub+zip'.encode('ascii'), compress_type=zipfile.ZIP_STORED)

        # wirte files
        for filename, _file in self.files.items():
            z.writestr(ROOT_OF_OPF + os.sep + filename, _file.binary, zipfile.ZIP_DEFLATED)

        # write temp files
        for filename, _file in self._temp_files.items():
            z.writestr(ROOT_OF_OPF + os.sep + filename, _file.binary, zipfile.ZIP_DEFLATED)

        # write opf data
        z.writestr(ROOT_OF_OPF + '/' + opf_filename, opf_data, zipfile.ZIP_DEFLATED)

        # write container
        z.writestr(CONTAINER_PATH, container_data, zipfile.ZIP_DEFLATED)

        z.close()

        self._temp_files.clear()
Example #3
0
def test_parse():
    xmlstr = open(TEST_XML_PATH).read()
    xl = parse(xmlstr)

    e = xl.root

    clean_e = clean_whitespaces(e)

    pretty_e = pretty_insert(clean_e, dont_do_when_one_child=True)

    assert clean_e.string() == clean_whitespaces(pretty_e).string()

    assert pretty_e.string() == pretty_insert(clean_e, dont_do_when_one_child=True).string()

    assert clean_e.string() != pretty_e.string()

    Et.fromstring(xl.string())

    xl.root = clean_e
    Et.fromstring(xl.string())

    xl.root = pretty_e
    Et.fromstring(xl.string())
Example #4
0
    def _get_container_xmlstring(opf_path):
        e = Element('container')

        e.attributes['version'] = '1.0'

        e.prefixes['urn:oasis:names:tc:opendocument:xmlns:container'] = None

        rootfiles = Element('rootfiles')
        e.children.append(rootfiles)

        rootfile = Element('rootfile')
        rootfiles.children.append(rootfile)

        rootfile.attributes['full-path'] = opf_path

        rootfile.attributes['media-type'] = 'application/oebps-package+xml'

        return Xl(root=pretty_insert(e, dont_do_when_one_child=True)).string()
Example #5
0
    def addons_make_toc_page(self):
        """Some EPUB reader not supports nav hidden attribute, they just ignor sub section, not fold.
        So, this member function can make a toc page, with it's little JS code, it can fold and unfold sections.

        You must put the returned file to Epub3.files by yourself.

        :returns: xhtml page file
        :rtype: File
        """

        html = self._make_nav_element()

        def find_element_by_name(tag):
            e = None
            for one in html.children:
                if one.tag == tag:
                    return one

            return e

        head = find_element_by_name((None, 'head'))
        body = find_element_by_name((None, 'body'))

        css_string = open(os.path.join(_dirt(__file__), 'static', 'user_toc_nav.css')).read()
        css = Element('style', attributes={'type': 'text/css'})
        css.children.append(css_string)

        head.children.append(css)

        js_string = io.open(os.path.join(_dirt(__file__), 'static', 'user_toc_nav_fold.js'), encoding='utf-8').read()
        script = Element('script')
        script.children.append(js_string)

        head.children.append(script)

        script_before_body_close = Element('script', attributes={'type': 'text/javascript'})
        script_before_body_close.children.append('set_button();')
        body.children.append(script_before_body_close)

        toc_page = pretty_insert(html).string().encode()
        return File(toc_page)
Example #6
0
    def _get_opf_xmlstring(self):

        package = Element('package', prefixes={OPF_NS: None}, attributes={'version': '2.0'})

        # unique - identifier = "pub-id"
        if self._find_unique_id():
            package.attributes['unique-identifier'] = self._find_unique_id()

        # Metadata
        package.children.append(self._make_metadata_element())

        # Manifest
        manifest = self._make_manifest_element()
        package.children.append(manifest)

        # Find ncx id for spine
        toc_ncx_item_e_id = self._find_ncx_id(manifest.children)

        # Spine
        spine = self._make_spine_element()
        package.children.append(spine)
        spine.attributes['toc'] = toc_ncx_item_e_id

        return Xl(root=pretty_insert(package, dont_do_when_one_child=True)).string()