From eff9a695e13e863387bbd555de99a898e290fe47 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 8 Oct 2015 10:01:16 +0530 Subject: [PATCH] Tag mapper: When the replacement tag contains commas, create multiple tags instead of replacing the comma with a semi-colon. Fixes #1503526 [Tap Mapping comma as semi-colon as of 2.40- did not on 2.39](https://bugs.launchpad.net/calibre/+bug/1503526) --- src/calibre/ebooks/metadata/tag_mapper.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/metadata/tag_mapper.py b/src/calibre/ebooks/metadata/tag_mapper.py index 279d1e4b21..7bd41c3d49 100644 --- a/src/calibre/ebooks/metadata/tag_mapper.py +++ b/src/calibre/ebooks/metadata/tag_mapper.py @@ -44,6 +44,8 @@ def apply_rules(tag, rules): tag = regex.sub(rule['query'], rule['replace'], tag, flags=REGEX_FLAGS) else: tag = rule['replace'] + if ',' in tag: + tag = [x.strip() for x in tag.split(',')] return tag def uniq(vals, kmap=icu_lower): @@ -62,10 +64,18 @@ def map_tags(tags, rules=()): if not rules: return list(tags) rules = [(r, matcher(r)) for r in rules] - return uniq([x for x in (apply_rules(t, rules) for t in tags) if x]) + ans = [] + for t in tags: + mapped = apply_rules(t, rules) + (ans.extend if isinstance(mapped, list) else ans.append)(mapped) + return uniq(filter(None, ans)) def test(): rules = [{'action':'replace', 'query':'t1', 'match_type':'one_of', 'replace':'t2'}] assert map_tags(['t1', 'x1'], rules) == ['t2', 'x1'] rules = [{'action':'replace', 'query':'(.)1', 'match_type':'matches', 'replace':r'\g<1>2'}] assert map_tags(['t1', 'x1'], rules) == ['t2', 'x2'] + rules = [{'action':'replace', 'query':'t1', 'match_type':'one_of', 'replace':'t2, t3'}] + assert map_tags(['t1', 'x1'], rules) == ['t2', 't3', 'x1'] + rules = [{'action':'replace', 'query':'(.)1', 'match_type':'matches', 'replace':r'\g<1>2,3'}] + assert map_tags(['t1', 'x1'], rules) == ['t2', '3', 'x2']