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)
-
- 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'))
+ 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:
+ 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 = []
paths = []
- lines = content.split("\n")
+ lines = wikitext[content:endtag].split("\n")
i = 0
while i < len(lines):
line = lines[i].strip()
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)