Fix: escape XML special chars when generating GPX from Park4Night bookmarks
This commit is contained in:
parent
3c138f3820
commit
5b328e4d63
1 changed files with 16 additions and 4 deletions
|
|
@ -6,6 +6,18 @@ import os
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from xml.sax.saxutils import escape as _xml_escape
|
||||||
|
|
||||||
|
def xml_escape(text):
|
||||||
|
"""
|
||||||
|
Escape text for use inside XML element text nodes.
|
||||||
|
|
||||||
|
Important: At least '&', '<', '>' must be escaped to keep GPX well-formed.
|
||||||
|
We also escape quotes defensively.
|
||||||
|
"""
|
||||||
|
if text is None:
|
||||||
|
return ''
|
||||||
|
return _xml_escape(str(text), entities={'"': '"', "'": '''})
|
||||||
|
|
||||||
def create_gpx(places, folder_name, output_file='places.gpx'):
|
def create_gpx(places, folder_name, output_file='places.gpx'):
|
||||||
"""Create a GPX file from the collected places."""
|
"""Create a GPX file from the collected places."""
|
||||||
|
|
@ -29,12 +41,12 @@ def create_gpx(places, folder_name, output_file='places.gpx'):
|
||||||
waypoints.append(waypoint_template.format(
|
waypoints.append(waypoint_template.format(
|
||||||
lat=place['coordinates']['lat'],
|
lat=place['coordinates']['lat'],
|
||||||
lon=place['coordinates']['lng'],
|
lon=place['coordinates']['lng'],
|
||||||
name=place['name'],
|
name=xml_escape(place['name']),
|
||||||
desc=place['description'] or ''
|
desc=xml_escape(place['description'] or '')
|
||||||
))
|
))
|
||||||
|
|
||||||
gpx_content = gpx_template.format(
|
gpx_content = gpx_template.format(
|
||||||
folder_name=folder_name,
|
folder_name=xml_escape(folder_name),
|
||||||
timestamp=datetime.utcnow().isoformat(),
|
timestamp=datetime.utcnow().isoformat(),
|
||||||
waypoints='\n'.join(waypoints)
|
waypoints='\n'.join(waypoints)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue