1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-24 07:01:51 +00:00

Fix random order of chapter list (close #129)

Directories and files returned by walk() are not always sorted.
Currently we sort the filelist after creating the it. When walk()-ing
topdown (we do) the directory list can be modified in-place. By sorting
the directories and files before creating the filelist, we guarantee
a sorted chapter list.
This commit is contained in:
fsteffek
2015-02-21 15:29:58 +01:00
committed by Paweł Jastrzębski
parent 5f8f7e0919
commit 1ec07fe4ec

View File

@@ -422,9 +422,14 @@ def buildEPUB(path, chapterNames, tomeNumber):
"}", "}",
]) ])
f.close() f.close()
# Ensure we're sorting dirs, files naturally
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in split('([0-9]+)', key)]
for (dirpath, dirnames, filenames) in walk(os.path.join(path, 'OEBPS', 'Images')): for (dirpath, dirnames, filenames) in walk(os.path.join(path, 'OEBPS', 'Images')):
chapter = False chapter = False
filenames.sort() # Traverse dirs in a sorted manner
dirnames.sort(key=lambda name: alphanum_key(name.lower()))
filenames.sort(key=lambda name: alphanum_key(name.lower()))
for afile in filenames: for afile in filenames:
filename = getImageFileName(afile) filename = getImageFileName(afile)
if '-kcc-hq' not in filename[0]: if '-kcc-hq' not in filename[0]:
@@ -442,10 +447,6 @@ def buildEPUB(path, chapterNames, tomeNumber):
and options.profile not in ['K1', 'K2', 'KDX', 'OTHER']: and options.profile not in ['K1', 'K2', 'KDX', 'OTHER']:
filelist[-1] = buildHTML(lastfile[0], lastfile[1], lastfile[2], True) filelist[-1] = buildHTML(lastfile[0], lastfile[1], lastfile[2], True)
buildNCX(path, options.title, chapterlist, chapterNames) buildNCX(path, options.title, chapterlist, chapterNames)
# Ensure we're sorting files alphabetically
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in split('([0-9]+)', key)]
filelist.sort(key=lambda name: (alphanum_key(name[0].lower()), alphanum_key(name[1].lower())))
buildOPF(path, options.title, filelist, cover) buildOPF(path, options.title, filelist, cover)