mirror of
https://github.com/ciromattia/kcc
synced 2025-12-19 12:41:47 +00:00
Add cover to NCX
This commit is contained in:
@@ -65,7 +65,7 @@ def buildHTML(path,file):
|
|||||||
f.close()
|
f.close()
|
||||||
return path,file
|
return path,file
|
||||||
|
|
||||||
def buildNCX(dstdir, title):
|
def buildNCX(dstdir, title, chapters):
|
||||||
ncxfile = dstdir + '/toc.ncx'
|
ncxfile = dstdir + '/toc.ncx'
|
||||||
f = open(ncxfile, "w")
|
f = open(ncxfile, "w")
|
||||||
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
||||||
@@ -73,12 +73,19 @@ def buildNCX(dstdir, title):
|
|||||||
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
||||||
"<head>\n</head>\n",
|
"<head>\n</head>\n",
|
||||||
"<docTitle><text>",title,"</text></docTitle>\n",
|
"<docTitle><text>",title,"</text></docTitle>\n",
|
||||||
"<navMap></navMap>\n</ncx>"
|
"<navMap>"
|
||||||
])
|
])
|
||||||
|
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("<navPoint id=\"" + folder + "\"><navLabel><text>" + title
|
||||||
|
+ "</text></navLabel><content src=\"" + filename[0] + ".html\"/></navPoint>\n")
|
||||||
|
f.write("</navMap>\n</ncx>")
|
||||||
f.close()
|
f.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
def buildOPF(profile, dstdir, title, filelist):
|
def buildOPF(profile, dstdir, title, filelist, cover=None):
|
||||||
opffile = dstdir + '/content.opf'
|
opffile = dstdir + '/content.opf'
|
||||||
# read the first file resolution
|
# read the first file resolution
|
||||||
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
||||||
@@ -89,20 +96,30 @@ def buildOPF(profile, dstdir, title, filelist):
|
|||||||
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
||||||
"<dc:title>",title,"</dc:title>\n",
|
"<dc:title>",title,"</dc:title>\n",
|
||||||
"<dc:language>en-US</dc:language>\n",
|
"<dc:language>en-US</dc:language>\n",
|
||||||
|
"<meta name=\"cover\" content=\"cover\"/>\n",
|
||||||
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
||||||
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
||||||
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
||||||
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
||||||
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
||||||
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
||||||
"</metadata><manifest><item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"])
|
"</metadata>\n<manifest>\n<item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\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("<item id=\"cover\" href=\"" + os.path.join(folder,cover[1]) + "\" media-type=\"" + mt + "\"/>\n")
|
||||||
for path in filelist:
|
for path in filelist:
|
||||||
folder = path[0].replace(dstdir,'').lstrip('/')
|
folder = path[0].replace(dstdir,'').lstrip('/')
|
||||||
filename = getImageFileName(path[1])
|
filename = getImageFileName(path[1])
|
||||||
uniqueid = os.path.join(folder,filename[0]).replace('/','_')
|
uniqueid = os.path.join(folder,filename[0]).replace('/','_')
|
||||||
f.write("<item id=\"page_" + uniqueid + "\" href=\"" + os.path.join(folder,filename[0])
|
f.write("<item id=\"page_" + uniqueid + "\" href=\"" + os.path.join(folder,filename[0])
|
||||||
+ ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
+ ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
||||||
#for filename in filelist:
|
|
||||||
if '.png' == filename[1]:
|
if '.png' == filename[1]:
|
||||||
mt = 'image/png'
|
mt = 'image/png'
|
||||||
else:
|
else:
|
||||||
@@ -154,7 +171,8 @@ def dirImgProcess(path):
|
|||||||
img = image.ComicPage(os.path.join(dirpath,file), options.profile)
|
img = image.ComicPage(os.path.join(dirpath,file), options.profile)
|
||||||
split = img.splitPage(dirpath, options.righttoleft)
|
split = img.splitPage(dirpath, options.righttoleft)
|
||||||
if split is not None:
|
if split is not None:
|
||||||
print "Splitted " + file
|
if options.verbose:
|
||||||
|
print "Splitted " + file
|
||||||
img0 = image.ComicPage(split[0],options.profile)
|
img0 = image.ComicPage(split[0],options.profile)
|
||||||
img1 = image.ComicPage(split[1],options.profile)
|
img1 = image.ComicPage(split[1],options.profile)
|
||||||
applyImgOptimization(img0)
|
applyImgOptimization(img0)
|
||||||
@@ -168,7 +186,10 @@ def dirImgProcess(path):
|
|||||||
def genEpubStruct(path):
|
def genEpubStruct(path):
|
||||||
global options
|
global options
|
||||||
filelist = []
|
filelist = []
|
||||||
|
chapterlist = []
|
||||||
|
cover = None
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
|
chapter = False
|
||||||
for file in filenames:
|
for file in filenames:
|
||||||
if getImageFileName(file) is not None:
|
if getImageFileName(file) is not None:
|
||||||
# put credits at the end
|
# 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))
|
os.rename(os.path.join(dirpath,file), os.path.join(dirpath,'ZZZ999_'+file))
|
||||||
file = 'ZZZ999_'+file
|
file = 'ZZZ999_'+file
|
||||||
filelist.append(buildHTML(dirpath,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':
|
if options.title == 'defaulttitle':
|
||||||
options.title = os.path.basename(path)
|
options.title = os.path.basename(path)
|
||||||
buildNCX(path,options.title)
|
buildNCX(path,options.title,chapterlist)
|
||||||
# ensure we're sorting files alphabetically
|
# ensure we're sorting files alphabetically
|
||||||
filelist = sorted(filelist, key=lambda name: (name[0].lower(), name[1].lower()))
|
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):
|
def getWorkFolder(file):
|
||||||
fname = os.path.splitext(file)
|
fname = os.path.splitext(file)
|
||||||
|
|||||||
Reference in New Issue
Block a user