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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unbenanntes Dokument</title>
<title></title>
</head>
<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
from os.path import dirname, abspath, relpath, exists, basename
from lxml import etree
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.customize.conversion import OptionRecommendation
from urllib import unquote
@ -22,26 +24,49 @@ class HTMLOutput(OutputFormatPlugin):
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):
self.log = log
self.opts = opts
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:
link_prefix=os.path.basename(output_path)+'/'
root = oeb_book.html_toc(link_prefix=link_prefix)
html_txt = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=False)
f.write(html_txt)
link_prefix=basename(output_dir)+'/'
root = self.generate_toc(oeb_book, output_dir, output_dir)
html_toc = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=False)
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):
os.makedirs(output_path)
with CurrentDir(output_path):
with CurrentDir(output_dir):
for item in oeb_book.manifest:
path = os.path.abspath(unquote(item.href))
dir = os.path.dirname(path)
if not os.path.exists(dir):
path = abspath(unquote(item.href))
dir = dirname(path)
if not exists(dir):
os.makedirs(dir)
if item.spine_position is not None:
with open(path, 'wb') as f:
@ -52,32 +77,24 @@ class HTMLOutput(OutputFormatPlugin):
item.unload_data_from_memory(memory=path)
for item in oeb_book.spine:
path = os.path.abspath(unquote(item.href))
dir = os.path.dirname(path)
path = abspath(unquote(item.href))
dir = dirname(path)
root = item.data.getroottree()
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 = re.sub(r'\<\/?body.*\>', '', ebook_content)
if item.spine_position+1 < len(oeb_book.spine):
nextLink = oeb_book.spine[item.spine_position+1].href
nextLink = os.path.abspath((nextLink))
nextLink = os.path.relpath(nextLink, dir)
nextLink = oeb_book.spine[item.spine_position+1].href
nextLink = relpath(abspath(nextLink), dir)
else:
nextLink = None
nextLink = None
if item.spine_position > 0:
prevLink = oeb_book.spine[item.spine_position-1].href
prevLink = os.path.abspath((prevLink))
prevLink = os.path.relpath(prevLink, dir)
prevLink = oeb_book.spine[item.spine_position-1].href
prevLink = relpath(abspath(prevLink), dir)
else:
prevLink = None
vars = {
}
template_file = os.path.dirname(__file__)+'/outputtemplates/default.tmpl'
templite = Templite(open(template_file).read())
prevLink = None
templite = Templite(P('templates/html_export_default.tmpl', data=True))
t = templite.render(ebookContent=ebook_content, prevLink=prevLink, nextLink=nextLink)
with open(path, 'wb') as f:
f.write(t)
item.unload_data_from_memory(memory=path)

View File

@ -1644,22 +1644,6 @@ class TOC(object):
node.to_ncx(point)
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):
'''
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
results[PAGE_MAP_MIME] = (href, self.pages.to_page_map())
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