From 58bca5f56834e7fdffed7c1e9609ced58affa941 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Aug 2011 09:53:14 -0600 Subject: [PATCH 1/2] MOBI Output: Add a command line option --extract-to that uses the inspect MOBI tool to extract the created MOBI file to the specified directory --- src/calibre/ebooks/mobi/debug.py | 7 ++++--- src/calibre/ebooks/mobi/output.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/mobi/debug.py b/src/calibre/ebooks/mobi/debug.py index 8034117f9b..47450842d1 100644 --- a/src/calibre/ebooks/mobi/debug.py +++ b/src/calibre/ebooks/mobi/debug.py @@ -1430,16 +1430,17 @@ class MOBIFile(object): # {{{ print (str(self.mobi_header).encode('utf-8'), file=f) # }}} -def inspect_mobi(path_or_stream, prefix='decompiled'): # {{{ +def inspect_mobi(path_or_stream, ddir=None): # {{{ stream = (path_or_stream if hasattr(path_or_stream, 'read') else open(path_or_stream, 'rb')) f = MOBIFile(stream) - ddir = prefix + '_' + os.path.splitext(os.path.basename(stream.name))[0] + if ddir is None: + ddir = 'decompiled_' + os.path.splitext(os.path.basename(stream.name))[0] try: shutil.rmtree(ddir) except: pass - os.mkdir(ddir) + os.makedirs(ddir) with open(os.path.join(ddir, 'header.txt'), 'wb') as out: f.print_header(f=out) diff --git a/src/calibre/ebooks/mobi/output.py b/src/calibre/ebooks/mobi/output.py index aeab8518a9..4451bc66b8 100644 --- a/src/calibre/ebooks/mobi/output.py +++ b/src/calibre/ebooks/mobi/output.py @@ -50,6 +50,11 @@ 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='mobi_navpoints_only_deepest', recommended_value=False, help=_('When adding navpoints for the chapter-to-chapter' @@ -185,3 +190,8 @@ class MOBIOutput(OutputFormatPlugin): write_page_breaks_after_item=write_page_breaks_after_item) writer(oeb, output_path) + if opts.extract_to is not None: + from calibre.ebooks.mobi.debug import inspect_mobi + ddir = opts.extract_to + inspect_mobi(output_path, ddir=ddir) + From e20e8dd0f57093eea0001f40f0be7c7cc5ca2699 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Aug 2011 10:08:46 -0600 Subject: [PATCH 2/2] MOBI Output: Fix bug that caused paragraphs that had only a non breaking space as text before the first child element to be removed. Fixes #819058 (EPUB->MOBI conversion omitting some blockquotes) --- src/calibre/ebooks/mobi/mobiml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py index d108742f3c..eefa9d9e03 100644 --- a/src/calibre/ebooks/mobi/mobiml.py +++ b/src/calibre/ebooks/mobi/mobiml.py @@ -532,7 +532,7 @@ class MobiMLizer(object): bstate.pbreak = True if isblock: para = bstate.para - if para is not None and para.text == u'\xa0': + if para is not None and para.text == u'\xa0' and len(para) < 1: para.getparent().replace(para, etree.Element(XHTML('br'))) bstate.para = None bstate.istate = None