metadata in index file

This commit is contained in:
Fabian Graßl 2010-10-08 20:45:35 +02:00
parent 06e856bf61
commit 66602a84d7
2 changed files with 31 additions and 2 deletions

View File

@ -2,10 +2,24 @@
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<link rel="schema.DCTERMS" href="http://purl.org/dc/terms/" />
<title></title> <title></title>
${for item_name in meta.items:}$
${for item in meta[item_name]:}$
${if namespace(item.term) == DC11_NS:}$
<meta ${print 'name="DC.'+barename(item.term)+'"',}$ ${print 'content="'+item.value+'"',}$ />
${:endif}$
${:endfor}$
${:endfor}$
</head> </head>
<body> <body>
<h1>Table of contents</h1>
${toc}$ ${toc}$
</body> </body>

View File

@ -10,7 +10,7 @@ from os.path import dirname, abspath, relpath, exists, basename
from lxml import etree from lxml import etree
from templite import Templite from templite import Templite
from calibre.ebooks.oeb.base import element from calibre.ebooks.oeb.base import element, namespace, barename, DC11_NS
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
from calibre import CurrentDir from calibre import CurrentDir
@ -25,6 +25,9 @@ class HTMLOutput(OutputFormatPlugin):
recommendations = set([('pretty_print', True, OptionRecommendation.HIGH)]) recommendations = set([('pretty_print', True, OptionRecommendation.HIGH)])
def generate_toc(self, oeb_book, ref_url, output_dir): def generate_toc(self, oeb_book, ref_url, output_dir):
'''
Generate table of contents
'''
with CurrentDir(output_dir): with CurrentDir(output_dir):
def build_node(current_node, parent=None): def build_node(current_node, parent=None):
if parent is None: if parent is None:
@ -62,7 +65,8 @@ class HTMLOutput(OutputFormatPlugin):
link_prefix=basename(output_dir)+'/' link_prefix=basename(output_dir)+'/'
html_toc = self.generate_html_toc(oeb_book, output_file, output_dir) html_toc = self.generate_html_toc(oeb_book, output_file, output_dir)
templite = Templite(P('templates/html_export_default_index.tmpl', data=True)) templite = Templite(P('templates/html_export_default_index.tmpl', data=True))
t = templite.render(toc=html_toc) print oeb_book.metadata.items
t = templite.render(toc=html_toc, meta=oeb_book.metadata, namespace=lambda x:namespace(x), barename=lambda x:barename(x), DC11_NS=DC11_NS)
f.write(t) f.write(t)
with CurrentDir(output_dir): with CurrentDir(output_dir):
@ -83,27 +87,38 @@ class HTMLOutput(OutputFormatPlugin):
path = abspath(unquote(item.href)) path = abspath(unquote(item.href))
dir = dirname(path) dir = dirname(path)
root = item.data.getroottree() root = item.data.getroottree()
# get & clean HTML <HEAD>-data
head = root.xpath('//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] head = root.xpath('//h:head', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0]
head_content = etree.tostring(head, pretty_print=True, encoding='utf-8') head_content = etree.tostring(head, pretty_print=True, encoding='utf-8')
head_content = re.sub(r'\<\/?head.*\>', '', head_content) head_content = re.sub(r'\<\/?head.*\>', '', head_content)
head_content = re.sub(re.compile(r'\<style.*\/style\>', re.M|re.S), '', head_content) head_content = re.sub(re.compile(r'\<style.*\/style\>', re.M|re.S), '', head_content)
# get & clean HTML <BODY>-data
body = root.xpath('//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0] body = root.xpath('//h:body', namespaces={'h': 'http://www.w3.org/1999/xhtml'})[0]
ebook_content = etree.tostring(body, pretty_print=True, encoding='utf-8') ebook_content = etree.tostring(body, pretty_print=True, encoding='utf-8')
ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content) ebook_content = re.sub(r'\<\/?body.*\>', '', ebook_content)
# generate link to next page
if item.spine_position+1 < len(oeb_book.spine): if item.spine_position+1 < len(oeb_book.spine):
nextLink = oeb_book.spine[item.spine_position+1].href nextLink = oeb_book.spine[item.spine_position+1].href
nextLink = relpath(abspath(nextLink), dir) nextLink = relpath(abspath(nextLink), dir)
else: else:
nextLink = None nextLink = None
# generate link to previous page
if item.spine_position > 0: if item.spine_position > 0:
prevLink = oeb_book.spine[item.spine_position-1].href prevLink = oeb_book.spine[item.spine_position-1].href
prevLink = relpath(abspath(prevLink), dir) prevLink = relpath(abspath(prevLink), dir)
else: else:
prevLink = None prevLink = None
# render template
templite = Templite(P('templates/html_export_default.tmpl', data=True)) templite = Templite(P('templates/html_export_default.tmpl', data=True))
toc = lambda: self.generate_html_toc(oeb_book, path, output_dir) toc = lambda: self.generate_html_toc(oeb_book, path, output_dir)
t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink, toc=toc, head_content=head_content) t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink, toc=toc, head_content=head_content)
# write html to file
with open(path, 'wb') as f: with open(path, 'wb') as f:
f.write(t) f.write(t)
item.unload_data_from_memory(memory=path) item.unload_data_from_memory(memory=path)