Esempio n. 1
0
 def update_item(self, item, where, name, frag, title):
     if isinstance(frag, tuple):
         frag = add_id(self.ebook, name, *frag)
     child = TOC(title, name, frag)
     child.dest_exists = True
     if item is None:
         # New entry at root level
         c = self.create_item(self.root, child)
         self.tocw.setCurrentItem(c, 0, QItemSelectionModel.ClearAndSelect)
         self.tocw.scrollToItem(c)
     else:
         if where is None:
             # Editing existing entry
             self.populate_item(item, child)
         else:
             if where == 'inside':
                 parent = item
                 idx = -1
             else:
                 parent = item.parent() or self.root
                 idx = parent.indexOfChild(item)
                 if where == 'after':
                     idx += 1
             c = self.create_item(parent, child, idx=idx)
             self.tocw.setCurrentItem(c, 0,
                                      QItemSelectionModel.ClearAndSelect)
             self.tocw.scrollToItem(c)
Esempio n. 2
0
def create_toc(mi, opf, html_name, lang):
    uuid = ''
    for u in opf.xpath('//*[@id="uuid_id"]'):
        uuid = u.text
    toc = TOC()
    toc.add(_('Start'), html_name)
    return create_ncx(toc, lambda x: x, mi.title, lang, uuid)
Esempio n. 3
0
def create_toc(mi, opf, html_name, lang):
    uuid = ''
    for u in opf.xpath('//*[@id="uuid_id"]'):
        uuid = u.text
    toc = TOC()
    toc.add(_('Start'), html_name)
    return create_ncx(toc, lambda x:x, mi.title, lang, uuid)
Esempio n. 4
0
 def update_item(self, item, where, name, frag, title):
     if isinstance(frag, tuple):
         frag = add_id(self.ebook, name, *frag)
     child = TOC(title, name, frag)
     child.dest_exists = True
     if item is None:
         # New entry at root level
         c = self.create_item(self.root, child)
         self.tocw.setCurrentItem(c, 0, QItemSelectionModel.ClearAndSelect)
         self.tocw.scrollToItem(c)
     else:
         if where is None:
             # Editing existing entry
             self.populate_item(item, child)
         else:
             if where == 'inside':
                 parent = item
                 idx = -1
             else:
                 parent = item.parent() or self.root
                 idx = parent.indexOfChild(item)
                 if where == 'after':
                     idx += 1
             c = self.create_item(parent, child, idx=idx)
             self.tocw.setCurrentItem(c, 0, QItemSelectionModel.ClearAndSelect)
             self.tocw.scrollToItem(c)
Esempio n. 5
0
def parse_outline(raw, output_dir):
    from lxml import etree
    from calibre.ebooks.oeb.parse_utils import RECOVER_PARSER
    raw = clean_xml_chars(
        xml_to_unicode(raw, strip_encoding_pats=True, assume_utf8=True)[0])
    outline = etree.fromstring(raw,
                               parser=RECOVER_PARSER).xpath('(//outline)[1]')
    if outline:
        from calibre.ebooks.oeb.polish.toc import TOC, create_ncx
        outline = outline[0]
        toc = TOC()
        count = [0]

        def process_node(node, toc):
            for child in node.iterchildren('*'):
                if child.tag == 'outline':
                    parent = toc.children[-1] if toc.children else toc
                    process_node(child, parent)
                else:
                    if child.text:
                        page = child.get('page', '1')
                        toc.add(child.text, 'index.html', 'p' + page)
                        count[0] += 1

        process_node(outline, toc)
        if count[0] > 2:
            root = create_ncx(toc, (lambda x: x), 'pdftohtml', 'en',
                              'pdftohtml')
            with open(os.path.join(output_dir, 'toc.ncx'), 'wb') as f:
                f.write(
                    etree.tostring(root,
                                   pretty_print=True,
                                   with_tail=False,
                                   encoding='utf-8',
                                   xml_declaration=True))
Esempio n. 6
0
    def create_toc(self):
        root = TOC()

        def process_node(parent, toc_parent):
            for i in xrange(parent.childCount()):
                item = parent.child(i)
                title = unicode(item.data(0, Qt.DisplayRole) or '').strip()
                toc = item.data(0, Qt.UserRole)
                dest, frag = toc.dest, toc.frag
                toc = toc_parent.add(title, dest, frag)
                process_node(item, toc)

        process_node(self.tocw.invisibleRootItem(), root)
        return root