Add the list_equals formatter function.

This commit is contained in:
Charles Haley 2011-09-27 18:42:11 +02:00
parent d39ba839e2
commit c4404f52c8
2 changed files with 20 additions and 1 deletions

View File

@ -266,6 +266,7 @@ The following functions are available in addition to those described in single-f
* ``has_cover()`` -- return ``Yes`` if the book has a cover, otherwise return the empty string
* ``not(value)`` -- returns the string "1" if the value is empty, otherwise returns the empty string. This function works well with test or first_non_empty. You can have as many values as you want.
* ``list_difference(list1, list2, separator)`` -- return a list made by removing from `list1` any item found in `list2`, using a case-insensitive compare. The items in `list1` and `list2` are separated by separator, as are the items in the returned list.
* ``list_equals(list1, sep1, list2, sep2, yes_val, no_val) -- return `yes_val` if list1 and list2 contain the same items, otherwise return `no_val`. The items are determined by splitting each list using the appropriate separator character (`sep1` or `sep2`). The order of items in the lists is not relevant. The compare is case insensitive.
* ``list_intersection(list1, list2, separator)`` -- return a list made by removing from `list1` any item not found in `list2`, using a case-insensitive compare. The items in `list1` and `list2` are separated by separator, as are the items in the returned list.
* ``list_sort(list, direction, separator)`` -- return list sorted using a case-insensitive sort. If `direction` is zero, the list is sorted ascending, otherwise descending. The list items are separated by separator, as are the items in the returned list.
* ``list_union(list1, list2, separator)`` -- return a list made by merging the items in list1 and list2, removing duplicate items using a case-insensitive compare. If items differ in case, the one in list1 is used. The items in list1 and list2 are separated by separator, as are the items in the returned list.

View File

@ -965,6 +965,24 @@ class BuiltinListSort(BuiltinFormatterFunction):
res = [l.strip() for l in list1.split(separator) if l.strip()]
return ', '.join(sorted(res, key=sort_key, reverse=direction != "0"))
class BuiltinListEquals(BuiltinFormatterFunction):
name = 'list_equals'
arg_count = 6
category = 'List Manipulation'
__doc__ = doc = _('list_equals(list1, sep1, list2, sep2, yes_val, no_val) -- '
'return yes_val if list1 and list2 contain the same items, '
'otherwise return `no_val. The items are determined by splitting '
'each list using the appropriate separator character (sep1 or '
'sep2). The order of items in the lists is not relevant. '
'The compare is case insensitive.')
def evaluate(self, formatter, kwargs, mi, locals, list1, sep1, list2, sep2, yes_val, no_val):
s1 = set([icu_lower(l.strip()) for l in list1.split(sep1) if l.strip()])
s2 = set([icu_lower(l.strip()) for l in list2.split(sep2) if l.strip()])
if s1 == s2:
return yes_val
return no_val
class BuiltinToday(BuiltinFormatterFunction):
name = 'today'
arg_count = 0
@ -1045,7 +1063,7 @@ _formatter_builtins = [
BuiltinFormatNumber(), BuiltinFormatsModtimes(), BuiltinFormatsSizes(),
BuiltinHasCover(), BuiltinHumanReadable(), BuiltinIdentifierInList(),
BuiltinIfempty(), BuiltinLanguageCodes(), BuiltinLanguageStrings(),
BuiltinInList(), BuiltinListDifference(),
BuiltinInList(), BuiltinListDifference(), BuiltinListEquals(),
BuiltinListIntersection(), BuiltinListitem(), BuiltinListSort(),
BuiltinListUnion(), BuiltinLookup(),
BuiltinLowercase(), BuiltinMultiply(), BuiltinNot(),