diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index d90365eceb..b39c8b0fe3 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -516,7 +516,13 @@ class TagTreeItem(object): # {{{ name = tag.sort tt_author = True else: - name = tag.name + p = self + while p.parent.type != self.ROOT: + p = p.parent + if p.category_key.startswith('@'): + name = getattr(tag, 'original_name', tag.name) + else: + name = tag.name tt_author = False if role == Qt.DisplayRole: if tag.count == 0: @@ -903,18 +909,19 @@ class TagsModel(QAbstractItemModel): # {{{ node_parent = category components = [t for t in tag.name.split('.')] - if key in ['authors', 'publisher', 'title'] or len(components) == 1: + if key in ['authors', 'publisher', 'title'] or len(components) == 1 or \ + self.db.field_metadata[key]['kind'] == 'user': self.beginInsertRows(category_index, 999999, 1) TagTreeItem(parent=node_parent, data=tag, tooltip=tt, icon_map=self.icon_state_map) self.endInsertRows() else: - print components for i,comp in enumerate(components): - children = dict([(t.tag.name, t) for t in node_parent.children + child_map = dict([(t.tag.name, t) for t in node_parent.children if t.type != TagTreeItem.CATEGORY]) - if comp in children: - node_parent = children[comp] + if comp in child_map: + node_parent = child_map[comp] + node_parent.tag.count += tag.count else: if i < len(components)-1: t = copy.copy(tag) @@ -934,14 +941,14 @@ class TagsModel(QAbstractItemModel): # {{{ for category in self.category_nodes: if len(category.children) > 0: - children = category.children + child_map = category.children states = [c.tag.state for c in category.child_tags()] names = [(c.tag.name, c.tag.category) for c in category.child_tags()] state_map = dict(izip(names, states)) - ctags = [c for c in children if c.type == TagTreeItem.CATEGORY] + ctags = [c for c in child_map if c.type == TagTreeItem.CATEGORY] start = len(ctags) self.beginRemoveRows(self.createIndex(category.row(), 0, category), - start, len(children)-1) + start, len(child_map)-1) category.children = ctags self.endRemoveRows() else: diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 518f2ed140..03491c038a 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -396,6 +396,34 @@ class BuiltinListitem(BuiltinFormatterFunction): except: return '' +class BuiltinSublist(BuiltinFormatterFunction): + name = 'sublist' + arg_count = 4 + doc = _('sublist(val, start_index, end_index, separator) -- interpret the ' + ' value as a list of items separated by `separator`, returning a ' + ' new list made from the `start_index`th to the `end_index`th item. ' + 'The first item is number zero. If an index is negative, then it ' + 'counts from the end of the list. As a special case, an end_index ' + 'of zero is assumed to be the length of the list. Examples using ' + 'basic template mode and assuming a #genre value if A.B.C: ' + '{#genre:sublist(-1,0,.)} returns C
' + '{#genre:sublist(0,1,.)} returns A
' + '{#genre:sublist(0,-1,.)} returns A.B') + + def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep): + if not val: + return '' + si = int(start_index) + ei = int(end_index) + val = val.split(sep) + try: + if ei == 0: + return sep.join(val[si:]) + else: + return sep.join(val[si:ei]) + except: + return '' + class BuiltinUppercase(BuiltinFormatterFunction): name = 'uppercase' arg_count = 1 @@ -447,6 +475,7 @@ builtin_re = BuiltinRe() builtin_shorten = BuiltinShorten() builtin_strcat = BuiltinStrcat() builtin_strcmp = BuiltinStrcmp() +builtin_sublist = BuiltinSublist() builtin_substr = BuiltinSubstr() builtin_subtract = BuiltinSubtract() builtin_switch = BuiltinSwitch()