From b453f61b92dec1efce2dd9709a9f72d566af13d2 Mon Sep 17 00:00:00 2001 From: Ciro Mattia Gonano Date: Mon, 21 Jan 2013 19:23:08 +0100 Subject: [PATCH] Add cover to NCX --- kcc/comic2ebook.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index c3888cc..0ccd616 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -65,7 +65,7 @@ def buildHTML(path,file): f.close() return path,file -def buildNCX(dstdir, title): +def buildNCX(dstdir, title, chapters): ncxfile = dstdir + '/toc.ncx' f = open(ncxfile, "w") f.writelines(["\n", @@ -73,12 +73,19 @@ def buildNCX(dstdir, title): "\n", "\n\n", "",title,"\n", - "\n" + "" ]) + for chapter in chapters: + folder = chapter[0].replace(dstdir,'').lstrip('/') + title = os.path.basename(folder) + filename = getImageFileName(os.path.join(folder,chapter[1])) + f.write("" + title + + "\n") + f.write("\n") f.close() return -def buildOPF(profile, dstdir, title, filelist): +def buildOPF(profile, dstdir, title, filelist, cover=None): opffile = dstdir + '/content.opf' # read the first file resolution profilelabel, deviceres, palette = image.ProfileData.Profiles[profile] @@ -89,20 +96,30 @@ def buildOPF(profile, dstdir, title, filelist): "\n", "",title,"\n", "en-US\n", + "\n", "\n", "\n", "\n", "\n", "\n", "\n", - "\n"]) + "\n\n\n" + ]) + # set cover + if cover is not None: + folder = cover[0].replace(dstdir,'').lstrip('/') + filename = getImageFileName(cover[1]) + if '.png' == filename[1]: + mt = 'image/png' + else: + mt = 'image/jpeg' + f.write("\n") for path in filelist: folder = path[0].replace(dstdir,'').lstrip('/') filename = getImageFileName(path[1]) uniqueid = os.path.join(folder,filename[0]).replace('/','_') f.write("\n") - #for filename in filelist: if '.png' == filename[1]: mt = 'image/png' else: @@ -154,7 +171,8 @@ def dirImgProcess(path): img = image.ComicPage(os.path.join(dirpath,file), options.profile) split = img.splitPage(dirpath, options.righttoleft) if split is not None: - print "Splitted " + file + if options.verbose: + print "Splitted " + file img0 = image.ComicPage(split[0],options.profile) img1 = image.ComicPage(split[1],options.profile) applyImgOptimization(img0) @@ -168,7 +186,10 @@ def dirImgProcess(path): def genEpubStruct(path): global options filelist = [] + chapterlist = [] + cover = None for (dirpath, dirnames, filenames) in os.walk(path): + chapter = False for file in filenames: if getImageFileName(file) is not None: # put credits at the end @@ -176,13 +197,17 @@ def genEpubStruct(path): os.rename(os.path.join(dirpath,file), os.path.join(dirpath,'ZZZ999_'+file)) file = 'ZZZ999_'+file filelist.append(buildHTML(dirpath,file)) - #filelist.extend(filenames) + if not chapter: + chapterlist.append((dirpath,filelist[-1][1])) + chapter = True + if cover is None: + cover = filelist[-1] if options.title == 'defaulttitle': options.title = os.path.basename(path) - buildNCX(path,options.title) + buildNCX(path,options.title,chapterlist) # ensure we're sorting files alphabetically filelist = sorted(filelist, key=lambda name: (name[0].lower(), name[1].lower())) - buildOPF(options.profile,path,options.title,filelist) + buildOPF(options.profile,path,options.title,filelist,cover) def getWorkFolder(file): fname = os.path.splitext(file)