TXTZ Output support.

This commit is contained in:
John Schember 2011-02-06 15:46:07 -05:00
parent 0f86858c3b
commit c481be879d
3 changed files with 37 additions and 1 deletions

View File

@ -468,6 +468,7 @@ from calibre.ebooks.rb.output import RBOutput
from calibre.ebooks.rtf.output import RTFOutput
from calibre.ebooks.tcr.output import TCROutput
from calibre.ebooks.txt.output import TXTOutput
from calibre.ebooks.txt.output import TXTZOutput
from calibre.ebooks.html.output import HTMLOutput
from calibre.ebooks.snb.output import SNBOutput
@ -553,6 +554,7 @@ plugins += [
RTFOutput,
TCROutput,
TXTOutput,
TXTZOutput,
HTMLOutput,
SNBOutput,
]

View File

@ -34,6 +34,5 @@ def get_metadata(stream, extract_cover=True):
return mi
def set_metadata(stream, mi):
stream.seek(0)
opf = StringIO(metadata_to_opf(mi))
safe_replace(stream, 'metadata.opf', opf)

View File

@ -5,11 +5,17 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
import os
import shutil
from lxml import etree
from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation
from calibre.ebooks.oeb.base import OEB_IMAGES
from calibre.ebooks.txt.txtml import TXTMLizer
from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
from calibre.ptempfile import TemporaryDirectory, TemporaryFile
from calibre.utils.zipfile import ZipFile
class TXTOutput(OutputFormatPlugin):
@ -93,3 +99,32 @@ class TXTOutput(OutputFormatPlugin):
if close:
out_stream.close()
class TXTZOutput(TXTOutput):
name = 'TXTZ Output'
author = 'John Schember'
file_type = 'txtz'
def convert(self, oeb_book, output_path, input_plugin, opts, log):
with TemporaryDirectory('_txtz_output') as tdir:
# TXT
with TemporaryFile('index.txt') as tf:
TXTOutput.convert(self, oeb_book, tf, input_plugin, opts, log)
shutil.copy(tf, os.path.join(tdir, 'index.txt'))
# Images
for item in oeb_book.manifest:
if item.media_type in OEB_IMAGES:
path = os.path.join(tdir, os.path.dirname(item.href))
if not os.path.exists(path):
os.makedirs(path)
with open(os.path.join(tdir, item.href), 'wb') as imgf:
imgf.write(item.data)
# Metadata
with open(os.path.join(tdir, 'metadata.opf'), 'wb') as mdataf:
mdataf.write(etree.tostring(oeb_book.metadata.to_opf1()))
txtz = ZipFile(output_path, 'w')
txtz.add_dir(tdir)