<package version="2.0" 
         xmlns="http://www.idpf.org/2007/opf" 
         xmlns:py="http://genshi.edgewall.org/" 
         unique-identifier="${__appname__}_id"
         
>
    <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata">
        <dc:title py:with="attrs={'opf:file-as':mi.title_sort}" py:attrs="attrs">${mi.title}</dc:title>
        <dc:creator opf:role="aut" py:for="i, author in enumerate(mi.authors)" py:attrs="{'opf:file-as':mi.author_sort} if mi.author_sort and i == 0 else {}">${author}</dc:creator>
        <dc:contributor opf:role="bkp" py:with="attrs={'opf:file-as':__appname__}" py:attrs="attrs">${'%s (%s)'%(__appname__, __version__)} [http://${__appname__}.kovidgoyal.net]</dc:contributor> 
        <dc:identifier opf:scheme="${__appname__}" id="${__appname__}_id">${mi.application_id}</dc:identifier>
        <dc:date py:if="getattr(mi, 'pubdate', None) is not None">${mi.pubdate.isoformat()}</dc:date>    
        <dc:language>${mi.language if mi.language else 'UND'}</dc:language>
        <dc:type py:if="getattr(mi, 'category', False)">${mi.category}</dc:type>
        <dc:description py:if="mi.comments">${mi.comments}</dc:description>
        <dc:publisher py:if="mi.publisher">${mi.publisher}</dc:publisher>
        <dc:identifier opf:scheme="ISBN" py:if="mi.isbn">${mi.isbn}</dc:identifier>
        <dc:rights py:if="mi.rights">${mi.rights}</dc:rights>
        <meta py:if="mi.series is not None" name="calibre:series" content="${mi.series}"/>
        <meta py:if="mi.series_index is not None" name="calibre:series_index" content="${mi.format_series_index()}"/>
        <meta py:if="mi.rating is not None" name="calibre:rating" content="${mi.rating}"/>
        <meta py:if="mi.timestamp is not None" name="calibre:timestamp" content="${mi.timestamp.isoformat()}"/>
        <meta py:if="mi.publication_type is not None" name="calibre:publication_type" content="${mi.publication_type}" />
        <py:for each="tag in mi.tags">
        <dc:subject py:if="mi.tags is not None">${tag}</dc:subject>
        </py:for>
    </metadata>
    
    <manifest py:if="getattr(mi, 'manifest', None)">
        <py:for each="ref in mi.manifest">
        <item id="${ref.id}" href="${ref.href()}" media-type="${ref.mime_type}" /> 
        </py:for>
    </manifest>
    
    <guide py:if="getattr(mi, 'guide', None)">
    <py:for each="ref in mi.guide">
        <reference type="${ref.type}" href="${ref.href()}" py:with="attrs={'title': ref.title if ref.title else None}" py:attrs="attrs" /> 
    </py:for>    
    </guide>
    
    <spine py:if="getattr(mi, 'spine', None)" 
           py:with="attrs={'toc':'ncx' if mi.toc else None}" py:attrs="attrs">
        <py:for each="resource in mi.spine">
        <itemref idref="${resource.id}" />
        </py:for>
    </spine> 
    
       
</package>
