from pelican import signals from xml.dom import minidom collection = {} gpx_root = ''' ''' kml_root = ''' ''' def make_files(void): makeGPX() makeKML() def makeKML(): if len(collection) > 0: for category in collection: root = minidom.parseString(kml_root) folder = root.createElement('name') foldername = root.createTextNode( collection[category]["name"] ) folder.appendChild( foldername ) root.childNodes[0].childNodes[0].childNodes[0].appendChild( folder ) for place in collection[category]["points"]: placemark = root.createElement('Placemark') name = root.createElement('name') title = root.createTextNode( place['name'] ) name.appendChild( title ) placemark.appendChild( name ) point = root.createElement('Point') coordinates = root.createElement('coordinates') lat, lon = place['coordinates'].replace(' ', '').split(',') latlon = root.createTextNode( lon + ',' + lat) coordinates.appendChild( latlon ) point.appendChild( coordinates ) placemark.appendChild( point ) root.childNodes[0].childNodes[0].childNodes[0].appendChild( placemark ) with open('output/projects/' + category + '.kml', 'w') as xml_file: root.writexml(xml_file, indent="", addindent=" ", newl='\n') def makeGPX(): if len(collection) > 0: for category in collection: root = minidom.parseString(kml_root) for place in collection[category]["points"]: lat, lon = place['coordinates'].replace(' ', '').split(',') wpt = root.createElement('wpt') wpt.setAttribute('lat', lat) wpt.setAttribute('lon', lon) name = root.createElement('name') title = root.createTextNode( place['name'] ) name.appendChild( title ) wpt.appendChild( name ) root.childNodes[0].appendChild( wpt ) with open('output/projects/' + category + '.gpx', 'w') as xml_file: root.writexml(xml_file, indent="", addindent=" ", newl='\n') def geodata(generator): for category, articles in generator.categories: locations = [] for article in articles: if "location" in article.metadata: locations.append({ "name": article.title, "coordinates": article.metadata["location"] }) collection.update({ category.slug : { "name" : category.name, "points" : locations }}) def register(): signals.article_generator_finalized.connect(geodata) signals.finalized.connect(make_files)