1
# -*- coding: utf-8 -*-
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
5
__docformat__ = 'restructuredtext en'
11
from calibre.customize.conversion import InputFormatPlugin
12
from calibre.ptempfile import TemporaryDirectory
13
from calibre.utils.zipfile import ZipFile
14
from calibre.ebooks.pml.pmlconverter import pml_to_html
15
from calibre.ebooks.metadata.opf2 import OPFCreator
17
class PMLInput(InputFormatPlugin):
20
author = 'John Schember'
21
description = 'Convert PML to OEB'
22
# pmlz is a zip file containing pml files and png images.
23
file_types = set(['pml', 'pmlz'])
25
def process_pml(self, pml_path, html_path):
29
if not hasattr(pml_path, 'read'):
30
pml_stream = open(pml_path, 'rb')
35
if not hasattr(html_path, 'write'):
36
html_stream = open(html_path, 'wb')
39
html_stream = html_path
41
ienc = pml_stream.encoding if pml_stream.encoding else 'utf-8'
42
if self.options.input_encoding:
43
ienc = self.options.input_encoding
45
self.log.debug('Converting PML to HTML...')
46
html = pml_to_html(pml_stream.read().decode(ienc))
47
html_stream.write('<html><head><title /></head><body>' + html.encode('utf-8', 'replace') + '</body></html>')
54
def convert(self, stream, options, file_ext, log,
56
self.options = options
58
pages, images = [], []
60
if file_ext == 'pmlz':
61
log.debug('De-compressing content to temporary directory...')
62
with TemporaryDirectory('_unpmlz') as tdir:
66
pmls = glob.glob(os.path.join(tdir, '*.pml'))
68
html_name = os.path.splitext(os.path.basename(pml))[0]+'.html'
69
html_path = os.path.join(os.getcwd(), html_name)
71
pages.append(html_name)
72
log.debug('Processing PML item %s...' % pml)
73
self.process_pml(pml, html_path)
75
imgs = glob.glob(os.path.join(tdir, '*.png'))
77
os.makedirs(os.path.join(os.getcwd(), 'images'))
79
pimg_name = os.path.basename(img)
80
pimg_path = os.path.join(os.getcwd(), 'images', pimg_name)
82
images.append(pimg_name)
84
shutil.move(img, pimg_path)
86
self.process_pml(stream, 'index.html')
88
pages.append('index.html')
91
# We want pages to be orded alphabetically.
95
for item in pages+images:
96
manifest_items.append((item, None))
98
from calibre.ebooks.metadata.meta import get_metadata
99
log.debug('Reading metadata from input file...')
100
mi = get_metadata(stream, 'pml')
101
opf = OPFCreator(os.getcwd(), mi)
102
log.debug('Generating manifest...')
103
opf.create_manifest(manifest_items)
104
opf.create_spine(pages)
105
with open('metadata.opf', 'wb') as opffile:
108
return os.path.join(os.getcwd(), 'metadata.opf')