From 5cd5ba746c0c577b62e4ab05e8b727a20523283e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 17 Jun 2016 19:27:16 +0530 Subject: [PATCH] HTML Input: Fix error when converting HTML file with URL encoded filename. Fixes #1593632 [ebook-convert crashes on files with urlencoded characters in file name](https://bugs.launchpad.net/calibre/+bug/1593632) --- .../ebooks/conversion/plugins/html_input.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/html_input.py b/src/calibre/ebooks/conversion/plugins/html_input.py index f56c21c48d..5c12f3991c 100644 --- a/src/calibre/ebooks/conversion/plugins/html_input.py +++ b/src/calibre/ebooks/conversion/plugins/html_input.py @@ -87,8 +87,8 @@ class HTMLInput(InputFormatPlugin): return self._is_case_sensitive if not path or not os.path.exists(path): return islinux or isbsd - self._is_case_sensitive = not (os.path.exists(path.lower()) - and os.path.exists(path.upper())) + self._is_case_sensitive = not (os.path.exists(path.lower()) and + os.path.exists(path.upper())) return self._is_case_sensitive def create_oebbook(self, htmlpath, basedir, opts, log, mi): @@ -96,7 +96,7 @@ class HTMLInput(InputFormatPlugin): from calibre.ebooks.conversion.plumber import create_oebbook from calibre.ebooks.oeb.base import (DirContainer, rewrite_links, urlnormalize, urldefrag, BINARY_MIME, OEB_STYLES, - xpath) + xpath, urlquote) from calibre import guess_type from calibre.ebooks.oeb.transforms.metadata import \ meta_info_to_oeb_metadata @@ -149,6 +149,8 @@ class HTMLInput(InputFormatPlugin): href=ascii_filename(bname)) htmlfile_map[path] = href item = oeb.manifest.add(id, href, 'text/html') + if path == htmlpath and '%' in path: + bname = urlquote(bname) item.html_input_href = bname oeb.spine.add(item, True) @@ -168,7 +170,11 @@ class HTMLInput(InputFormatPlugin): path = f.path dpath = os.path.dirname(path) oeb.container = DirContainer(dpath, log, ignore_opf=True) - item = oeb.manifest.hrefs[htmlfile_map[path]] + href = htmlfile_map[path] + try: + item = oeb.manifest.hrefs[href] + except KeyError: + item = oeb.manifest.hrefs[urlnormalize(href)] rewrite_links(item.data, partial(self.resource_adder, base=dpath)) for item in oeb.manifest.values():