Пример #1
0
def build_ncx(doctitle, mainid, maxlvl, pgcnt, toclist, pagelist):
    ncxres = []
    ind = '  '
    ncxres.append('<?xml version="1.0" encoding="utf-8"?>\n')
    ncxres.append('<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">\n')
    ncxres.append('  <head>\n')
    ncxres.append('    <meta name="dtb:uid" content="' + mainid + '" />\n')
    ncxres.append('    <meta name="dtb:depth" content="' + str(maxlvl) + '" />\n')
    ncxres.append('    <meta name="dtb:totalPageCount" content="' + str(pgcnt) + '" />\n')
    ncxres.append('    <meta name="dtb:maxPageNumber" content="' + str(pgcnt) + '" />\n')
    ncxres.append('  </head>\n')
    ncxres.append('<docTitle>\n')
    ncxres.append('  <text>' + doctitle + '</text>\n')
    ncxres.append('</docTitle>\n')
    ncxres.append('<navMap>\n')
    plvl = -1
    for (po, lvl, href, title) in toclist:
        # first close off any already opened navPoints
        while lvl <= plvl:
            space = ind*plvl
            ncxres.append(space + '</navPoint>\n')
            plvl -= 1
        # now append this navpoint
        space = ind*lvl
        porder = str(po)
        if title is None:
            title = ""
        ncxres.append(space + '<navPoint id="navPoint' + porder +'">\n')
        ncxres.append(space + '  <navLabel>\n')
        ncxres.append(space + '    <text>' + title + '</text>\n')
        ncxres.append(space + '  </navLabel>\n')
        ncxres.append(space + '  <content src="' + quoteurl(href) + '" />\n')
        plvl = lvl
    # now finish off any open navpoints
    while plvl > 0:
        space = ind*plvl
        ncxres.append(space + '</navPoint>\n')
        plvl -= 1
    ncxres.append('</navMap>\n')
    if pgcnt > 0:
        play = len(toclist)
        ncxres.append('<pageList>\n')
        for (cnt, href, title) in pagelist:
            porder = str(play + cnt)
            target = ind + '<pageTarget id="navPoint' + porder + '" type="normal"'
            target += ' value="' + title + '">\n'
            ncxres.append(target)
            ncxres.append(ind*2 + '<navLabel><text>' + title + '</text></navLabel>\n')
            ncxres.append(ind*2 + '<content src="' + quoteurl(href) + '" />\n')
            ncxres.append(ind + '</pageTarget>\n')
        ncxres.append('</pageList>\n')
    # now close it off
    ncxres.append('</ncx>\n')
    return  "".join(ncxres)
Пример #2
0
 def build_guide_xml(self):
     guideout = []
     if len(self.guide) > 0:
         guideout.append('  <guide>\n')
         for (type, title, href) in self.guide:
             href = quoteurl(href)
             guideout.append('    <reference type="%s" href="%s" title="%s"/>\n' % (type, href, title))
         guideout.append('  </guide>\n')
     return "".join(guideout)
Пример #3
0
 def buildTOC(self, toclist):
     navres = []
     ind = '  '
     ibase = ind * 3
     incr = ind * 2
     # start with the toc
     navres.append(ind * 2 + '<nav epub:type="toc" id="toc">\n')
     navres.append(ind * 3 + '<h1>Table of Contents</h1>\n')
     navres.append(ibase + '<ol>\n')
     curlvl = 1
     initial = True
     for po, lvl, href, lbl in toclist:
         href = quoteurl(href)
         lbl = xmlencode(lbl)
         if lvl > curlvl:
             while lvl > curlvl:
                 indent = ibase + incr * (curlvl)
                 navres.append(indent + '<ol>\n')
                 navres.append(indent + ind + '<li>\n')
                 navres.append(indent + ind * 2 + '<a href="%s">%s</a>\n' %
                               (href, lbl))
                 curlvl += 1
         elif lvl < curlvl:
             while lvl < curlvl:
                 indent = ibase + incr * (curlvl - 1)
                 navres.append(indent + ind + '</li>\n')
                 navres.append(indent + '</ol>\n')
                 curlvl -= 1
             indent = ibase + incr * (lvl - 1)
             navres.append(indent + ind + '</li>\n')
             navres.append(indent + ind + '<li>\n')
             navres.append(indent + ind * 2 + '<a href="%s">%s</a>\n' %
                           (href, lbl))
         else:
             indent = ibase + incr * (lvl - 1)
             if not initial:
                 navres.append(indent + ind + '</li>\n')
             navres.append(indent + ind + '<li>\n')
             navres.append(indent + ind * 2 + '<a href="%s">%s</a>\n' %
                           (href, lbl))
         initial = False
         curlvl = lvl
     while (curlvl > 0):
         indent = ibase + incr * (curlvl - 1)
         navres.append(indent + ind + "</li>\n")
         navres.append(indent + "</ol>\n")
         curlvl -= 1
     navres.append(ind * 2 + '</nav>\n')
     return "".join(navres)
Пример #4
0
 def buildLandmarks(self, landmarks):
     navres = []
     ind = '  '
     navres.append(ind * 2 +
                   '<nav epub:type="landmarks" id="landmarks" hidden="">\n')
     navres.append(ind * 3 + '<h2>Guide</h2>\n')
     navres.append(ind * 3 + '<ol>\n')
     for etyp, href, title in landmarks:
         href = quoteurl(href)
         title = xmlencode(title)
         navres.append(ind * 4 + '<li>\n')
         navres.append(ind * 5 + '<a epub:type="%s" href="%s">%s</a>\n' %
                       (etyp, href, title))
         navres.append(ind * 4 + '</li>\n')
     navres.append(ind * 3 + '</ol>\n')
     navres.append(ind * 2 + '</nav>\n')
     return "".join(navres)
Пример #5
0
 def buildPageList(self, pagelist):
     navres = []
     ind = '  '
     # add any existing page-list if need be
     if len(pagelist) > 0:
         navres.append(
             ind * 2 +
             '<nav epub:type="page-list" id="page-list" hidden="">\n')
         navres.append(ind * 3 + '<ol>\n')
         for pn, href, title in pagelist:
             href = quoteurl(href)
             title = xmlencode(title)
             navres.append(ind * 4 + '<li><a href="%s">%s</a></li>\n' %
                           (href, title))
         navres.append(ind * 3 + '</ol>\n')
         navres.append(ind * 2 + '</nav>\n')
     return "".join(navres)
Пример #6
0
 def build_manifest_xml(self):
     manout = []
     manout.append('  <manifest>\n')
     for id in sorted(self.id_to_mime):
         href = quoteurl(self.id_to_href[id])
         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)
Пример #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
     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' % (quoteurl(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' % (quoteurl(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' % (quoteurl(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