diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index fe1f987dc9..6b1f33818a 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -207,6 +207,10 @@ class PRS505(CLI, Device): self.report_progress((i+1) / float(len(paths)), _('Removing books from device...')) if os.path.exists(path): os.unlink(path) + try: + os.removedirs(os.path.dirname(path)) + except: + pass self.report_progress(1.0, _('Removing books from device...')) @classmethod diff --git a/src/calibre/ebooks/lit/writer.py b/src/calibre/ebooks/lit/writer.py index 1e72ab2e4a..943fa6db62 100644 --- a/src/calibre/ebooks/lit/writer.py +++ b/src/calibre/ebooks/lit/writer.py @@ -308,6 +308,31 @@ class LitWriter(object): else: self._logger.warn('No suitable cover image found.') + # Remove comments because they are not supported by LIT HTML + for item in oeb.spine: + for elem in item.data.getiterator(): + if isinstance(elem, etree._Comment): + tail = elem.tail + parent = elem.getparent() + index = parent.index(elem) + text = u'' + if index == 0: + if parent.text: + text += parent.text + if tail: + text += tail + parent.text = text + else: + prev = parent[index-1] + text = u'' + if prev.tail: + text += prev.tail + if tail: + text += tail + prev.tail = text + parent.remove(elem) + + def __call__(self, oeb, path): if hasattr(path, 'write'): return self._dump_stream(oeb, path) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index a3dadb995d..276f315873 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -868,33 +868,18 @@ class Manifest(object): def _parse_txt(self, data): if '' in data: return self._parse_xhtml(data) - from xml.sax.saxutils import escape - self.oeb.log.debug('Converting', self.href, '...') - paras = [] - lines = [] - for l in data.splitlines(): - if not l: - if lines: - paras.append('

'+'\n'.join(lines)+'

') - lines = [] - lines.append(escape(l)) - if lines: - paras.append('

'+'\n'.join(lines)+'

') + self.oeb.log.debug('Converting', self.href, '...') + + from calibre.ebooks.txt.processor import txt_to_markdown + title = self.oeb.metadata.title if title: title = unicode(title[0]) else: - title = 'No title' - data = '''\ - - %s - %s - - '''%(title, '\n'.join(paras)) - data = self._parse_xhtml(data) - print etree.tostring(data) - return data + title = _('Unknown') + + return self._parse_xhtml(txt_to_markdown(data, title)) def _parse_css(self, data): diff --git a/src/calibre/ebooks/txt/processor.py b/src/calibre/ebooks/txt/processor.py index c8f2690622..ac0d4cac28 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -13,12 +13,13 @@ __license__ = 'GPL v3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' -def txt_to_markdown(txt): +def txt_to_markdown(txt, title=''): md = markdown.Markdown( extensions=['footnotes', 'tables', 'toc'], safe_mode=False,) - html = '</head><body>'+md.convert(txt)+'</body></html>' - + html = u'<html><head><title>%s%s' % (title, + md.convert(txt)) + return html def opf_writer(path, opf_name, manifest, spine, mi): diff --git a/src/calibre/gui2/convert/toc.py b/src/calibre/gui2/convert/toc.py index d86c8333c9..8dc0b9e89c 100644 --- a/src/calibre/gui2/convert/toc.py +++ b/src/calibre/gui2/convert/toc.py @@ -18,7 +18,7 @@ class TOCWidget(Widget, Ui_Form): HELP = _('Control the creation/conversion of the Table of Contents.') def __init__(self, parent, get_option, get_help, db=None, book_id=None): - Widget.__init__(self, parent, 'structure_detection', + Widget.__init__(self, parent, 'toc', ['level1_toc', 'level2_toc', 'level3_toc', 'toc_threshold', 'max_toc_links', 'no_chapters_in_toc', 'use_auto_toc', 'toc_filter', diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 517f23fa72..3bde907f95 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -19,7 +19,7 @@ from calibre.utils.ipc.job import BaseJob from calibre.devices.scanner import DeviceScanner from calibre.gui2 import config, error_dialog, Dispatcher, dynamic, \ pixmap_to_data, warning_dialog, \ - info_dialog + question_dialog from calibre.ebooks.metadata import authors_to_string from calibre import sanitize_file_name, preferred_encoding from calibre.utils.filenames import ascii_filename @@ -566,26 +566,27 @@ class DeviceGUI(object): else: bad.append(self.library_view.model().db.title(id, index_is_id=True)) else: - if specific_format in available_output_formats(): + if specific_format in list(set(fmts).intersection(set(available_output_formats()))): auto.append(id) else: bad.append(self.library_view.model().db.title(id, index_is_id=True)) if auto != []: - format = None - for fmt in fmts: - if fmt in list(set(fmts).intersection(set(available_output_formats()))): - format = fmt - break + format = specific_format if specific_format in list(set(fmts).intersection(set(available_output_formats()))) else None + if not format: + for fmt in fmts: + if fmt in list(set(fmts).intersection(set(available_output_formats()))): + format = fmt + break if format is None: bad += auto else: autos = [self.library_view.model().db.title(id, index_is_id=True) for id in auto] autos = '\n'.join('%s'%i for i in autos) - info_dialog(self, _('No suitable formats'), - _('Auto converting the following books before sending via ' - 'email:'), det_msg=autos, show=True) - self.auto_convert_mail(to, fmts, delete_from_library, auto, format) + if question_dialog(self, _('No suitable formats'), + _('Auto convert the following books before sending via ' + 'email?'), det_msg=autos): + self.auto_convert_mail(to, fmts, delete_from_library, auto, format) if bad: bad = '\n'.join('%s'%(i,) for i in bad) @@ -680,10 +681,10 @@ class DeviceGUI(object): if format is not None: autos = [self.library_view.model().db.title(id, index_is_id=True) for id in auto] autos = '\n'.join('%s'%i for i in autos) - info_dialog(self, _('No suitable formats'), - _('Auto converting the following books before uploading to ' - 'the device:'), det_msg=autos, show=True) - self.auto_convert_news(auto, format) + if question_dialog(self, _('No suitable formats'), + _('Auto convert the following books before uploading to ' + 'the device?'), det_msg=autos): + self.auto_convert_news(auto, format) files = [f for f in files if f is not None] if not files: dynamic.set('news_to_be_synced', set([])) @@ -776,26 +777,27 @@ class DeviceGUI(object): else: bad.append(self.library_view.model().db.title(id, index_is_id=True)) else: - if specific_format in available_output_formats(): + if specific_format in list(set(self.device_manager.device_class.settings().format_map).intersection(set(available_output_formats()))): auto.append(id) else: bad.append(self.library_view.model().db.title(id, index_is_id=True)) if auto != []: - format = None - for fmt in self.device_manager.device_class.settings().format_map: - if fmt in list(set(self.device_manager.device_class.settings().format_map).intersection(set(available_output_formats()))): - format = fmt - break - if format is None: + format = specific_format if specific_format in list(set(self.device_manager.device_class.settings().format_map).intersection(set(available_output_formats()))) else None + if not format: + for fmt in self.device_manager.device_class.settings().format_map: + if fmt in list(set(self.device_manager.device_class.settings().format_map).intersection(set(available_output_formats()))): + format = fmt + break + if not format: bad += auto else: autos = [self.library_view.model().db.title(id, index_is_id=True) for id in auto] autos = '\n'.join('%s'%i for i in autos) - info_dialog(self, _('No suitable formats'), - _('Auto converting the following books before uploading to ' - 'the device:'), det_msg=autos, show=True) - self.auto_convert(auto, on_card, format) + if question_dialog(self, _('No suitable formats'), + _('Auto convert the following books before uploading to ' + 'the device?'), det_msg=autos): + self.auto_convert(auto, on_card, format) if bad: bad = '\n'.join('%s'%(i,) for i in bad) diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index f641e6f64e..353f4d9c37 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -1186,7 +1186,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): try: if job.failed: return self.job_exception(job) - data = open(temp_files[0].name, 'rb') + data = open(temp_files[-1].name, 'rb') self.library_view.model().db.add_format(book_id, fmt, data, index_is_id=True) data.close() self.status_bar.showMessage(job.description + (' completed'), 2000) @@ -1210,7 +1210,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): if job.failed: self.job_exception(job) return - data = open(temp_files[0].name, 'rb') + data = open(temp_files[-1].name, 'rb') self.library_view.model().db.add_format(book_id, fmt, data, index_is_id=True) data.close() self.status_bar.showMessage(job.description + (' completed'), 2000) @@ -1233,7 +1233,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): try: if job.failed: return self.job_exception(job) - data = open(temp_files[0].name, 'rb') + data = open(temp_files[-1].name, 'rb') self.library_view.model().db.add_format(book_id, fmt, data, index_is_id=True) data.close() self.status_bar.showMessage(job.description + (' completed'), 2000)