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)

This commit is contained in:
Kovid Goyal 2019-11-21 09:16:21 +05:30
parent d57626c043
commit 30b8e1de84
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 9 deletions

View File

@ -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))), ).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) name, ext = os.path.splitext(fname)
c = 1 candidate = os.path.join(self.output_dir, fname)
while os.path.exists(os.path.join(base_dir, '{}-{}{}'.format(name, c, ext))): c = 0
while os.path.exists(candidate):
c += 1 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: with open(ans, 'wb') as f:
f.write(data) f.write(data)
return f.name return f.name
@ -117,7 +119,7 @@ class TXTInput(InputFormatPlugin):
if os.access(src, os.R_OK): if os.access(src, os.R_OK):
with open(src, 'rb') as f: with open(src, 'rb') as f:
data = f.read() 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 changed = True
img.set('src', os.path.basename(f)) img.set('src', os.path.basename(f))
if changed: if changed:
@ -141,7 +143,7 @@ class TXTInput(InputFormatPlugin):
txt = b'' txt = b''
log.debug('Reading text from file...') log.debug('Reading text from file...')
length = 0 length = 0
base_dir = getcwd() base_dir = self.output_dir = getcwd()
# Extract content from zip archive. # Extract content from zip archive.
if file_ext == 'txtz': if file_ext == 'txtz':
@ -278,7 +280,7 @@ class TXTInput(InputFormatPlugin):
for opt in html_input.options: for opt in html_input.options:
setattr(options, opt.option.name, opt.recommended_value) setattr(options, opt.option.name, opt.recommended_value)
options.input_encoding = 'utf-8' 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 odi = options.debug_pipeline
options.debug_pipeline = None options.debug_pipeline = None
# Generate oeb from html conversion. # Generate oeb from html conversion.

View File

@ -19,7 +19,7 @@ from css_parser import replaceUrls
from css_parser.css import CSSRule from css_parser.css import CSSRule
from lxml.etree import Comment 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.constants import iswindows, plugins
from calibre.customize.ui import plugin_for_input_format from calibre.customize.ui import plugin_for_input_format
from calibre.ebooks import parse_css_length 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): 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: with RenderManager(max_workers) as render_manager:
mi = None mi = None
if serialize_metadata: if serialize_metadata:
@ -794,6 +795,7 @@ def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, ex
from calibre.customize.ui import quick_metadata from calibre.customize.ui import quick_metadata
with lopen(pathtoebook, 'rb') as f, quick_metadata: with lopen(pathtoebook, 'rb') as f, quick_metadata:
mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower()) mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower())
with CurrentDir(output_dir):
book_fmt, opfpath, input_fmt = extract_book(pathtoebook, output_dir, log=default_log) book_fmt, opfpath, input_fmt = extract_book(pathtoebook, output_dir, log=default_log)
container, bookmark_data = process_exploded_book( container, bookmark_data = process_exploded_book(
book_fmt, opfpath, input_fmt, output_dir, render_manager, book_fmt, opfpath, input_fmt, output_dir, render_manager,