Add an --extract-to option for DOCX output

Also re-use the help string for that option everywhere.
This commit is contained in:
Kovid Goyal 2015-03-11 09:08:13 +05:30
parent 4d88d05621
commit 8817629486
4 changed files with 27 additions and 15 deletions

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from calibre.customize.conversion import OutputFormatPlugin
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
class DOCXOutput(OutputFormatPlugin):
@ -14,10 +14,20 @@ class DOCXOutput(OutputFormatPlugin):
author = 'Kovid Goyal'
file_type = 'docx'
options = {
OptionRecommendation(name='extract_to',
help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first '
'deleted, so be careful.') % 'DOCX'),
}
def convert(self, oeb, output_path, input_plugin, opts, log):
from calibre.ebooks.docx.writer.container import DOCX
from calibre.ebooks.docx.writer.from_html import Convert
docx = DOCX(opts, log)
Convert(oeb, docx)()
docx.write(output_path)
if opts.extract_to:
from calibre.ebooks.docx.dump import do_dump
do_dump(output_path, opts.extract_to)

View File

@ -51,9 +51,9 @@ class EPUBOutput(OutputFormatPlugin):
options = set([
OptionRecommendation(name='extract_to',
help=_('Extract the contents of the generated EPUB file to the '
help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first '
'deleted, so be careful.')),
'deleted, so be careful.') % 'EPUB'),
OptionRecommendation(name='dont_split_on_page_breaks',
recommended_value=False, level=OptionRecommendation.LOW,

View File

@ -73,10 +73,10 @@ class MOBIOutput(OutputFormatPlugin):
help=_('When adding the Table of Contents to the book, add it at the start of the '
'book instead of the end. Not recommended.')
),
OptionRecommendation(name='extract_to', recommended_value=None,
help=_('Extract the contents of the MOBI file to the'
' specified directory. If the directory already '
'exists, it will be deleted.')
OptionRecommendation(name='extract_to',
help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first '
'deleted, so be careful.') % 'MOBI'
),
OptionRecommendation(name='share_not_sync', recommended_value=False,
help=_('Enable sharing of book content via Facebook etc. '
@ -286,11 +286,10 @@ class AZW3Output(OutputFormatPlugin):
help=_('When adding the Table of Contents to the book, add it at the start of the '
'book instead of the end. Not recommended.')
),
OptionRecommendation(name='extract_to', recommended_value=None,
help=_('Extract the contents of the MOBI file to the'
' specified directory. If the directory already '
'exists, it will be deleted.')
),
OptionRecommendation(name='extract_to',
help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first '
'deleted, so be careful.') % 'AZW3'),
OptionRecommendation(name='share_not_sync', recommended_value=False,
help=_('Enable sharing of book content via Facebook etc. '
' on the Kindle. WARNING: Using this feature means that '

View File

@ -24,15 +24,18 @@ def pretty_all_xml_in_dir(path):
stream.truncate()
stream.write(etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True))
def dump(path):
dest = os.path.splitext(os.path.basename(path))[0]
dest += '-dumped'
def do_dump(path, dest):
if os.path.exists(dest):
shutil.rmtree(dest)
with ZipFile(path) as zf:
zf.extractall(dest)
pretty_all_xml_in_dir(dest)
def dump(path):
dest = os.path.splitext(os.path.basename(path))[0]
dest += '-dumped'
do_dump(path, dest)
print (path, 'dumped to', dest)
if __name__ == '__main__':