about summary refs log tree commit diff
diff options
context:
space:
mode:
authorfsteffek <fsteffek@users.noreply.github.com>2015-02-21 15:29:58 +0100
committerPaweł Jastrzębski <pawelj@iosphe.re>2015-02-21 17:59:46 +0100
commit1ec07fe4ec7d7a67401ef2c3420ab41dcc34634f (patch)
tree4338e2597f4ca9ab77c7df823e120ee06548dea6
parentMerge pull request #128 from fsteffek/dev (diff)
downloadkcc-1ec07fe4ec7d7a67401ef2c3420ab41dcc34634f.tar.gz
kcc-1ec07fe4ec7d7a67401ef2c3420ab41dcc34634f.tar.bz2
kcc-1ec07fe4ec7d7a67401ef2c3420ab41dcc34634f.zip
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.
-rwxr-xr-xkcc/comic2ebook.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py
index 31e1e8f..adfc4c9 100755
--- a/kcc/comic2ebook.py
+++ b/kcc/comic2ebook.py
@@ -422,9 +422,14 @@ def buildEPUB(path, chapterNames, tomeNumber):
                   "}",
                   ])
     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')):
         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:
             filename = getImageFileName(afile)
             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']:
         filelist[-1] = buildHTML(lastfile[0], lastfile[1], lastfile[2], True)
     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)