diff --git a/src/calibre/ebooks/html/outputtemplates/default.tmpl b/resources/templates/html_export_default.tmpl
similarity index 92%
rename from src/calibre/ebooks/html/outputtemplates/default.tmpl
rename to resources/templates/html_export_default.tmpl
index 9673c74328..9c7f480fc8 100644
--- a/src/calibre/ebooks/html/outputtemplates/default.tmpl
+++ b/resources/templates/html_export_default.tmpl
@@ -2,7 +2,7 @@
-Unbenanntes Dokument
+
diff --git a/resources/templates/html_export_default_index.tmpl b/resources/templates/html_export_default_index.tmpl
new file mode 100644
index 0000000000..b1e0bfc70b
--- /dev/null
+++ b/resources/templates/html_export_default_index.tmpl
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+${toc}$
+
+
+
diff --git a/src/calibre/ebooks/html/output.py b/src/calibre/ebooks/html/output.py
index f25593dcee..7286cb02eb 100644
--- a/src/calibre/ebooks/html/output.py
+++ b/src/calibre/ebooks/html/output.py
@@ -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)
diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py
index 611eef5738..e85098e293 100644
--- a/src/calibre/ebooks/oeb/base.py
+++ b/src/calibre/ebooks/oeb/base.py
@@ -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