From 30b8e1de8484b11638514fe64c791e37a52af5e9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 21 Nov 2019 09:16:21 +0530 Subject: [PATCH] Viewer: Fix opening .txt files in the standalone viewer leaving behind a temporary index.html file in the directory of the txt file. Fixes #1853232 [Viewing Text Files Creates an Index File](https://bugs.launchpad.net/calibre/+bug/1853232) --- .../ebooks/conversion/plugins/txt_input.py | 16 +++++++++------- src/calibre/srv/render_book.py | 6 ++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/txt_input.py b/src/calibre/ebooks/conversion/plugins/txt_input.py index 66357c5fb7..ded07e0e13 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_input.py +++ b/src/calibre/ebooks/conversion/plugins/txt_input.py @@ -95,12 +95,14 @@ class TXTInput(InputFormatPlugin): ).format('https://python-markdown.github.io/extensions/') + '\n'.join('* %s: %s' % (k, MD_EXTENSIONS[k]) for k in sorted(MD_EXTENSIONS))), } - def shift_file(self, base_dir, fname, data): + def shift_file(self, fname, data): name, ext = os.path.splitext(fname) - c = 1 - while os.path.exists(os.path.join(base_dir, '{}-{}{}'.format(name, c, ext))): + candidate = os.path.join(self.output_dir, fname) + c = 0 + while os.path.exists(candidate): c += 1 - ans = os.path.join(base_dir, '{}-{}{}'.format(name, c, ext)) + candidate = os.path.join(self.output_dir, '{}-{}{}'.format(name, c, ext)) + ans = candidate with open(ans, 'wb') as f: f.write(data) return f.name @@ -117,7 +119,7 @@ class TXTInput(InputFormatPlugin): if os.access(src, os.R_OK): with open(src, 'rb') as f: data = f.read() - f = self.shift_file(base_dir, os.path.basename(src), data) + f = self.shift_file(os.path.basename(src), data) changed = True img.set('src', os.path.basename(f)) if changed: @@ -141,7 +143,7 @@ class TXTInput(InputFormatPlugin): txt = b'' log.debug('Reading text from file...') length = 0 - base_dir = getcwd() + base_dir = self.output_dir = getcwd() # Extract content from zip archive. if file_ext == 'txtz': @@ -278,7 +280,7 @@ class TXTInput(InputFormatPlugin): for opt in html_input.options: setattr(options, opt.option.name, opt.recommended_value) options.input_encoding = 'utf-8' - htmlfile = self.shift_file(base_dir, 'index.html', html.encode('utf-8')) + htmlfile = self.shift_file('index.html', html.encode('utf-8')) odi = options.debug_pipeline options.debug_pipeline = None # Generate oeb from html conversion. diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index 21857f7705..3a97a4fcc3 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -19,7 +19,7 @@ from css_parser import replaceUrls from css_parser.css import CSSRule from lxml.etree import Comment -from calibre import detect_ncpus, force_unicode, prepare_string_for_xml +from calibre import CurrentDir, detect_ncpus, force_unicode, prepare_string_for_xml from calibre.constants import iswindows, plugins from calibre.customize.ui import plugin_for_input_format from calibre.ebooks import parse_css_length @@ -787,6 +787,7 @@ def get_stored_annotations(container, bookmark_data): def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, extract_annotations=False, virtualize_resources=True, max_workers=1): + pathtoebook = os.path.abspath(pathtoebook) with RenderManager(max_workers) as render_manager: mi = None if serialize_metadata: @@ -794,7 +795,8 @@ def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, ex from calibre.customize.ui import quick_metadata with lopen(pathtoebook, 'rb') as f, quick_metadata: mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower()) - book_fmt, opfpath, input_fmt = extract_book(pathtoebook, output_dir, log=default_log) + with CurrentDir(output_dir): + book_fmt, opfpath, input_fmt = extract_book(pathtoebook, output_dir, log=default_log) container, bookmark_data = process_exploded_book( book_fmt, opfpath, input_fmt, output_dir, render_manager, book_hash=book_hash, save_bookmark_data=extract_annotations,