Пример #1
0
def anchorNCXUpdates(data, ncx_bookpath, originating_bookpath, keylist, valuelist):
    data = _remove_xml_header(data)
    # lxml on a Mac does not seem to handle full unicode properly, so encode as utf-8
    data = data.encode('utf-8')
    # rebuild serialized lookup dictionary
    id_dict = OrderedDict()
    for i in range(0, len(keylist)):
        id_dict[ keylist[i] ] = valuelist[i]
    startdir = startingDir(ncx_bookpath)
    xmlbuilder = LXMLTreeBuilderForXML(parser=None, empty_element_tags=ebook_xml_empty_tags)
    soup = BeautifulSoup(data, features=None, from_encoding="utf-8", builder=xmlbuilder)
    for tag in soup.find_all("content"):
        if "src" in tag.attrs:
            src = tag["src"]
            if src.find(":") == -1:
                parts = src.split('#')
                apath = urldecodepart(parts[0])
                # convert this path to its target bookpath
                target_bookpath = buildBookPath(apath, startdir)
                if (parts is not None) and (len(parts) > 1) and (target_bookpath == originating_bookpath) and (parts[1] != ""):
                    fragment_id = urldecodepart(parts[1])
                    if fragment_id in id_dict:
                        target_bookpath = id_dict[fragment_id]
                        attribute_value = urlencodepart(buildRelativePath(ncx_bookpath, target_bookpath))
                        attribute_value = attribute_value + "#" + urlencodepart(fragment_id)
                        tag["src"] = attribute_value;
    newdata = soup.decodexml(indent_level=0, formatter='minimal', indent_chars="  ")
    return newdata
Пример #2
0
def performPageMapUpdates(data, newbkpath, oldbkpath, keylist, valuelist):
    data = _remove_xml_header(data)
    # lxml on a Mac does not seem to handle full unicode properly, so encode as utf-8
    data = data.encode('utf-8')
    # rebuild serialized lookup dictionary of xml_updates properly adjusted
    updates = OrderedDict()
    for i in range(0, len(keylist)):
        updates[ keylist[i] ] = valuelist[i]
    xml_empty_tags = ["page"]
    xmlbuilder = LXMLTreeBuilderForXML(parser=None, empty_element_tags=xml_empty_tags)
    soup = BeautifulSoup(data, features=None, from_encoding="utf-8", builder=xmlbuilder)
    for tag in soup.find_all(["page"]):
        for att in ["href"]:
            if att in tag.attrs :
                ref = tag[att]
                if ref.find(":") == -1 :
                    parts = ref.split('#')
                    apath = urldecodepart(parts[0])
                    fragment = ""
                    if len(parts) > 1:
                        fragment = urldecodepart(parts[1])
                    oldtarget = buildBookPath(apath, startingDir(oldbkpath))
                    newtarget = updates.get(oldtarget, oldtarget)
                    attribute_value = urlencodepart(buildRelativePath(newbkpath, newtarget))
                    if fragment != "":
                        attribute_value = attribute_value + "#" + urlencodepart(fragment)
                    tag[att] = attribute_value
    newdata = soup.decodexml(indent_level=0, formatter='minimal', indent_chars="  ")
    return newdata
Пример #3
0
def anchorNCXUpdatesAfterMerge(data, ncx_bookpath, sink_bookpath,
                               merged_bookpaths):
    data = _remove_xml_header(data)
    startdir = startingDir(ncx_bookpath)
    # lxml on a Mac does not seem to handle full unicode properly, so encode as utf-8
    data = data.encode('utf-8')
    xmlbuilder = LXMLTreeBuilderForXML(parser=None,
                                       empty_element_tags=ebook_xml_empty_tags)
    soup = BeautifulSoup(data,
                         features=None,
                         from_encoding="utf-8",
                         builder=xmlbuilder)
    for tag in soup.find_all("content"):
        if "src" in tag.attrs:
            src = tag["src"]
            if src.find(":") == -1:
                parts = src.split('#')
                if parts is not None:
                    apath = urldecodepart(parts[0])
                    target_bookpath = buildBookPath(apath, startdir)
                    if target_bookpath in merged_bookpaths:
                        attribute_value = urlencodepart(
                            buildRelativePath(ncx_bookpath, sink_bookpath))
                        if len(parts) > 1 and parts[1] != "":
                            fragment = urldecodepart(parts[1])
                            attribute_value += "#" + urlencodepart(parts[1])
                        tag["src"] = attribute_value
    newdata = soup.decodexml(indent_level=0,
                             formatter='minimal',
                             indent_chars="  ")
    return newdata
Пример #4
0
 def _parseData(self):
     cnt = 0
     for prefix, tname, tattr, tcontent in self._opf_tag_iter():
         # package
         if tname == "package":
             ver = tattr.pop("version", "2.0")
             uid = tattr.pop("unique-identifier", "bookid")
             if self.ns_remap:
                 if "xmlns:opf" in tattr:
                     tattr.pop("xmlns:opf")
                     tattr["xmlns"] = "http://www/idpf.org/2007/opf"
             self.package = (ver, uid, tattr)
             continue
         # metadata
         if tname == "metadata":
             if self.ns_remap:
                 if not "xmlns:opf" in tattr:
                     tattr["xmlns:opf"] = "http://www/idpf.org/2007/opf"
             self.metadata_attr = tattr
             continue
         if tname in ["meta", "link"
                      ] or tname.startswith("dc:") and "metadata" in prefix:
             self.metadata.append((tname, tcontent, tattr))
             continue
         # manifest
         if tname == "item" and "manifest" in prefix:
             nid = "xid%03d" % cnt
             cnt += 1
             id = tattr.pop("id", nid)
             # must keep all hrefs in encoded) form
             # if relative, then no fragments so decode and then encode for safety
             href = tattr.pop("href", "")
             if href.find(':') == -1:
                 href = urldecodepart(href)
                 href = urlencodepart(href)
             mtype = tattr.pop("media-type", "")
             self.manifest.append((id, href, mtype, tattr))
             continue
         # spine
         if tname == "spine":
             self.spine_attr = tattr
             continue
         if tname == "itemref" and "spine" in prefix:
             idref = tattr.pop("idref", "")
             self.spine.append((idref, tattr))
             continue
         # guide
         if tname == "reference" and "guide" in prefix:
             type = tattr.pop("type", "")
             title = tattr.pop("title", "")
             # must keep all hrefs in quoted (encoded) form
             href = tattr.pop("href", "")
             self.guide.append((type, title, href))
             continue
         # bindings
         if tname in ["mediaType", "mediatype"] and "bindings" in prefix:
             mtype = tattr.pop("media-type", "")
             handler = tattr.pop("handler", "")
             self.bindings.append((mtype, handler))
             continue
Пример #5
0
 def build_manifest_xml(self):
     manout = []
     manout.append('  <manifest>\n')
     for id in sorted(self.id_to_mime):
         href = self.id_to_href[id]
         # relative manifest hrefs must have no fragments
         if href.find(':') == -1:
             href = urlencodepart(href)
         mime = self.id_to_mime[id]
         props = ''
         properties = self.id_to_props[id]
         if properties is not None:
             props = ' properties="%s"' % properties
         fall = ''
         fallback = self.id_to_fall[id]
         if fallback is not None:
             fall = ' fallback="%s"' % fallback
         over = ''
         overlay = self.id_to_over[id]
         if overlay is not None:
             over = ' media-overlay="%s"' % overlay
         manout.append(
             '    <item id="%s" href="%s" media-type="%s"%s%s%s />\n' %
             (id, href, mime, props, fall, over))
     manout.append('  </manifest>\n')
     return "".join(manout)
Пример #6
0
def parse_nav(qp, navdata, navbkpath, newdir):
    qp.setContent(navdata)
    toclist = []
    pagelist = []
    landmarks = []
    lvl = 0
    pgcnt = 0
    maxlvl = -1
    nav_type = None
    href = None
    title = ""
    play = 0
    navdir = startingDir(navbkpath)

    for txt, tp, tname, ttype, tattr in qp.parse_iter():
        if txt is not None:
            if ".a." in tp or tp.endswith(".a"):
                title = title + txt
            else:
                title = ""
        else:
            if tname == "nav":
                if ttype == "begin":
                    nav_type = tattr.get("epub:type", None)
                if ttype == "end":
                    nav_type = None
                continue
            if tname == "ol" and nav_type is not None and nav_type in (
                    "toc", "page-list", "landmarks"):
                if ttype == "begin":
                    lvl += 1
                    if nav_type == "toc":
                        if lvl > maxlvl: maxlvl = lvl
                if ttype == "end": lvl -= 1
                continue
            if tname == "a" and ttype == "begin":
                # get the raw href (urlencoded)
                href = tattr.get("href", "")
                if href.find(":") == -1:
                    # first strip off any fragment
                    fragment = ""
                    if href.find("#") != -1:
                        href, fragment = href.split("#")
                    # find destination bookpath
                    href = urldecodepart(href)
                    fragment = urldecodepart(fragment)
                    if href.startswith("./"): href = href[2:]
                    if href == "":
                        destbkpath = navbkpath
                    else:
                        destbkpath = buildBookPath(href, navdir)
                    # create relative path to destbkpath from newdir
                    href = relativePath(destbkpath, newdir)
                    href = urlencodepart(href)
                    fragment = urlencodepart(fragment)
                    if fragment != "":
                        href = href + "#" + fragment
                epubtype = tattr.get("epub:type", None)
                continue
            if tname == "a" and ttype == "end":
                if nav_type == "toc":
                    play += 1
                    toclist.append((play, lvl, href, title))
                elif nav_type == "page-list":
                    pgcnt += 1
                    pagelist.append((pgcnt, href, title))
                elif nav_type == "landmarks":
                    if epubtype is not None:
                        gtype = _epubtype_guide_map.get(epubtype, None)
                        landmarks.append((gtype, href, title))
                title = ""
                continue

    return toclist, pagelist, landmarks, maxlvl, pgcnt
Пример #7
0
 def launch(self):
     script_module = self.script_module
     script_type = self.script_type
     container = self.container
     sys.stdout = SavedStream(sys.stdout, 'stdout', self)
     sys.stderr = SavedStream(sys.stderr, 'stderr', self)
     try:
         target_script = __import__(script_module)
         self.exitcode = target_script.run(container)
         sys.stdout = sys.stdout.stream
         sys.stderr = sys.stderr.stream
     except Exception as e:
         sys.stderr.write(traceback.format_exc())
         sys.stderr.write("Error: %s\n" % e)
         sys.stdout = sys.stdout.stream
         sys.stderr = sys.stderr.stream
         self.exitcode = -1
         pass
     if self.exitcode != 0:
         self.wrapout.append(_XML_HEADER)
         self.wrapout.append(
             '<wrapper type="%s">\n<result>failed</result>\n<changes/>\n' %
             script_type)
         return self.exitcode
     if script_type == "edit":
         # write out the final updated opf to the outdir
         container._w.write_opf()
     # save the wrapper results to a file before exiting the thread
     # Note: the hrefs generated for the result xml are all relative paths
     #       with no fragments since they must refer to entire files
     self.wrapout.append(_XML_HEADER)
     self.wrapout.append('<wrapper type="%s">\n' % script_type)
     self.wrapout.append('<result>success</result>\n')
     self.wrapout.append('<changes>\n')
     if script_type == "edit":
         for ftype, id, href in container._w.deleted:
             if ftype == 'manifest':
                 bookhref = href
                 mime = container._w.getmime(bookhref)
             else:
                 bookhref = id
                 id = ""
                 mime = container._w.getmime(bookhref)
             self.wrapout.append(
                 '<deleted href="%s" id="%s" media-type="%s" />\n' %
                 (urlencodepart(bookhref), id, mime))
     if script_type in ['input', 'edit']:
         for id in container._w.added:
             if id in container._w.id_to_bookpath:
                 bookhref = container._w.id_to_bookpath[id]
                 mime = container._w.id_to_mime[id]
             else:
                 bookhref = id
                 id = ""
                 mime = container._w.getmime(bookhref)
             self.wrapout.append(
                 '<added href="%s" id="%s" media-type="%s" />\n' %
                 (urlencodepart(bookhref), id, mime))
     if script_type == 'edit':
         for id in container._w.modified:
             if id in container._w.id_to_bookpath:
                 bookhref = container._w.id_to_bookpath[id]
                 mime = container._w.id_to_mime[id]
             else:
                 bookhref = id
                 id = ""
                 mime = container._w.getmime(bookhref)
             self.wrapout.append(
                 '<modified href="%s" id="%s" media-type="%s" />\n' %
                 (urlencodepart(bookhref), id, mime))
     if script_type == 'validation':
         for vres in container.results:
             self.wrapout.append(
                 '<validationresult type="%s" bookpath="%s" linenumber="%s" charoffset="%s" message="%s" />\n'
                 % (vres.restype, vres.bookpath, vres.linenumber,
                    vres.charoffset, vres.message))
     self.wrapout.append('</changes>\n')
     self.exitcode = 0
     return