1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 01:36:27 +00:00

Fix page order and bookmarks by renaming numerically (#507)

* disable slugify

* rename to digits

* add -kcc

* add ABC checks

* remove kobo

* don't use if statements

* rename to suffix

* re-order check

* only slugify directories

* add break

* add sorted

* fix kcc-b logic

* add kcc to front of filename

---------

Co-authored-by: Alexander Xu <alexanderx@qualtrics.com>
This commit is contained in:
Alex Xu
2023-08-07 11:22:46 -07:00
committed by GitHub
parent f75ea6dfe8
commit 6836c20377

View File

@@ -514,18 +514,30 @@ def buildEPUB(path, chapternames, tomenumber):
# Overwrite chapternames if tree is flat and ComicInfo.xml has bookmarks
if not chapternames and options.chapters:
chapterlist = []
globaldiff = 0
global_diff = 0
diff_delta = 0
# if split
if options.splitter == 0:
diff_delta = 1
# if rotate and split
elif options.splitter == 2:
diff_delta = 2
for aChapter in options.chapters:
pageid = aChapter[0]
for x in range(0, pageid + globaldiff + 1):
cur_diff = global_diff
global_diff = 0
for x in range(0, pageid + cur_diff + 1):
if '-kcc-b' in filelist[x][1]:
pageid += 1
if '-kcc-c' in filelist[pageid][1]:
pageid -= 1
pageid += diff_delta
global_diff += diff_delta
filename = filelist[pageid][1]
chapterlist.append((filelist[pageid][0].replace('Images', 'Text'), filename))
chapternames[filename] = aChapter[1]
globaldiff = pageid - (aChapter[0] + globaldiff)
buildNCX(path, options.title, chapterlist, chapternames)
buildNAV(path, options.title, chapterlist, chapternames)
buildOPF(path, options.title, filelist, cover)
@@ -749,19 +761,23 @@ def getPanelViewSize(deviceres, size):
def sanitizeTree(filetree):
chapterNames = {}
for root, dirs, files in os.walk(filetree, False):
for name in files:
for i, name in enumerate(sorted(files)):
splitname = os.path.splitext(name)
slugified = slugify(splitname[0], False)
while os.path.exists(os.path.join(root, slugified + splitname[1])) and splitname[0].upper()\
!= slugified.upper():
slugified += "A"
# file needs kcc at front AND back to avoid renaming issues
slugified = f'kcc-{i:04}'
for suffix in '-KCC', '-KCC-A', '-KCC-B', '-KCC-C':
if splitname[0].endswith(suffix):
slugified += suffix.lower()
break
newKey = os.path.join(root, slugified + splitname[1])
key = os.path.join(root, name)
if key != newKey:
os.replace(key, newKey)
for name in dirs:
tmpName = name
slugified = slugify(name, True)
slugified = slugify(name)
while os.path.exists(os.path.join(root, slugified)) and name.upper() != slugified.upper():
slugified += "A"
chapterNames[slugified] = tmpName
@@ -772,23 +788,6 @@ def sanitizeTree(filetree):
return chapterNames
def sanitizeTreeKobo(filetree):
pageNumber = 0
for root, dirs, files in os.walk(filetree):
dirs, files = walkSort(dirs, files)
for name in files:
splitname = os.path.splitext(name)
slugified = str(pageNumber).zfill(5)
pageNumber += 1
while os.path.exists(os.path.join(root, slugified + splitname[1])) and splitname[0].upper()\
!= slugified.upper():
slugified += "A"
newKey = os.path.join(root, slugified + splitname[1])
key = os.path.join(root, name)
if key != newKey:
os.replace(key, newKey)
def sanitizePermissions(filetree):
for root, dirs, files in os.walk(filetree, False):
for name in files:
@@ -911,11 +910,8 @@ def createNewTome():
return tomePath, tomePathRoot
def slugify(value, isdir):
if isdir:
value = slugify_ext(value, regex_pattern=r'[^-a-z0-9_\.]+').strip('.')
else:
value = slugify_ext(value).strip('.')
def slugify(value):
value = slugify_ext(value, regex_pattern=r'[^-a-z0-9_\.]+').strip('.')
value = sub(r'0*([0-9]{4,})', r'\1', sub(r'([0-9]+)', r'0000\1', value, count=2))
return value
@@ -1162,8 +1158,6 @@ def makeBook(source, qtgui=None):
if GUI:
GUI.progressBarTick.emit('1')
chapterNames = sanitizeTree(os.path.join(path, 'OEBPS', 'Images'))
if 'Ko' in options.profile and options.format == 'CBZ':
sanitizeTreeKobo(os.path.join(path, 'OEBPS', 'Images'))
if options.batchsplit > 0:
tomes = splitDirectory(path)
else: