#! /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()