From 2b4307fc7fae9f722bf1d2b2ebd53b1d51c00d34 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 Jul 2012 08:56:15 +0530 Subject: [PATCH] CHM Input: Fix handling of chm files that split their html into multiple sub-directories. Fixes #1018792 (CHM ToC generated incorrectly) --- .../ebooks/conversion/plugins/chm_input.py | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/chm_input.py b/src/calibre/ebooks/conversion/plugins/chm_input.py index a674735f1d..30cf984c77 100644 --- a/src/calibre/ebooks/conversion/plugins/chm_input.py +++ b/src/calibre/ebooks/conversion/plugins/chm_input.py @@ -152,27 +152,31 @@ class CHMInput(InputFormatPlugin): #print "=============================" log.debug('Found %d section nodes' % len(chapters)) htmlpath = os.path.splitext(hhcpath)[0] + ".html" - f = open(htmlpath, 'wb') - if chapters: - f.write('\n') - path0 = chapters[0][1] - subpath = os.path.dirname(path0) + with open(htmlpath, 'wb') as f: + if chapters: + f.write('\n') + path0 = chapters[0][1] + subpath = os.path.dirname(path0) + base = os.path.dirname(f.name) - for chapter in chapters: - title = chapter[0] - rsrcname = os.path.basename(chapter[1]) - rsrcpath = os.path.join(subpath, rsrcname) - # title should already be url encoded - url = "
" + title + " \n" - if isinstance(url, unicode): - url = url.encode('utf-8') - f.write(url) + for chapter in chapters: + title = chapter[0] + rsrcname = os.path.basename(chapter[1]) + rsrcpath = os.path.join(subpath, rsrcname) + if (not os.path.exists(os.path.join(base, rsrcpath)) and + os.path.exists(os.path.join(base, chapter[1]))): + rsrcpath = chapter[1] - f.write("") - else: - f.write(hhcdata) - f.close() + # title should already be url encoded + url = "
" + title + " \n" + if isinstance(url, unicode): + url = url.encode('utf-8') + f.write(url) + + f.write("") + else: + f.write(hhcdata) return htmlpath