>>> print wikitext.__getslice__(*find_template(wikitext, u'Color'))
{{Color|red|red text}}
- The search is done with regular expression.
+ The search is done with regular expression. It gives wrong results when parsing a template
+ containing the characters "}}"
:param wikitext: The text (preferalbe unicode) that has the template in it.
:param template_title: The page title of the template with or without namespace (but as in the wikitext).
(start, end) of the first occurence with start >= 0 and end > start.
(None, None) if the template is not found.
"""
- match = re.search(u"\{\{" + template_title + "[^\}]*\}\}", wikitext, re.DOTALL)
+ match = re.search(u"\{\{" + template_title + "\s*(\|[^\}]*)?\}\}", wikitext, re.DOTALL)
if match is None: return None, None
return match.start(), match.end()
-
def split_template(template):
"""Takes a template, like u'{{Color|red|text=Any text}}' and translates it to a Python tuple
- (template_title, parameters) where parameters is a Python dictionary {1: u'red', u'text'=u'Any text'}.
+ (template_title, parameters) where parameters is a Python dictionary {u'1': u'red', u'text'=u'Any text'}.
Anonymous parameters get integer keys (converted to unicode) starting with 1
like in MediaWiki, named parameters are unicode strings.
Whitespace is stripped.
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])
+ 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
# match Rodelbahnbox
start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
if start is None: raise formencode.Invalid(u"Rodelbahnbox nicht gefunden", wikitext, None)
- template_name, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ template_title, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
# process properties
for key, value in properties.iteritems():
elif key == u'Lawinen': sledrun.avalanches = _conv(wrpylib.wrvalidators.GermanAvalanches().to_python, value, key) # 'kaum'
elif key == u'Betreiber': sledrun.operator = _conv(wrpylib.wrvalidators.UnicodeNone().to_python, value, key) # 'Max Mustermann'
elif key == u'Öffentliche Anreise': sledrun.public_transport = _conv(wrpylib.wrvalidators.GermanPublicTransport().to_python, value, key) # 'Mittelmäßig'
- elif key == u'Gehzeit': sledrun.walkup_time = _conv(wrpylib.wrvalidators.UnsignedNone().to_python, value, key) # 90
elif key == u'Aufstieg möglich': sledrun.walkup_possible = _conv(wrpylib.wrvalidators.GermanBoolNone().to_python, value, key) # 'Ja'
elif key == u'Aufstieg getrennt': sledrun.walkup_separate, sledrun.walkup_separate_comment = _conv(wrpylib.wrvalidators.GermanTristateFloatComment().to_python, value, key) # 'Ja'
+ elif key == u'Gehzeit': sledrun.walkup_time = _conv(wrpylib.wrvalidators.UnsignedNone().to_python, value, key) # 90
elif key == u'Aufstiegshilfe': sledrun.lift, sledrun.lift_details = _conv(wrpylib.wrvalidators.GermanLift().to_python, value, key) # 'Gondel (unterer Teil)'
elif key == u'Beleuchtungsanlage': sledrun.night_light, sledrun.night_light_comment = _conv(wrpylib.wrvalidators.GermanTristateFloatComment().to_python, value, key)
elif key == u'Beleuchtungstage': sledrun.night_light_days, sledrun.night_light_days_comment = _conv(wrpylib.wrvalidators.UnsignedCommentNone(7).to_python, value, key) # '3 (Montag, Mittwoch, Freitag)'
if version == '1.4':
keys.append(u'Aufstieg möglich')
values.append(wrpylib.wrvalidators.GermanBoolNone().from_python(sledrun.walkup_possible))
- keys.append(u'Gehzeit')
- values.append(wrpylib.wrvalidators.UnsignedNone().from_python(sledrun.walkup_time))
keys.append(u'Aufstieg getrennt')
values.append(wrpylib.wrvalidators.GermanTristateFloatComment().from_python((sledrun.walkup_separate, sledrun.walkup_separate_comment)))
+ keys.append(u'Gehzeit')
+ values.append(wrpylib.wrvalidators.UnsignedNone().from_python(sledrun.walkup_time))
keys.append(u'Aufstiegshilfe')
values.append(wrpylib.wrvalidators.GermanLift().from_python((sledrun.lift, sledrun.lift_details)))
keys.append(u'Beleuchtungsanlage')
# Match Gasthausbox
start, end = wrpylib.mwmarkup.find_template(wikitext, u'Gasthausbox')
if start is None: raise formencode.Invalid(u"No 'Gasthausbox' found", wikitext, None)
- template_name, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ template_title, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
# Process properties
for key, value in properties.iteritems():
return wrpylib.mwmarkup.create_template(u'Gasthausbox', [], keys, values, True)
+def find_template_latlon_ele(wikitext, template_title):
+ """Finds the first occurance of the '{{template_title|47.076207 N 11.453553 E|1890}}' template
+ and returns the tuple (start, end, lat, lon, ele) or (None, None, None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, template_title)
+ if start is None: return (None,) * 5
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ lat, lon = wrpylib.wrvalidators.GeoNone().to_python(params[u'1'].strip())
+ ele = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'2'].strip())
+ return start, end, lat, lon, ele
+
+
+def create_template_latlon_ele(template_title, lat, lon, ele):
+ geo = wrpylib.wrvalidators.GeoNone().from_python((lat, lon))
+ if len(geo) == 0: geo = u' '
+ ele = wrpylib.wrvalidators.UnsignedNone().from_python(ele)
+ if len(ele) == 0: ele = u' '
+ return wrpylib.mwmarkup.create_template(template_title, [geo, ele])
+
+
+def find_template_PositionOben(wikitext):
+ """Same as find_template_latlon_ele with template '{{Position oben|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Position oben')
+
+
+def create_template_PositionOben(lat, lon, ele):
+ return create_template_latlon_ele(u'Position, oben', lat, lon, ele)
+
+
+def find_template_PositionUnten(wikitext):
+ """Same as find_template_latlon_ele with template '{{Position unten|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Position unten')
+
+
+def find_template_unsigned(wikitext, template_title):
+ """Finds the first occurance of the '{{template_title|1890}}' template
+ and returns the tuple (start, end, unsigned_value) or (None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, template_title)
+ if start is None: return (None,) * 3
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ unsigned_value = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'1'].strip())
+ return start, end, unsigned_value
+
+
+def find_template_Hoehenunterschied(wikitext):
+ """Same as find_template_unsigned with template '{{Höhenunterschied|350}}'"""
+ return find_template_unsigned(wikitext, u'Höhenunterschied')
+
+
+def find_template_Bahnlaenge(wikitext):
+ """Same as find_template_unsigned with template '{{Bahnlänge|4500}}'"""
+ return find_template_unsigned(wikitext, u'Bahnlänge')
+
+
+def find_template_Gehzeit(wikitext):
+ """Same as find_template_unsigned with template '{{Gehzeit|60}}'"""
+ return find_template_unsigned(wikitext, u'Gehzeit')
+
+
+def find_template_Forumlink(wikitext):
+ """Same as find_template_unsigned with template '{{Forumlink|26}}'"""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Forumlink')
+ if start is None: return (None,) * 3
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ forumid = params[u'1'].strip()
+ if forumid == u'<nummer einfügen>': unsigned_value = None
+ else: unsigned_value = wrpylib.wrvalidators.UnsignedNone().to_python(forumid)
+ return start, end, unsigned_value
+ # return find_template_unsigned(wikitext, u'Forumlink')
+
+
+def find_template_Parkplatz(wikitext):
+ """Same as find_template_latlon_ele with template '{{Parkplatz|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Parkplatz')
+
+
+def find_template_Haltestelle(wikitext):
+ """Finds the first occurance of the '{{Haltestelle|Ortsname|Haltestellenname|47.076207 N 11.453553 E|1890}}' template
+ and returns the tuple (start, end, city, stop, lat, lon, ele) or (None, None, None, None, None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Haltestelle')
+ if start is None: return (None,) * 7
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ city = wrpylib.wrvalidators.UnicodeNone().to_python(params[u'1'].strip())
+ stop = wrpylib.wrvalidators.UnicodeNone().to_python(params[u'2'].strip())
+ lat, lon = wrpylib.wrvalidators.GeoNone().to_python(params[u'3'].strip())
+ ele = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'4'].strip())
+ return start, end, city, stop, lat, lon, ele
+
+
+def find_all_templates(wikitext, find_func):
+ """Returns a list of return values of find_func that searches for a template.
+ Example:
+ >>> find_all_tempaltes(wikitext, find_template_Haltestelle)
+ Returns an empty list if the template was not found at all.
+ """
+ results = []
+ result = find_func(wikitext)
+ start, end = result[:2]
+ while start is not None:
+ results.append(result)
+ result = find_func(wikitext[end:])
+ if result[0] is None:
+ start = None
+ else:
+ start = result[0] + end
+ end += result[1]
+ result = (start, end) + result[2:]
+ return results
+