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']
+ value2 = v.from_python((title, anonym_params, named_params))
+ assert value2 == u'{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}'
+ v = wrpylib.mwmarkup.TemplateValidator(as_table=True)
+ value3 = v.from_python((title, anonym_params, named_params))
+ print value3
+ assert value3 == \
+ u'{{Rodelbahnbox\n' + \
+ u'| Unbenannt\n' + \
+ u'| Position = 47.309820 N 9.986508 E\n' + \
+ u'| Aufstieg möglich = Ja\n' + \
+ u'}}'
v = wrpylib.mwmarkup.TemplateValidator(strip=False)
title, anonym_params, named_params = v.to_python(value)
assert title == u'Rodelbahnbox '
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."""
+ def __init__(self, strip=True, as_table=False, as_table_keylen=None):
+ """Validates a MediaWiki template, e.g. {{Color|red}}
+ :param stip: If strip is True, the title, and the parameter keys and values are stripped in to_python.
+ :param as_table: formats the returned template in one row for each parameter
+ :param as_table_keylen: length of the key field for from_python. None for "automatic"."""
self.strip = (lambda s: s.strip()) if strip else (lambda s: s)
+ self.as_table = as_table
+ self.as_table_keylen = as_table_keylen
def to_python(self, value, state=None):
"""Takes a template, like u'{{Color|red|text=Any text}}' and translates it to a Python tuple
return title, anonym_params, named_params
+ def from_python(self, value, state=None):
+ """Formats a MediaWiki template.
+ value is a tuple: (title, anonym_params, named_params)
+ where title is the template title, anonym_params is a list of anonymous parameters and
+ named_params is a dict or OrderedDict of named parameters."""
+ title, anonym_params, named_params = value
+ pipe_char, equal_char, end_char = (u'\n| ', u' = ', u'\n}}') if self.as_table else (u'|', u'=', u'}}')
+ parts = [u"{{" + title]
+ parts += anonym_params
+ as_table_keylen = self.as_table_keylen
+ if self.as_table and as_table_keylen is None:
+ as_table_keylen = max(map(len, named_params.iterkeys()))
+ for k, v in named_params.iteritems():
+ if self.as_table:
+ k = k.ljust(as_table_keylen)
+ parts.append((k + equal_char + v).rstrip())
+ else:
+ parts.append(k + equal_char + v)
+ return pipe_char.join(parts) + end_char
+
def split_template(template):
"""Deprecated legacy function.
def create_template(template_title, anonym_params=[], named_param_keys=[], named_param_values=[], as_table=False, as_table_keylen=None):
- """Formats a MediaWiki template.
+ """Deprecated legacy function.
+
+ Formats a MediaWiki template.
:param template_title: Unicode string with the template name
:param anonym_params: list with parameters without keys
:param named_param_keys: list with keys of named parameters
:param as_table: formats the returned template in one row for each parameter
:param as_table_keylen: length of the key field. None for "automatic".
:return: unicode template"""
- pipe_char, equal_char, end_char = (u'\n| ', u' = ', u'\n}}') if as_table else (u'|', u'=', u'}}')
- parts = [u"{{" + template_title]
- parts += anonym_params
- if as_table and as_table_keylen is None:
- as_table_keylen = max([len(k) for k in named_param_keys])
- for i in xrange(len(named_param_keys)):
- key = named_param_keys[i]
- if as_table:
- key = key.ljust(as_table_keylen)
- parts.append((key + equal_char + named_param_values[i]).rstrip())
- else:
- parts.append(key + equal_char + named_param_values[i])
- return pipe_char.join(parts) + end_char
+ named_params = collections.OrderedDict(zip(named_param_keys, named_param_values))
+ return TemplateValidator(as_table=as_table, as_table_keylen=as_table_keylen).from_python((template_title, anonym_params, named_params))
def find_tag(wikitext, tagname, pos=0):