aboutsummaryrefslogtreecommitdiff
path: root/epub/ez_epub.py
blob: ecfd4f5a1cbea6cbc9d7991552a8c7269edf0a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /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)
bgstack15