Implemented that stripping the whitespace in a template is now optional.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sat, 8 Mar 2014 19:42:51 +0000 (19:42 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sat, 8 Mar 2014 19:42:51 +0000 (19:42 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1912 7aebc617-e5e2-0310-91dc-80fb5f6d2477

tests/test_mwmarkup.py
wrpylib/mwmarkup.py

index 1f17ca54b205f1bafe07ee1d70d1419e1f4c7eaf..dc891264a38b13aaf7c7f317392866718dc30518 100644 (file)
@@ -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']
     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():
 
 
 def test_split_template():
index 2f8faea2850cf8a7a7dd42a03a2b15b460fce4b7..b121f08dcfb5532a1628feda885dc8f3c4deb57d 100644 (file)
@@ -50,6 +50,10 @@ def find_template(wikitext, template_title):
 
 
 class TemplateValidator(formencode.FancyValidator):
 
 
 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,
     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
         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]
         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
         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
             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'=')
             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)
             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
             del parts[0]
 
         return title, anonym_params, named_params