From a71636ec4eb81791e2b7bcba884fc0ee3a9851ef Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Fri, 27 May 2011 09:06:35 +0000 Subject: [PATCH] Allow modifier % on styledtext svn: r17578 --- src/gen/lib/styledtext.py | 37 +++++++++++++++++++++++++++++++++++++ src/plugins/Records.py | 7 +++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/gen/lib/styledtext.py b/src/gen/lib/styledtext.py index 63253528d..ea922687f 100644 --- a/src/gen/lib/styledtext.py +++ b/src/gen/lib/styledtext.py @@ -126,6 +126,43 @@ class StyledText(object): def __ne__(self, other): return self._string != other._string or self._tags != other._tags + def __mod__(self, other): + """Implement '%' operation on the class.""" + + # test whether the formatting operation is valid at all + self._string % other + + result = self.__class__(self._string, self._tags) + + i0 = 0 + while True: + i1 = result._string.find('%', i0) + if i1 < 0: + break + if result._string[i1+1] == '(': + i2 = result._string.find(')', i1+3) + param_name = result._string[i1+2:i2] + else: + i2 = i1 + param_name = None + for i3 in range(i2+1, len(result._string)): + if result._string[i3] in 'diouxXeEfFgGcrs%': + break + if param_name is not None: + param = other[param_name] + elif isinstance(other, tuple): + param = other[0] + other = other[1:] + else: + param = other + if not isinstance(param, StyledText): + param = StyledText('%' + result._string[i2+1:i3+1] % param) + (before, after) = result.split(result._string[i1:i3+1], 1) + result = before + param + after + i0 = i3 + 1 + + return result + # private methods diff --git a/src/plugins/Records.py b/src/plugins/Records.py index 6bf451ecd..9d0781483 100644 --- a/src/plugins/Records.py +++ b/src/plugins/Records.py @@ -215,10 +215,9 @@ def _find_records(db, filter, callname): father = db.get_person_from_handle(father_handle) mother = db.get_person_from_handle(mother_handle) - name = StyledText("").join([ - _Person_get_styled_primary_name(father, callname), - _(" and "), - _Person_get_styled_primary_name(mother, callname)]) + name = StyledText(_("%(father)s and %(mother)s")) % { + 'father': _Person_get_styled_primary_name(father, callname), + 'mother': _Person_get_styled_primary_name(mother, callname)} _record(None, family_mostchildren, len(family.get_child_ref_list()),