From e6472738b5253aa328f8b2a4f4f2a23abc8582c2 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Sun, 15 Apr 2012 18:59:50 +0200 Subject: Reorganization of folders. --- source/epub/epub.py | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 source/epub/epub.py (limited to 'source/epub/epub.py') diff --git a/source/epub/epub.py b/source/epub/epub.py new file mode 100644 index 00000000..2c01b54a --- /dev/null +++ b/source/epub/epub.py @@ -0,0 +1,343 @@ +#! /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') + +if __name__ == '__main__': + test() \ No newline at end of file -- cgit