- # Find start tag
- regexp_starttag = re.compile(u"<{0}.*?(/?)>".format(tagname), re.DOTALL)
- match_starttag = regexp_starttag.search(wikitext, pos)
- if match_starttag is None:
- return None, None, None, None
-
- # does the tag have content?
- if len(match_starttag.group(1)) == 1: # group(1) is either '' or '/'.
- # single tag
- return match_starttag.start(), None, None, match_starttag.end()
-
- # tag with content
- regexp_endtag = re.compile(u'</{0}>'.format(tagname), re.DOTALL)
- match_endtag = regexp_endtag.search(wikitext, match_starttag.end())
- if match_endtag is None:
- # No closing tag - error in wikitext
- return None, None, None, None
- return match_starttag.start(), match_starttag.end(), match_endtag.start(), match_endtag.end()
-
-
-def parse_googlemap(wikitext, detail=False):
- """Parses the (unicode) u'<googlemap ...>content</googlemap>' of the googlemap extension
- out of a page. If wikitext does not contain the googlemap extension text None is returned.
- If the googlemap contains invalid formatted lines, a RuntimeError is raised.
-
- :param wikitext: wikitext containing the template. Example:
- :param detail: bool. If True, start and end position of <googlemap>...</googlemap> is
- returned additionally.
-
- wikitext = '''
- <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15">
- (Parkplatz)47.114958,11.266026
- Parkplatz
-
- (Gasthaus) 47.114715, 11.266262, Alt Bärnbad (Gasthaus)
- 6#FF014E9A
- 47.114715,11.266262
- 47.114135,11.268381
- 47.113421,11.269322
- 47.11277,11.269979
- 47.112408,11.271119
- </googlemap>
- '''
- :returns: the tuple (center, zoom, coords, paths).
- center is the tuple (lon, lat) of the google maps or (None, None) if not provided
- zoom is the google zoom level as integer or None if not provided
- coords is a list of (lon, lat, symbol, title) tuples.
- paths is a list of (style, coords) tuples.
- coords is again a list of (lot, lat, symbol, title) tuples.
- If detail is True, (center, zoom, coords, paths, start, end) is returned."""
-
- def is_coord(line):
- """Returns True if the line contains a coordinate."""
- match = re.search('[0-9]{1,2}\.[0-9]+, ?[0-9]{1,2}\.[0-9]+', line)
- return not match is None
-
- def is_path(line):
- """Returns True if the line contains a path style definition."""
- match = re.match('[0-9]#[0-9a-fA-F]{8}', line)
- return not match is None
-
- def parse_coord(line):
- """Returns (lon, lat, symbol, title). If symbol or text is not present, None is returned."""
- match = re.match(u'\(([^)]+)\) ?([0-9]{1,2}\.[0-9]+), ?([0-9]{1,2}\.[0-9]+), ?(.*)', line)
- if not match is None: return (float(match.group(3)), float(match.group(2)), match.group(1), match.group(4))
- match = re.match(u'\(([^)]+)\) ?([0-9]{1,2}\.[0-9]+), ?([0-9]{1,2}\.[0-9]+)', line)
- if not match is None: return (float(match.group(3)), float(match.group(2)), match.group(1), None)
- match = re.match(u'([0-9]{1,2}\.[0-9]+), ?([0-9]{1,2}\.[0-9]+), ?(.*)', line)
- if not match is None: return (float(match.group(2)), float(match.group(1)), None, match.group(3))
- match = re.match(u'([0-9]{1,2}\.[0-9]+), ?([0-9]{1,2}\.[0-9]+)', line)
- if not match is None: return (float(match.group(2)), float(match.group(1)), None, None)
- return RuntimeError(u'Could not parse line ' + line)
-
- regexp = re.compile(u"(<googlemap[^>]*>)(.*?)(</googlemap>)", re.DOTALL)
- match = regexp.search(wikitext)
- if match is None: return None
- start = match.start()
- end = match.end()
- content = match.group(2)
- gm = xml.etree.ElementTree.XML((match.group(1)+match.group(3)).encode('UTF8'))
- zoom = gm.get('zoom')
- lon = gm.get('lon')
- lat = gm.get('lat')
- if not zoom is None: zoom = int(zoom)
- if not lon is None: lon = float(lon)
- if not lat is None: lat = float(lat)
- center = (lon, lat)
-
- coords = []
- paths = []
- lines = content.split("\n")
- i = 0
- while i < len(lines):
- line = lines[i].strip()
- i += 1
-
- # Skip whitespace
- if len(line) == 0: continue
-
- # Handle a path
- if is_path(line):
- match = re.match(u'([0-9]#[0-9a-fA-F]{8})', line)
- style = match.group(1)
- local_coords = []
- while i < len(lines):
- line = lines[i].strip()
- i += 1
- if is_path(line):
- i -= 1
- break
- if is_coord(line):
- lon, lat, symbol, title = parse_coord(line)
- local_coords.append((lon, lat, symbol, title))
- paths.append((style, local_coords))
- continue
-
- # Handle a coordinate
- if is_coord(line):
- lon, lat, symbol, title = parse_coord(line)
- while i < len(lines):
- line = lines[i].strip()
- i += 1
- if is_path(line) or is_coord(line):
- i -= 1
- break
- if len(line) > 0 and title is None: title = line
- coords.append((lon, lat, symbol, title))
- continue
-
- raise RuntimeError(u'Unknown line syntax: ' + line)
- if detail:
- return (center, zoom, coords, paths, start, end)
- return (center, zoom, coords, paths)
-
+ if keylen is None:
+ shown_keys = [len(param.name.strip()) for param in template.params if param.showkey]
+ keylen = max(shown_keys) if shown_keys else 0
+ template.name = '{}\n'.format(template.name.strip())
+ for param in template.params:
+ if param.showkey:
+ param.name = ' {{:{}}} '.format(keylen).format(param.name.strip())
+ value = param.value.strip()
+ if len(value) > 0:
+ param.value = ' {}\n'.format(value)
+ else:
+ param.value = '\n'
+
+
+def format_template_oneline(template):
+ """Formats a template like this: {{template_name|param| }}
+ (whitespace is stripped and empty parameters are replaced with one space)."""
+ template.name = template.name.strip()
+ for param in template.params:
+ if param.showkey:
+ param.name = param.name.strip()
+ value = param.value.strip()
+ if value == '':
+ value = ' '
+ param.value = value
+
+
+def dbkey_to_title(value):
+ """Converts a article database key to a article title. Private function secureAndSplit() of the Title class
+ on line 3316 of includes/Title.php says:
+ $this->mTextform = str_replace( '_', ' ', $this->mDbkeyform );
+ No check for None because a missing title is an error."""
+ return value.replace('_', ' ')