[Python-modules-commits] [python-odf] 79/118: Can now generate TOC.NCX from the document's table of content index
Wolfgang Borgert
debacle at moszumanska.debian.org
Fri Oct 3 21:27:25 UTC 2014
This is an automated email from the git hooks/post-receive script.
debacle pushed a commit to reference refs/remotes/upstream/master
in repository python-odf.
commit ca64f4d769c782fbc27571aa2e7bfe9b343107eb
Author: Søren Roug <soren.roug at eea.europa.eu>
Date: Tue Mar 23 20:49:24 2010 +0000
Can now generate TOC.NCX from the document's table of content index
---
contrib/odf2epub/odf2epub | 52 ++++++++++++++++++++++++++++++++++++++++++-----
odf/odf2xhtml.py | 17 +++++++++++++---
2 files changed, 61 insertions(+), 8 deletions(-)
diff --git a/contrib/odf2epub/odf2epub b/contrib/odf2epub/odf2epub
index b293abf..bffad6f 100755
--- a/contrib/odf2epub/odf2epub
+++ b/contrib/odf2epub/odf2epub
@@ -18,6 +18,7 @@
# Contributor(s):
#
from odf.odf2xhtml import ODF2XHTML
+from odf.namespaces import TEXTNS, XLINKNS
import sys, getopt, time, zipfile
from StringIO import StringIO
from cgi import escape
@@ -30,6 +31,34 @@ def escaped(string):
def usage():
sys.stderr.write("Usage: %s [-p] inputfile\n" % sys.argv[0])
+class NavpointEntry:
+ def __init__(self, anchor, title):
+ self.anchor = anchor
+ self.title = title
+
+class ODF2EPUB(ODF2XHTML):
+
+ in_toc = False
+ navpoint_list = []
+
+ def __init__(self, generate_css=True, embedable=False):
+ ODF2XHTML.__init__(self, generate_css, embedable)
+ self.elements[(TEXTNS, "table-of-content")] = (self.s_text_table_of_content, self.e_text_table_of_content)
+
+ def s_text_table_of_content(self, tag, attrs):
+ self.in_toc = True
+
+ def e_text_table_of_content(self, tag, attrs):
+ self.in_toc = False
+
+ def s_text_a(self, tag, attrs):
+ if self.in_toc:
+ href = attrs[(XLINKNS,"href")].split("|")[0]
+ if href[0] == "#":
+ n = NavpointEntry("#" + self.get_anchor(href[1:]), href[1:])
+ self.navpoint_list.append(n)
+ return ODF2XHTML.s_text_a(self, tag, attrs)
+
class EPublication:
@@ -59,7 +88,7 @@ class EPublication:
</spine>
</package>"""
- toc_ncx = """<?xml version="1.0"?>
+ toc_ncx_head = """<?xml version="1.0"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
@@ -79,13 +108,13 @@ class EPublication:
<text>Start</text>
</navLabel>
<content src="book.xhtml"/>
- </navPoint>
- </navMap>
+ </navPoint>"""
+ toc_ncx_foot = """ </navMap>
</ncx>"""
def __init__(self, filename):
self.filename = filename
- self.odhandler = ODF2XHTML(True, False)
+ self.odhandler = ODF2EPUB(True, False)
self.odhandler.load(filename)
def _zipwrite(self, outputfp):
@@ -133,7 +162,17 @@ class EPublication:
zout.compress_type = zipfile.ZIP_DEFLATED
zout.external_attr = UNIXPERMS
opf = []
- opf.append(self.toc_ncx % (escaped(self.odhandler.title)))
+ opf.append(self.toc_ncx_head % (escaped(self.odhandler.title)))
+ np_inx = 2
+ for np in self.odhandler.navpoint_list:
+ opf.append(""" <navPoint id="navPoint-%d" playOrder="%d">
+ <navLabel>
+ <text>%s</text>
+ </navLabel>
+ <content src="book.xhtml%s"/>
+ </navPoint>""" % (np_inx, np_inx, escaped(np.title), np.anchor))
+ np_inx += 1
+ opf.append(self.toc_ncx_foot)
outputfp.writestr(zout, '\n'.join(opf))
# zout = zipfile.ZipInfo('OEBPS/styles.css', now)
@@ -171,6 +210,9 @@ if len(args) != 1:
usage()
sys.exit(2)
+epub = EPublication(args[0])
+epub.save(outputfilename)
+sys.exit(0)
try:
epub = EPublication(args[0])
epub.save(outputfilename)
diff --git a/odf/odf2xhtml.py b/odf/odf2xhtml.py
index 9ae4453..8cf1fea 100644
--- a/odf/odf2xhtml.py
+++ b/odf/odf2xhtml.py
@@ -338,7 +338,7 @@ class ODF2XHTML(handler.ContentHandler):
self.elements = {
(DCNS, 'title'): (self.s_processcont, self.e_dc_title),
(DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage),
- (DCNS, 'creator'): (self.s_processcont, self.e_dc_metatag),
+ (DCNS, 'creator'): (self.s_processcont, self.e_dc_creator),
(DCNS, 'description'): (self.s_processcont, self.e_dc_metatag),
(DCNS, 'date'): (self.s_processcont, self.e_dc_metatag),
(DRAWNS, 'custom-shape'): (self.s_custom_shape, self.e_custom_shape),
@@ -517,6 +517,7 @@ class ODF2XHTML(handler.ContentHandler):
return c
def get_anchor(self, name):
+ """ Create a unique anchor id for a href name """
if not self.anchors.has_key(name):
self.anchors[name] = "anchor%03d" % (len(self.anchors) + 1)
return self.anchors.get(name)
@@ -535,8 +536,8 @@ class ODF2XHTML(handler.ContentHandler):
def e_dc_title(self, tag, attrs):
""" Get the title from the meta data and create a HTML <title>
"""
- self.metatags.append('<title>%s</title>\n' % escape(''.join(self.data)))
self.title = ''.join(self.data)
+ self.metatags.append('<title>%s</title>\n' % escape(self.title))
self.data = []
def e_dc_metatag(self, tag, attrs):
@@ -548,7 +549,15 @@ class ODF2XHTML(handler.ContentHandler):
def e_dc_contentlanguage(self, tag, attrs):
""" Set the content language. Identifies the targeted audience
"""
- self.metatags.append('<meta http-equiv="content-language" content="%s"/>\n' % ''.join(self.data))
+ self.language = ''.join(self.data)
+ self.metatags.append('<meta http-equiv="content-language" content="%s"/>\n' % escape(self.language))
+ self.data = []
+
+ def e_dc_creator(self, tag, attrs):
+ """ Set the content creator. Identifies the targeted audience
+ """
+ self.creator = ''.join(self.data)
+ self.metatags.append('<meta http-equiv="creator" content="%s"/>\n' % escape(self.creator))
self.data = []
def s_custom_shape(self, tag, attrs):
@@ -1278,6 +1287,8 @@ class ODF2XHTML(handler.ContentHandler):
def parseodf(self):
self.xmlfile = ''
self.title = ''
+ self.language = ''
+ self.creator = ''
self.data = []
self.tagstack = TagStack()
self.pstack = []
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-odf.git
More information about the Python-modules-commits
mailing list