-def add_to_manifest(manifest, partno):
- """ Adds a node to the manifest section in content.opf file """
-
- partstr = 'part%d' % partno
- e = manifest.makeelement(
- OPFNS('item'), attrib={'id': partstr, 'href': partstr + '.html',
- 'media-type': 'application/xhtml+xml'}
- )
- manifest.append(e)
-
-
-def add_to_spine(spine, partno):
- """ Adds a node to the spine section in content.opf file """
-
- e = spine.makeelement(
- OPFNS('itemref'),
- attrib={'idref': 'part%d' % partno}
- )
- spine.append(e)
-
-
-class TOC(object):
- def __init__(self, name=None, part_href=None):
- self.children = []
- self.name = name
- self.part_href = part_href
- self.sub_number = None
-
- def add(self, name, part_href, level=0, is_part=True, index=None):
- assert level == 0 or index is None
- if level > 0 and self.children:
- return self.children[-1].add(name, part_href, level - 1, is_part)
- else:
- t = TOC(name)
- t.part_href = part_href
- if index is not None:
- self.children.insert(index, t)
- else:
- self.children.append(t)
- if not is_part:
- t.sub_number = len(self.children) + 1
- return t.sub_number
-
- def append(self, toc):
- self.children.append(toc)
-
- def extend(self, toc):
- self.children.extend(toc.children)
-
- def depth(self):
- if self.children:
- return max((c.depth() for c in self.children)) + 1
- else:
- return 0
-
- def href(self):
- src = self.part_href
- if self.sub_number is not None:
- src += '#sub%d' % self.sub_number
- return src
-
- def write_to_xml(self, nav_map, counter=1):
- for child in self.children:
- nav_point = nav_map.makeelement(NCXNS('navPoint'))
- nav_point.set('id', 'NavPoint-%d' % counter)
- nav_point.set('playOrder', str(counter))
-
- nav_label = nav_map.makeelement(NCXNS('navLabel'))
- text = nav_map.makeelement(NCXNS('text'))
- if child.name is not None:
- text.text = re.sub(r'\n', ' ', child.name)
- else:
- text.text = child.name
- nav_label.append(text)
- nav_point.append(nav_label)
-
- content = nav_map.makeelement(NCXNS('content'))
- content.set('src', child.href())
- nav_point.append(content)
- nav_map.append(nav_point)
- counter = child.write_to_xml(nav_point, counter + 1)
- return counter
-
- def html_part(self, depth=0):
- texts = []
- for child in self.children:
- texts.append(
- "<div style='margin-left:%dem;'><a href='%s'>%s</a></div>" %
- (depth, child.href(), child.name))
- texts.append(child.html_part(depth + 1))
- return "\n".join(texts)
-
- def html(self):
- with open(get_resource('epub/toc.html'), 'rb') as f:
- t = f.read().decode('utf-8')
- return t % self.html_part()
-
-