From 0ce90a043eef532a71952e6aac43e6be6affc0f0 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Tue, 23 Nov 2010 22:13:32 +0100 Subject: Added import to EPUB function. --- epub/__init__.py | 1 + epub/epub.py | 354 +++++++++++++++++++++++++++++++++++++++++ epub/ez_epub.py | 38 +++++ epub/templates/container.xml | 6 + epub/templates/content.opf | 34 ++++ epub/templates/ez-section.html | 17 ++ epub/templates/image.html | 16 ++ epub/templates/title-page.html | 22 +++ epub/templates/toc.html | 32 ++++ epub/templates/toc.ncx | 28 ++++ 10 files changed, 548 insertions(+) create mode 100644 epub/__init__.py create mode 100644 epub/epub.py create mode 100644 epub/ez_epub.py create mode 100644 epub/templates/container.xml create mode 100644 epub/templates/content.opf create mode 100644 epub/templates/ez-section.html create mode 100644 epub/templates/image.html create mode 100644 epub/templates/title-page.html create mode 100644 epub/templates/toc.html create mode 100644 epub/templates/toc.ncx (limited to 'epub') diff --git a/epub/__init__.py b/epub/__init__.py new file mode 100644 index 00000000..8d1c8b69 --- /dev/null +++ b/epub/__init__.py @@ -0,0 +1 @@ + diff --git a/epub/epub.py b/epub/epub.py new file mode 100644 index 00000000..834acee6 --- /dev/null +++ b/epub/epub.py @@ -0,0 +1,354 @@ +#! /usr/local/bin/python +#-*- coding: utf-8 -*- + +import itertools +import mimetypes +import os +import shutil +import subprocess +import uuid +import zipfile +from genshi.template import TemplateLoader +from lxml import etree + +class TocMapNode: + + def __init__(self): + self.playOrder = 0 + self.title = '' + self.href = '' + self.children = [] + self.depth = 0 + + def assignPlayOrder(self): + nextPlayOrder = [0] + self.__assignPlayOrder(nextPlayOrder) + + def __assignPlayOrder(self, nextPlayOrder): + self.playOrder = nextPlayOrder[0] + nextPlayOrder[0] = self.playOrder + 1 + for child in self.children: + child.__assignPlayOrder(nextPlayOrder) + + +class EpubItem: + + def __init__(self): + self.id = '' + self.srcPath = '' + self.destPath = '' + self.mimeType = '' + self.html = '' + + +class EpubBook: + + def __init__(self): + self.loader = TemplateLoader('./epub/templates') + + self.rootDir = '' + self.UUID = uuid.uuid1() + + self.lang = 'en-US' + self.title = '' + self.creators = [] + self.metaInfo = [] + + self.imageItems = {} + self.htmlItems = {} + self.cssItems = {} + + self.coverImage = None + self.titlePage = None + self.tocPage = None + + self.spine = [] + self.guide = {} + self.tocMapRoot = TocMapNode() + self.lastNodeAtDepth = {0 : self.tocMapRoot} + + def setTitle(self, title): + self.title = title + + def setLang(self, lang): + self.lang = lang + + def addCreator(self, name, role = 'aut'): + self.creators.append((name, role)) + + def addMeta(self, metaName, metaValue, **metaAttrs): + self.metaInfo.append((metaName, metaValue, metaAttrs)) + + def getMetaTags(self): + l = [] + for metaName, metaValue, metaAttr in self.metaInfo: + beginTag = ' + +%s +

%s

+ +""" % (text, text) + + book = EpubBook() + book.setTitle('Most Wanted Tips for Aspiring Young Pirates') + book.addCreator('Monkey D Luffy') + book.addCreator('Guybrush Threepwood') + book.addMeta('contributor', 'Smalltalk80', role = 'bkp') + book.addMeta('date', '2010', event = 'publication') + + book.addTitlePage() + book.addTocPage() + book.addCover(r'D:\epub\blank.png') + + book.addCss(r'main.css', 'main.css') + + n1 = book.addHtml('', '1.html', getMinimalHtml('Chapter 1')) + n11 = book.addHtml('', '2.html', getMinimalHtml('Section 1.1')) + n111 = book.addHtml('', '3.html', getMinimalHtml('Subsection 1.1.1')) + n12 = book.addHtml('', '4.html', getMinimalHtml('Section 1.2')) + n2 = book.addHtml('', '5.html', getMinimalHtml('Chapter 2')) + + book.addSpineItem(n1) + book.addSpineItem(n11) + book.addSpineItem(n111) + book.addSpineItem(n12) + book.addSpineItem(n2) + + # You can use both forms to add TOC map + #t1 = book.addTocMapNode(n1.destPath, '1') + #t11 = book.addTocMapNode(n11.destPath, '1.1', parent = t1) + #t111 = book.addTocMapNode(n111.destPath, '1.1.1', parent = t11) + #t12 = book.addTocMapNode(n12.destPath, '1.2', parent = t1) + #t2 = book.addTocMapNode(n2.destPath, '2') + + book.addTocMapNode(n1.destPath, '1') + book.addTocMapNode(n11.destPath, '1.1', 2) + book.addTocMapNode(n111.destPath, '1.1.1', 3) + book.addTocMapNode(n12.destPath, '1.2', 2) + book.addTocMapNode(n2.destPath, '2') + + rootDir = r'd:\epub\test' + book.createBook(rootDir) + EpubBook.createArchive(rootDir, rootDir + '.epub') + #EpubBook.checkEpub('epubcheck-1.0.5.jar', rootDir + '.epub') + +if __name__ == '__main__': + test() \ No newline at end of file diff --git a/epub/ez_epub.py b/epub/ez_epub.py new file mode 100644 index 00000000..afd2dbff --- /dev/null +++ b/epub/ez_epub.py @@ -0,0 +1,38 @@ +#! /usr/local/bin/python +#-*- coding: utf-8 -*- + +import epub +from genshi.template import TemplateLoader + +class Section: + + def __init__(self): + self.title = '' + self.paragraphs = [] + self.tocDepth = 1 + +def makeBook(title, authors, sections, outputDir, lang='en-US', cover=None): + book = epub.EpubBook() + book.setLang(lang) + book.setTitle(title) + for author in authors: + book.addCreator(author) + #book.addTitlePage() + #book.addTocPage() + #if cover: + #book.addCover(cover) + + loader = TemplateLoader('./epub/templates') + tmpl = loader.load('ez-section.html') + + for i, section in enumerate(sections): + stream = tmpl.generate(section = section) + html = stream.render('xhtml', doctype='xhtml11', drop_xml_decl=False) + item = book.addHtml('', 's%d.html' % (i + 1), html) + book.addSpineItem(item) + book.addTocMapNode(item.destPath, section.title, section.tocDepth) + + outputFile = outputDir + 'article.epub' + book.createBook(outputDir) + book.createArchive(outputDir, outputFile) + #book.checkEpub('epubcheck-1.0.5.jar', outputFile) \ No newline at end of file diff --git a/epub/templates/container.xml b/epub/templates/container.xml new file mode 100644 index 00000000..eecf7a0d --- /dev/null +++ b/epub/templates/container.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/epub/templates/content.opf b/epub/templates/content.opf new file mode 100644 index 00000000..67f3f5c6 --- /dev/null +++ b/epub/templates/content.opf @@ -0,0 +1,34 @@ + + + + urn:uuid:${book.UUID} + ${book.lang} + ${book.title} + + $name + + + ${Markup(beginTag)}$content${Markup(endTag)} + + + + + + + + + + + + + + + + + + + + diff --git a/epub/templates/ez-section.html b/epub/templates/ez-section.html new file mode 100644 index 00000000..0a715e7f --- /dev/null +++ b/epub/templates/ez-section.html @@ -0,0 +1,17 @@ + + + ${section.title} + + + +

${section.title}

+ +

$p

+
+ + diff --git a/epub/templates/image.html b/epub/templates/image.html new file mode 100644 index 00000000..9a838c7e --- /dev/null +++ b/epub/templates/image.html @@ -0,0 +1,16 @@ + + + ${item.destPath} + + + +
${item.destPath}
+ + diff --git a/epub/templates/title-page.html b/epub/templates/title-page.html new file mode 100644 index 00000000..de0f55f0 --- /dev/null +++ b/epub/templates/title-page.html @@ -0,0 +1,22 @@ + + + ${book.title} + + + +

${book.title}

+

+ + $creator + +

+ + diff --git a/epub/templates/toc.html b/epub/templates/toc.html new file mode 100644 index 00000000..b14c9da3 --- /dev/null +++ b/epub/templates/toc.html @@ -0,0 +1,32 @@ + + + ${book.title} + + + + + + + ${tocEntry(child)} + + + + ${tocEntry(child)} + + + diff --git a/epub/templates/toc.ncx b/epub/templates/toc.ncx new file mode 100644 index 00000000..e7dd391a --- /dev/null +++ b/epub/templates/toc.ncx @@ -0,0 +1,28 @@ + + + + + + + + + + ${book.title} + + + + + ${node.title} + + + ${navPoint(child)} + + + + + ${navPoint(child)} + + + -- cgit