<?xml version="1.0"  encoding="UTF-8"?>
<?python
from uuid import uuid4
import re
?>
<ncx version="2005-1" 
     xml:lang="en" 
     xmlns="http://www.daisy.org/z3986/2005/ncx/"
     xmlns:py="http://genshi.edgewall.org/"
     xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata"
>
    <head>
        <meta name="dtb:uid" content="${uid}"/>
        <meta name="dtb:depth" content="${toc.depth()}"/>
        <meta name="dtb:generator" content="${__appname__}"/>
        <meta name="dtb:totalPageCount" content="0"/>
        <meta name="dtb:maxPageNumber" content="0"/>
    </head>
    <docTitle><text>Table of Contents</text></docTitle>
    
    <py:def function="navpoint(np, level)">
        ${'%*s'%(4*level,'')}<navPoint id="${str(uuid4())}" playOrder="${str(np.play_order)}">
            ${'%*s'%(4*level,'')}<navLabel>
                ${'%*s'%(4*level,'')}<text>${re.sub(r'\s+', ' ', np.text)}</text>
            ${'%*s'%(4*level,'')}</navLabel>
            ${'%*s'%(4*level,'')}<content src="${unicode(np.href)+(('#' + unicode(np.fragment)) if np.fragment else '')}" />
            ${'%*s'%(4*level,'')}<calibre:meta py:if="np.author" name="author">${np.author}</calibre:meta>
            ${'%*s'%(4*level,'')}<calibre:meta py:if="np.description" name="description">${np.description}</calibre:meta>
            <py:for each="np2 in np">${navpoint(np2, level+1)}</py:for>
        ${'%*s'%(4*level,'')}</navPoint>
    </py:def>
    <navMap>
    <py:for each="np in toc">${navpoint(np, 0)}</py:for>
    </navMap>
</ncx>
