From c6f13474c65baff0dd5430e32306b4ee61fa4922 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 6 Aug 2009 16:19:05 -0600 Subject: [PATCH] TXT Input: Fix regression that broke inclusion of images. Fixes #3067 (OS X - Problem including image in .mobi output) --- src/calibre/ebooks/epub/output.py | 6 +++--- src/calibre/ebooks/txt/input.py | 23 +++++++++++++++++++++++ src/calibre/ebooks/txt/processor.py | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/epub/output.py b/src/calibre/ebooks/epub/output.py index 2676e664ee..24b14ea1b9 100644 --- a/src/calibre/ebooks/epub/output.py +++ b/src/calibre/ebooks/epub/output.py @@ -189,8 +189,8 @@ class EPUBOutput(OutputFormatPlugin): Create a generic cover for books that dont have a cover ''' try: - from calibre.gui2 import images_rc # Needed for access to logo - from PyQt4.Qt import QApplication, QFile, QIODevice + from calibre.gui2 import images_rc, is_ok_to_use_qt # Needed for access to logo + from PyQt4.Qt import QFile, QIODevice except: return None from calibre.ebooks.metadata import authors_to_string @@ -199,7 +199,7 @@ class EPUBOutput(OutputFormatPlugin): title = unicode(m.title[0]) a = [unicode(x) for x in m.creator if x.role == 'aut'] author = authors_to_string(a) - if QApplication.instance() is None: QApplication([]) + if not is_ok_to_use_qt(): return f = QFile(':/library') f.open(QIODevice.ReadOnly) img_data = str(f.readAll()) diff --git a/src/calibre/ebooks/txt/input.py b/src/calibre/ebooks/txt/input.py index 56a2dadd5c..a4b2cd48d9 100644 --- a/src/calibre/ebooks/txt/input.py +++ b/src/calibre/ebooks/txt/input.py @@ -31,12 +31,35 @@ class TXTInput(InputFormatPlugin): raise ValueError('This txt file has malformed markup, it cannot be' 'converted by calibre. See http://daringfireball.net/projects/markdown/syntax') + from calibre.customize.ui import plugin_for_input_format + html_input = plugin_for_input_format('html') + for opt in html_input.options: + setattr(options, opt.option.name, opt.recommended_value) + base = os.getcwdu() + if hasattr(stream, 'name'): + base = os.path.dirname(stream.name) + htmlfile = open(os.path.join(base, 'temp_calibre_txt_input_to_html.html'), + 'wb') + htmlfile.write(html.encode('utf-8')) + htmlfile.close() + cwd = os.getcwdu() + odi = options.debug_input + options.debug_input = None + oeb = html_input(open(htmlfile.name, 'rb'), options, 'html', log, + {}, cwd) + options.debug_input = odi + os.remove(htmlfile.name) + return oeb + + + log.debug('Writing html output...') with open('index.html', 'wb') as index: index.write(html.encode('utf-8')) from calibre.ebooks.metadata.meta import get_metadata log.debug('Retrieving source document metadata...') + stream.seek(0) mi = get_metadata(stream, 'txt') manifest = [('index.html', None)] spine = ['index.html'] diff --git a/src/calibre/ebooks/txt/processor.py b/src/calibre/ebooks/txt/processor.py index ac0d4cac28..ddb9b6a121 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -17,7 +17,7 @@ def txt_to_markdown(txt, title=''): md = markdown.Markdown( extensions=['footnotes', 'tables', 'toc'], safe_mode=False,) - html = u'%s%s' % (title, + html = u'%s%s' % (title, md.convert(txt)) return html