[Python-modules-commits] [python-odf] 76/118: Can create simple EPUB books

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 5db45eba0024291f07993ce5edc0aaf185780c45
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Mon Mar 22 21:43:01 2010 +0000

    Can create simple EPUB books
---
 contrib/odf2epub/Makefile     |  15 ++++
 contrib/odf2epub/odf2epub     | 177 ++++++++++++++++++++++++++++++++++++++++++
 contrib/odf2epub/odf2epub.1   |  57 ++++++++++++++
 contrib/odf2epub/odf2epub.xml |  63 +++++++++++++++
 4 files changed, 312 insertions(+)

diff --git a/contrib/odf2epub/Makefile b/contrib/odf2epub/Makefile
new file mode 100644
index 0000000..c4d3f49
--- /dev/null
+++ b/contrib/odf2epub/Makefile
@@ -0,0 +1,15 @@
+all: odf odf2epub.1 
+
+txt: odf2epub.txt
+
+%.1: %.xml 
+	xmlto man $<
+
+%.txt: %.xml 
+	xmlto txt $<
+
+clean:
+	rm -f *.txt odf
+
+odf:
+	ln -s ../../odf
diff --git a/contrib/odf2epub/odf2epub b/contrib/odf2epub/odf2epub
new file mode 100755
index 0000000..f3bbe6b
--- /dev/null
+++ b/contrib/odf2epub/odf2epub
@@ -0,0 +1,177 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2010 Søren Roug, European Environment Agency
+#
+# This is free software.  You may redistribute it under the terms
+# of the Apache license and the GNU General Public License Version
+# 2 or at your option any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Contributor(s):
+#
+from odf.odf2xhtml import ODF2XHTML
+import sys, getopt, time, zipfile
+from StringIO import StringIO
+from cgi import escape
+
+UNIXPERMS = 0100644 << 16L  # -rw-r--r--
+
+def escaped(string):
+   return escape(string.encode('us-ascii','xmlcharrefreplace'))
+
+def usage():
+   sys.stderr.write("Usage: %s [-p] inputfile\n" % sys.argv[0])
+
+
+class EPublication:
+
+    mimetype = "application/epub+zip"
+    container = """<?xml version="1.0"?>
+<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
+    <rootfiles>
+        <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
+    </rootfiles>
+</container>"""
+
+    content_opf_head = """<?xml version="1.0"?>
+<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookID" version="2.0">
+    <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
+      <dc:title>%s</dc:title>
+      <dc:language>%s</dc:language>
+      <dc:identifier id="BookID" opf:scheme="URI">%s</dc:identifier>
+      <dc:creator>%s</dc:creator>
+    </metadata>
+    <manifest>
+        <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
+        <item id="book.xhtml" href="book.xhtml" media-type="application/xhtml+xml"/>
+"""
+    content_opf_foot = """</manifest>
+    <spine toc="ncx">
+        <itemref idref="book.xhtml"/>
+    </spine>
+</package>
+"""
+
+    toc_ncx = """<?xml version="1.0"?>
+<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
+   "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
+
+<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
+       <head>
+        <meta name="dtb:uid" content="d922af8e-452f-4fac-b372-a70c54fe218d"/>
+        <meta name="dtb:depth" content="0"/>
+        <meta name="dtb:totalPageCount" content="0"/>
+        <meta name="dtb:maxPageNumber" content="0"/>
+    </head>
+    <docTitle>
+        <text>%s</text>
+    </docTitle>
+    <navMap>
+        <navPoint id="navPoint-1" playOrder="1">
+            <navLabel>
+                <text>Start</text>
+            </navLabel>
+            <content src="book.xhtml"/>
+        </navPoint>
+    </navMap>
+</ncx>"""
+
+    def __init__(self, filename):
+        self.filename = filename
+        self.odhandler = ODF2XHTML(True, False)
+        self.odhandler.load(filename)
+
+    def _zipwrite(self, outputfp):
+        """ Write the document to an open file pointer """
+        now = time.localtime()[:6]
+
+        # Write mimetype
+        zout = zipfile.ZipInfo('mimetype', now)
+        zout.compress_type = zipfile.ZIP_STORED
+        zout.external_attr = UNIXPERMS
+        outputfp.writestr(zout, self.mimetype)
+
+        zout = zipfile.ZipInfo('META-INF/container.xml', now)
+        zout.compress_type = zipfile.ZIP_DEFLATED
+        zout.external_attr = UNIXPERMS
+        outputfp.writestr(zout, self.container)
+
+        zout = zipfile.ZipInfo('OEBPS/book.xhtml', now)
+        zout.compress_type = zipfile.ZIP_DEFLATED
+        zout.external_attr = UNIXPERMS
+        xhtml = self.odhandler.xhtml().encode('us-ascii','xmlcharrefreplace')
+        outputfp.writestr(zout, xhtml)
+
+        z = zipfile.ZipFile(self.filename)
+        for zinfo in z.infolist():
+            if zinfo.filename[0:9] == 'Pictures/':
+                zipinfo = zipfile.ZipInfo("OEBPS/" + zinfo.filename, now)
+                zipinfo.external_attr = UNIXPERMS
+                outputfp.writestr(zipinfo, z.read(zinfo.filename))
+
+        zout = zipfile.ZipInfo('OEBPS/content.opf', now)
+        zout.compress_type = zipfile.ZIP_DEFLATED
+        zout.external_attr = UNIXPERMS
+        opf = []
+        opf.append(self.content_opf_head % (escaped(self.odhandler.title), escaped(self.odhandler.language),
+                escaped(args[0]), escaped(self.odhandler.creator)))
+        for zname in z.namelist():
+            opf.append("""<item id="%s" href="%s" media-type="image/jpeg"/>""" % (zname, zname))
+        opf.append(self.content_opf_foot)
+        outputfp.writestr(zout, '\n'.join(opf))
+
+        z.close()
+
+        zout = zipfile.ZipInfo('OEBPS/toc.ncx', now)
+        zout.compress_type = zipfile.ZIP_DEFLATED
+        zout.external_attr = UNIXPERMS
+        opf = []
+        opf.append(self.toc_ncx % (escaped(self.odhandler.title)))
+        outputfp.writestr(zout, '\n'.join(opf))
+
+#        zout = zipfile.ZipInfo('OEBPS/styles.css', now)
+#        zout.compress_type = zipfile.ZIP_DEFLATED
+#        zout.external_attr = UNIXPERMS
+#        css = self.odhandler.css().encode('us-ascii','xmlcharrefreplace')
+#        outputfp.writestr(zout, css)
+
+    def save(self, outputfile):
+        """ Save the document under the filename """
+        if outputfile == '-':
+            outputfp = zipfile.ZipFile(sys.stdout,"w")
+        else:
+            outputfp = zipfile.ZipFile(outputfile, "w")
+        self._zipwrite(outputfp)
+        outputfp.close()
+
+try:
+    opts, args = getopt.getopt(sys.argv[1:], "e", ["plain"])
+except getopt.GetoptError:
+    usage()
+    sys.exit(2)
+
+generatecss = True
+embedable = False
+for o, a in opts:
+    if o in ("-p", "--plain"):
+        generatecss = False
+
+if len(args) != 1:
+    usage()
+    sys.exit(2)
+
+#try:
+epub = EPublication(args[0])
+epub.save("test.epub")
+#except:
+#    sys.stderr.write("Unable to open file %s or file is not OpenDocument\n" % sys.argv[1])
+#    sys.exit(1)
+
diff --git a/contrib/odf2epub/odf2epub.1 b/contrib/odf2epub/odf2epub.1
new file mode 100644
index 0000000..6cd9d53
--- /dev/null
+++ b/contrib/odf2epub/odf2epub.1
@@ -0,0 +1,57 @@
+'\" t
+.\"     Title: odf2epub
+.\"    Author: S\(/oren Roug
+.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
+.\"      Date: 03/22/2010
+.\"    Manual: User commands
+.\"    Source: odfpy
+.\"  Language: English
+.\"
+.TH "ODF2EPUB" "1" "03/22/2010" "odfpy" "User commands"
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+odf2epub \- Convert ODF to an ePub ebook
+.SH "SYNOPSIS"
+.HP \w'\fBodf2epub\fR\ 'u
+\fBodf2epub\fR [\-p] \fIpath\fR
+.SH "DESCRIPTION"
+.PP
+\fBodf2epub\fR
+is a program that will create an ebook (\&.epub) from the input file and will write the webpage to stdout\&.
+.PP
+"Path" is assumed to be an OpenDocument file of text, spreadsheet or presentation type\&.
+.SH "OPTIONS"
+.PP
+\-p, \-\-plain
+.RS 4
+The \-p flag will generate HTML without CSS\&.
+.RE
+.SH "EXAMPLE"
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+odf2epub odf\-file
+.fi
+.if n \{\
+.RE
+.\}
+.SH "SEE ALSO"
+.PP
+\fBodf2xhtml\fR(1)
+.SH "AUTHOR"
+.PP
+\fBS\(/oren Roug\fR
+.RS 4
+Original author
+.RE
diff --git a/contrib/odf2epub/odf2epub.xml b/contrib/odf2epub/odf2epub.xml
new file mode 100644
index 0000000..abe9e75
--- /dev/null
+++ b/contrib/odf2epub/odf2epub.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0"?>
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<refentry id="odf2epub">
+  <refentryinfo>
+    <productname>odfpy</productname>
+    <author><firstname>Søren</firstname><surname>Roug</surname>
+    <contrib>Original author</contrib>
+    </author>
+  </refentryinfo>
+  <refmeta>
+    <refentrytitle>odf2epub</refentrytitle>
+    <manvolnum>1</manvolnum>
+    <refmiscinfo class="manual">User commands</refmiscinfo>
+  </refmeta>
+  <refnamediv>
+    <refname>odf2epub</refname>
+    <refpurpose>Convert ODF to an ePub ebook</refpurpose>
+  </refnamediv>
+  <refsynopsisdiv>
+    <cmdsynopsis>
+      <command>odf2epub</command>
+      <arg choice="opt">-p </arg>
+      <arg choice="plain">
+        <replaceable>path</replaceable>
+      </arg>
+    </cmdsynopsis>
+  </refsynopsisdiv>
+  <refsect1>
+    <title>Description</title>
+    <para><command>odf2epub</command> is a program that will create 
+an ebook (.epub) from the input file and will write the webpage to stdout.
+</para>
+    <para>
+"Path" is assumed to be an
+OpenDocument file of text, spreadsheet or presentation type.
+</para>
+  </refsect1>
+
+  <refsect1>
+    <title>Options</title>
+    <variablelist>
+      <varlistentry>
+        <term>-p, --plain</term>
+        <listitem>
+          <para>
+The -p flag will generate HTML without CSS.
+</para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+  </refsect1>
+
+  <refsect1>
+    <title>Example</title>
+    <screen>
+odf2epub odf-file
+</screen>
+  </refsect1>
+  <refsect1>
+    <title>See Also</title>
+    <para><command>odf2xhtml</command>(1)</para>
+  </refsect1>
+</refentry>

-- 
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