From: philipp Date: Sat, 8 Mar 2014 19:42:51 +0000 (+0000) Subject: Implemented that stripping the whitespace in a template is now optional. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/commitdiff_plain/47d9a34c71d2d18c70a568c4abd82df648592a47?ds=sidebyside Implemented that stripping the whitespace in a template is now optional. git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1912 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/tests/test_mwmarkup.py b/tests/test_mwmarkup.py index 1f17ca5..dc89126 100644 --- a/tests/test_mwmarkup.py +++ b/tests/test_mwmarkup.py @@ -43,6 +43,12 @@ def test_TemplateValidator(): assert anonym_params == [u'Unbenannt'] assert named_params.keys() == [u'Position', u'Aufstieg möglich'] assert named_params.values() == ['47.309820 N 9.986508 E', 'Ja'] + v = wrpylib.mwmarkup.TemplateValidator(strip=False) + title, anonym_params, named_params = v.to_python(value) + assert title == u'Rodelbahnbox ' + assert anonym_params == [u' Unbenannt '] + assert named_params.keys() == [u' Position ', u' Aufstieg möglich '] + assert named_params.values() == [' 47.309820 N 9.986508 E ', ' Ja '] def test_split_template(): diff --git a/wrpylib/mwmarkup.py b/wrpylib/mwmarkup.py index 2f8faea..b121f08 100644 --- a/wrpylib/mwmarkup.py +++ b/wrpylib/mwmarkup.py @@ -50,6 +50,10 @@ def find_template(wikitext, template_title): class TemplateValidator(formencode.FancyValidator): + def __init__(self, strip=True): + """If strip is True, the title, and the parameter keys and values are stripped in to_python.""" + self.strip = (lambda s: s.strip()) if strip else (lambda s: s) + def to_python(self, value, state=None): """Takes a template, like u'{{Color|red|text=Any text}}' and translates it to a Python tuple (title, anonym_params, named_params) where title is the template title, @@ -62,7 +66,7 @@ class TemplateValidator(formencode.FancyValidator): parts = value[2:-2].split(u'|') # template name - title = parts[0].strip() + title = self.strip(parts[0]) if len(title) == 0: raise formencode.Invalid(u'Empty template tilte.', value, state) del parts[0] @@ -72,7 +76,7 @@ class TemplateValidator(formencode.FancyValidator): while len(parts) > 0: equalsign_pos = parts[0].find(u'=') if equalsign_pos >= 0: break # named parameter - anonym_params.append(parts[0].strip()) + anonym_params.append(self.strip(parts[0])) del parts[0] # named or numbered parameters @@ -82,12 +86,12 @@ class TemplateValidator(formencode.FancyValidator): if equalsign_pos < 0: raise formencode.Invalid(u'Anonymous parameter after named parameter.', value, state) key, sep, value = parts[0].partition(u'=') - key = key.strip() + key = self.strip(key) if len(key) == 0: raise formencode.Invalid(u'Empty key.', value, state) if named_params.has_key(key): raise formencode.Invalid(u'Duplicate key: "{0}"'.format(key), value, state) - named_params[key] = value.strip() + named_params[key] = self.strip(value) del parts[0] return title, anonym_params, named_params