output & templates

This commit is contained in:
Fabian Graßl 2010-10-07 20:17:36 +02:00
parent 498c34bca0
commit 0aac115afa
4 changed files with 61 additions and 59 deletions

View File

@ -2,7 +2,7 @@
<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" />
<title>Unbenanntes Dokument</title> <title></title>
</head> </head>
<body> <body>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
${toc}$
</body>
</html>

View File

@ -5,12 +5,14 @@ __docformat__ = 'restructuredtext en'
import os, re import os, re
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.customize.conversion import OutputFormatPlugin from calibre.ebooks.oeb.base import element
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
from calibre import CurrentDir from calibre import CurrentDir
from calibre.customize.conversion import OptionRecommendation
from urllib import unquote from urllib import unquote
@ -22,26 +24,49 @@ 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):
with CurrentDir(output_dir):
def build_node(current_node, parent=None):
if parent is None:
parent = etree.Element('ul')
elif len(current_node.nodes):
parent = element(parent, ('ul'))
for node in current_node.nodes:
point = element(parent, 'li')
href = relpath(abspath(unquote(node.href)), dirname(ref_url))
link = element(point, 'a', href=href)
title = node.title
if title:
title = re.sub(r'\s+', ' ', title)
link.text=title
build_node(node, point)
return parent
lang = unicode(oeb_book.metadata.language[0])
wrap = etree.Element('div')
wrap.append(build_node(oeb_book.toc))
return wrap
def convert(self, oeb_book, output_path, input_plugin, opts, log): def convert(self, oeb_book, output_path, input_plugin, opts, log):
self.log = log self.log = log
self.opts = opts self.opts = opts
output_file = output_path output_file = output_path
output_path = re.sub(r'\.html', '', output_path)+'_files' output_dir = re.sub(r'\.html', '', output_path)+'_files'
if not exists(output_dir):
os.makedirs(output_dir)
with open(output_file, 'wb') as f: with open(output_file, 'wb') as f:
link_prefix=os.path.basename(output_path)+'/' link_prefix=basename(output_dir)+'/'
root = oeb_book.html_toc(link_prefix=link_prefix) root = self.generate_toc(oeb_book, output_dir, output_dir)
html_txt = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=False) html_toc = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=False)
f.write(html_txt) templite = Templite(P('templates/html_export_default_index.tmpl', data=True))
t = templite.render(toc=html_toc)
f.write(t)
if not os.path.exists(output_path): with CurrentDir(output_dir):
os.makedirs(output_path)
with CurrentDir(output_path):
for item in oeb_book.manifest: for item in oeb_book.manifest:
path = os.path.abspath(unquote(item.href)) path = abspath(unquote(item.href))
dir = os.path.dirname(path) dir = dirname(path)
if not os.path.exists(dir): if not exists(dir):
os.makedirs(dir) os.makedirs(dir)
if item.spine_position is not None: if item.spine_position is not None:
with open(path, 'wb') as f: with open(path, 'wb') as f:
@ -52,32 +77,24 @@ class HTMLOutput(OutputFormatPlugin):
item.unload_data_from_memory(memory=path) item.unload_data_from_memory(memory=path)
for item in oeb_book.spine: for item in oeb_book.spine:
path = os.path.abspath(unquote(item.href)) path = abspath(unquote(item.href))
dir = os.path.dirname(path) dir = dirname(path)
root = item.data.getroottree() root = item.data.getroottree()
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)
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 = os.path.abspath((nextLink)) nextLink = relpath(abspath(nextLink), dir)
nextLink = os.path.relpath(nextLink, dir)
else: else:
nextLink = None nextLink = None
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 = os.path.abspath((prevLink)) prevLink = relpath(abspath(prevLink), dir)
prevLink = os.path.relpath(prevLink, dir)
else: else:
prevLink = None prevLink = None
vars = { templite = Templite(P('templates/html_export_default.tmpl', data=True))
}
template_file = os.path.dirname(__file__)+'/outputtemplates/default.tmpl'
templite = Templite(open(template_file).read())
t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink) t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink)
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)

View File

@ -1644,22 +1644,6 @@ class TOC(object):
node.to_ncx(point) node.to_ncx(point)
return parent return parent
def to_xhtml(self, parent=None, link_prefix=''):
if parent is None:
parent = etree.Element(XHTML('ul'))
elif len(self.nodes):
parent = element(parent, (XHTML('ul')))
for node in self.nodes:
point = element(parent, XHTML('li'))
href = link_prefix+urlunquote(node.href)
link = element(point, XHTML('a'), href=href)
title = node.title
if title:
title = re.sub(r'\s+', ' ', title)
link.text=title
node.to_xhtml(point, link_prefix=link_prefix)
return parent
def rationalize_play_orders(self): def rationalize_play_orders(self):
''' '''
Ensure that all nodes with the same play_order have the same href and Ensure that all nodes with the same play_order have the same href and
@ -1978,14 +1962,3 @@ class OEBBook(object):
spine.attrib['page-map'] = id spine.attrib['page-map'] = id
results[PAGE_MAP_MIME] = (href, self.pages.to_page_map()) results[PAGE_MAP_MIME] = (href, self.pages.to_page_map())
return results return results
def html_toc(self, link_prefix=''):
lang = unicode(self.metadata.language[0])
html = etree.Element(XHTML('html'),
attrib={XML('lang'): lang},
nsmap={None: XHTML_NS})
head = etree.SubElement(html, XHTML('head'))
title = etree.SubElement(head, XHTML('title'))
body = etree.SubElement(html, XHTML('body'))
body.append(self.toc.to_xhtml(link_prefix=link_prefix))
return html