當前位置: 首頁>>代碼示例>>Python>>正文


Python TOC.flat方法代碼示例

本文整理匯總了Python中calibre.ebooks.metadata.toc.TOC.flat方法的典型用法代碼示例。如果您正苦於以下問題:Python TOC.flat方法的具體用法?Python TOC.flat怎麽用?Python TOC.flat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在calibre.ebooks.metadata.toc.TOC的用法示例。


在下文中一共展示了TOC.flat方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: build_toc

# 需要導入模塊: from calibre.ebooks.metadata.toc import TOC [as 別名]
# 或者: from calibre.ebooks.metadata.toc.TOC import flat [as 別名]
def build_toc(index_entries):
    ans = TOC(base_path=os.getcwdu())
    levels = {x['hlvl'] for x in index_entries}
    num_map = {-1: ans}
    level_map = {l:[x for x in index_entries if x['hlvl'] == l] for l in
            levels}
    for lvl in sorted(levels):
        for item in level_map[lvl]:
            parent = num_map[item['parent']]
            child = parent.add_item(item['href'], item['idtag'], item['text'])
            num_map[item['num']] = child

    # Set play orders in depth first order
    for i, item in enumerate(ans.flat()):
        item.play_order = i

    return ans
開發者ID:Eksmo,項目名稱:calibre,代碼行數:19,代碼來源:ncx.py

示例2: from_headings

# 需要導入模塊: from calibre.ebooks.metadata.toc import TOC [as 別名]
# 或者: from calibre.ebooks.metadata.toc.TOC import flat [as 別名]
def from_headings(body, log, namespace):
    ' Create a TOC from headings in the document '
    XPath, descendants = namespace.XPath, namespace.descendants
    headings = ('h1', 'h2', 'h3')
    tocroot = TOC()
    xpaths = [XPath('//%s' % x) for x in headings]
    level_prev = {i+1:None for i in xrange(len(xpaths))}
    level_prev[0] = tocroot
    level_item_map = {i+1:frozenset(xp(body)) for i, xp in enumerate(xpaths)}
    item_level_map = {e:i for i, elems in level_item_map.iteritems() for e in elems}

    idcount = Count()

    def ensure_id(elem):
        ans = elem.get('id', None)
        if not ans:
            idcount.val += 1
            ans = 'toc_id_%d' % idcount.val
            elem.set('id', ans)
        return ans

    for item in descendants(body, *headings):
        lvl = plvl = item_level_map.get(item, None)
        if lvl is None:
            continue
        parent = None
        while parent is None:
            plvl -= 1
            parent = level_prev[plvl]
        lvl = plvl + 1
        elem_id = ensure_id(item)
        text = elem_to_toc_text(item)
        toc = parent.add_item('index.html', elem_id, text)
        level_prev[lvl] = toc
        for i in xrange(lvl+1, len(xpaths)+1):
            level_prev[i] = None

    if len(tuple(tocroot.flat())) > 1:
        log('Generating Table of Contents from headings')
        return tocroot
開發者ID:Riva3000,項目名稱:calibre,代碼行數:42,代碼來源:toc.py

示例3: create_toc

# 需要導入模塊: from calibre.ebooks.metadata.toc import TOC [as 別名]
# 或者: from calibre.ebooks.metadata.toc.TOC import flat [as 別名]
    def create_toc(self):
        " Create a TOC from headings in the document "
        root = self.body
        headings = ("h1", "h2", "h3")
        tocroot = TOC()
        xpaths = [XPath("//%s" % x) for x in headings]
        level_prev = {i + 1: None for i in xrange(len(xpaths))}
        level_prev[0] = tocroot
        level_item_map = {i + 1: frozenset(xp(root)) for i, xp in enumerate(xpaths)}
        item_level_map = {e: i for i, elems in level_item_map.iteritems() for e in elems}

        self.idcount = 0

        def ensure_id(elem):
            ans = elem.get("id", None)
            if not ans:
                self.idcount += 1
                ans = "toc_id_%d" % self.idcount
                elem.set("id", ans)
            return ans

        for item in root.iterdescendants(*headings):
            lvl = plvl = item_level_map.get(item, None)
            if lvl is None:
                continue
            parent = None
            while parent is None:
                plvl -= 1
                parent = level_prev[plvl]
            lvl = plvl + 1
            elem_id = ensure_id(item)
            text = elem_to_toc_text(item)
            toc = parent.add_item("index.html", elem_id, text)
            level_prev[lvl] = toc
            for i in xrange(lvl + 1, len(xpaths) + 1):
                level_prev[i] = None

        if len(tuple(tocroot.flat())) > 1:
            return tocroot
開發者ID:bjhemens,項目名稱:calibre,代碼行數:41,代碼來源:to_html.py


注:本文中的calibre.ebooks.metadata.toc.TOC.flat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。