def from_links(container):
 toc = TOC()
 link_path = XPath(u'//h:a[@href]')
 (seen_titles, seen_dests) = (set(), set())
 for spinepath in container.spine_items:
  name = container.abspath_to_name(spinepath)
  root = container.parsed(name)
  for a in link_path(root):
   href = a.get(u'href')
   if ((not href) or (not href.strip())):
    continue
   dest = container.href_to_name(href, base=name)
   frag = (href.rpartition(u'#')[(-1)] or None)
   if ((dest, frag) in seen_dests):
    continue
   seen_dests.add((dest, frag))
   text = elem_to_toc_text(a)
   if (text in seen_titles):
    continue
   seen_titles.add(text)
   toc.add(text, dest, frag=frag)
 verify_toc_destinations(container, toc)
 for child in toc:
  if (not child.dest_exists):
   toc.remove(child)
 return toc

def from_links ( container ) : toc = TOC ( ) link_path = XPath ( u'//h:a[@href]' ) ( seen_titles , seen_dests ) = ( set ( ) , set ( ) ) for spinepath in container . spine_items : name = container . abspath_to_name ( spinepath ) root = container . parsed ( name ) for a in link_path ( root ) : href = a . get ( u'href' ) if ( ( not href ) or ( not href . strip ( ) ) ) : continue dest = container . href_to_name ( href , base = name ) frag = ( href . rpartition ( u'#' ) [ ( - 1 ) ] or None ) if ( ( dest , frag ) in seen_dests ) : continue seen_dests . add ( ( dest , frag ) ) text = elem_to_toc_text ( a ) if ( text in seen_titles ) : continue seen_titles . add ( text ) toc . add ( text , dest , frag = frag ) verify_toc_destinations ( container , toc ) for child in toc : if ( not child . dest_exists ) : toc . remove ( child ) return toc
generate a table of contents from links in the book .