This commit is contained in:
Kovid Goyal
2011-07-28 14:08:24 -06:00
parent c3a8776229
commit ea876907e9
3 changed files with 32 additions and 6 deletions
+28 -4
View File
@@ -7,7 +7,9 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os, subprocess, tempfile
import os, subprocess, tempfile, shutil
from lxml import etree
from calibre.constants import iswindows
from calibre.customize.ui import plugin_for_output_format
@@ -18,11 +20,24 @@ from calibre import CurrentDir
exe = 'kindlegen.exe' if iswindows else 'kindlegen'
def refactor_opf(opf, is_periodical):
pass
with open(opf, 'rb') as f:
root = etree.fromstring(f.read())
if is_periodical:
metadata = root.xpath('//*[local-name() = "metadata"]')[0]
xm = etree.SubElement(metadata, 'x-metadata')
xm.tail = '\n'
xm.text = '\n\t'
mobip = etree.SubElement(xm, attrib={'encoding':"utf-8",
'content-type':"application/x-mobipocket-subscription-magazine"})
mobip.tail = '\n'
with open(opf, 'wb') as f:
f.write(etree.tostring(root, method='xml', encodin='utf-8',
xml_declaration=True))
def refactor_guide(oeb):
for key in list(oeb.guide):
if key not in ('toc', 'start'):
if key not in ('toc', 'start', 'masthead'):
oeb.guide.remove(key)
def run_kindlegen(opf, log):
@@ -50,5 +65,14 @@ def kindlegen(oeb, opts, input_plugin, output_path):
opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0]
refactor_opf(opf, is_periodical)
with CurrentDir(tdir):
run_kindlegen(opf, oeb.log)
oname = run_kindlegen(opf, oeb.log)
shutil.copyfile(oname, output_path)
try:
if os.path.exists('/tmp/kindlegen'):
shutil.rmtree('/tmp/kindlegen')
shutil.copytree(tdir, '/tmp/kindlegen')
oeb.log('kindlegen intermediate output stored in: /tmp/kindlegen')
except:
pass