(11.269322, 47.113421, None, None),
(11.269979, 47.11277, None, None),
(11.271119, 47.112408, None, None)])]
- center, zoom, coords, paths, start, end = wrpylib.mwmarkup.parse_googlemap(wikitext, detail=True)
- assert start == 5
- assert end == 344
- result = wrpylib.mwmarkup.parse_googlemap(wikitext.replace('<googlemap', '|googlemap'), detail=True)
- assert result is None
+ try:
+ result = wrpylib.mwmarkup.parse_googlemap(wikitext.replace('<googlemap', '|googlemap'))
+ assert False
+ except wrpylib.mwmarkup.ParseError:
+ pass
import xml.etree.ElementTree
+class ParseError(RuntimeError):
+ """Exception used by some of the functions"""
+ pass
+
+
def find_template(wikitext, template_title):
"""Returns the tuple (start, end) of the first occurence of the template '{{template ...}} within wikitext'.
(None, None) is returned if the template is not found.
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.
+def parse_googlemap(wikitext):
+ """Parses the (unicode) u'<googlemap ...>content</googlemap>' of the googlemap extension.
+ If wikitext does not contain the <googlemap> tag or if the <googlemap> tag contains
+ invalid formatted lines, a ParseError is raised.
+ Use find_tag(wikitext, 'googlemap') to find the googlemap tag within an arbitrary
+ wikitext before using this function.
: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">
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."""
+ coords is again a list of (lot, lat, symbol, title) tuples."""
def is_coord(line):
"""Returns True if the line contains a coordinate."""
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)
+ return ParseError(u'Could not parse line ' + line)
start, content, endtag, end = find_tag(wikitext, 'googlemap')
+ if start is None:
+ raise ParseError('<googlemap> tag not found.')
if content is None:
- return None
- gm = xml.etree.ElementTree.XML((wikitext[start:content]+wikitext[endtag:end]).encode('UTF8'))
+ xml_only = wikitext[start:endtag]
+ else:
+ xml_only = wikitext[start:content]+wikitext[endtag:end]
+
+ try:
+ gm = xml.etree.ElementTree.XML(xml_only.encode('UTF8'))
+ except xml.etree.ElementTree.ParseError as e:
+ row, column = e.position
+ raise ParseError("XML parse error in <googlemap ...>.")
zoom = gm.get('zoom')
lon = gm.get('lon')
lat = gm.get('lat')
coords.append((lon, lat, symbol, title))
continue
- raise RuntimeError(u'Unknown line syntax: ' + line)
- if detail:
- return (center, zoom, coords, paths, start, end)
+ raise ParseError(u'Unknown line syntax: ' + line)
return (center, zoom, coords, paths)