本文整理汇总了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
示例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
示例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