From 4d9c30da56ffe6e07c0270d386019d4fb92b9e25 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 24 Dec 2013 09:11:53 +0530 Subject: [PATCH] Ebook edit: When splitting files, name the newly created file with a _split suffix. --- src/calibre/ebooks/oeb/polish/split.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/split.py b/src/calibre/ebooks/oeb/polish/split.py index 8e059c479d..114133deca 100644 --- a/src/calibre/ebooks/oeb/polish/split.py +++ b/src/calibre/ebooks/oeb/polish/split.py @@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' -import copy, os +import copy, os, re from future_builtins import map from urlparse import urlparse @@ -178,7 +178,13 @@ def split(container, name, loc_or_xpath, before=True): root1, root2 = tree1.getroot(), tree2.getroot() anchors_in_top = frozenset(root1.xpath('//*/@id')) | frozenset(root1.xpath('//*/@name')) | {''} anchors_in_bottom = frozenset(root2.xpath('//*/@id')) | frozenset(root2.xpath('//*/@name')) - manifest_item = container.generate_item(name, media_type=container.mime_map[name]) + base, ext = name.rpartition('.')[0::2] + base = re.sub(r'_split\d+$', '', base) + nname, s = None, 0 + while not nname or container.exists(nname): + s += 1 + nname = '%s_split%d.%s' % (base, s, ext) + manifest_item = container.generate_item(nname, media_type=container.mime_map[name]) bottom_name = container.href_to_name(manifest_item.get('href'), container.opf_name) # Fix links in the split trees