PML output plugin.

This commit is contained in:
John Schember 2009-05-02 10:19:01 -04:00
parent b2e749e00d
commit b0993c006f
4 changed files with 63 additions and 3 deletions

View File

@ -293,6 +293,7 @@ from calibre.ebooks.epub.output import EPUBOutput
from calibre.ebooks.txt.output import TXTOutput from calibre.ebooks.txt.output import TXTOutput
from calibre.ebooks.pdf.output import PDFOutput from calibre.ebooks.pdf.output import PDFOutput
from calibre.ebooks.pml.input import PMLInput from calibre.ebooks.pml.input import PMLInput
from calibre.ebooks.pml.output import PMLOutput
from calibre.customize.profiles import input_profiles, output_profiles from calibre.customize.profiles import input_profiles, output_profiles
from calibre.devices.prs500.driver import PRS500 from calibre.devices.prs500.driver import PRS500
@ -307,7 +308,8 @@ from calibre.devices.jetbook.driver import JETBOOK
plugins = [HTML2ZIP, EPUBInput, MOBIInput, PDBInput, PDFInput, HTMLInput, plugins = [HTML2ZIP, EPUBInput, MOBIInput, PDBInput, PDFInput, HTMLInput,
TXTInput, OEBOutput, TXTOutput, PDFOutput, LITInput, ComicInput, TXTInput, OEBOutput, TXTOutput, PDFOutput, LITInput, ComicInput,
FB2Input, ODTInput, RTFInput, EPUBOutput, RecipeInput, PMLInput] FB2Input, ODTInput, RTFInput, EPUBOutput, RecipeInput, PMLInput,
PMLOutput]
plugins += [PRS505, PRS700, CYBOOKG3, KINDLE, KINDLE2, BLACKBERRY, EB600, \ plugins += [PRS505, PRS700, CYBOOKG3, KINDLE, KINDLE2, BLACKBERRY, EB600, \
JETBOOK] JETBOOK]
plugins += [x for x in list(locals().values()) if isinstance(x, type) and \ plugins += [x for x in list(locals().values()) if isinstance(x, type) and \

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__license__ = 'GPL 3' __license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>' __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'

View File

@ -63,8 +63,10 @@ class PDFOutput(OutputFormatPlugin):
def convert_text(self, oeb_book): def convert_text(self, oeb_book):
with TemporaryDirectory('_pdf_out') as oebdir: with TemporaryDirectory('_pdf_out') as oebdir:
OEBOutput(None).convert(oeb_book, oebdir, self.input_plugin, self.opts, self.log) from calibre.customize.ui import plugin_for_output_format
oeb_output = plugin_for_output_format('oeb')
oeb_output.convert(oeb, oeb_dir, self.input_plugin, self.opts, self.log)
opfpath = glob.glob(os.path.join(oebdir, '*.opf'))[0] opfpath = glob.glob(os.path.join(oebdir, '*.opf'))[0]
opf = OPF(opfpath, os.path.dirname(opfpath)) opf = OPF(opfpath, os.path.dirname(opfpath))

View File

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
import os
import Image, cStringIO
from calibre.customize.conversion import OutputFormatPlugin
from calibre.ptempfile import TemporaryDirectory
from calibre.utils.zipfile import ZipFile
from calibre.ebooks.oeb.base import OEB_IMAGES
from calibre.ebooks.pml.pmlconverter import html_to_pml
class PMLOutput(OutputFormatPlugin):
name = 'PML Output'
author = 'John Schember'
file_type = 'pmlz'
def convert(self, oeb_book, output_path, input_plugin, opts, log):
with TemporaryDirectory('_pmlz_output') as tdir:
self.process_spine(oeb_book.spine, tdir)
self.write_images(oeb_book.manifest, tdir)
pmlz = ZipFile(output_path, 'w')
pmlz.add_dir(tdir)
def process_spine(self, spine, out_dir):
for item in spine:
html = html_to_pml(unicode(item)).encode('utf-8')
name = os.path.splitext(os.path.basename(item.href))[0] + '.pml'
path = os.path.join(out_dir, name)
with open(path, 'wb') as out:
out.write(html)
def write_images(self, manifest, out_dir):
for item in manifest:
if item.media_type in OEB_IMAGES:
im = Image.open(cStringIO.StringIO(item.data))
data = cStringIO.StringIO()
im.save(data, 'PNG')
data = data.getvalue()
name = os.path.splitext(os.path.basename(item.href))[0] + '.png'
path = os.path.join(out_dir, name)
with open(path, 'wb') as out:
out.write(data)