From ab1f42008689a8a61f6f8ca2ec76dfa315d4c91a Mon Sep 17 00:00:00 2001 From: NN Solex Date: Tue, 2 May 2023 16:48:51 +0300 Subject: [PATCH] init --- README.md | 27 +++++++++ geoblog/__init__.py | 1 + geoblog/geoblog.py | 81 +++++++++++++++++++++++++++ templates/archives.html | 118 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 README.md create mode 100644 geoblog/__init__.py create mode 100644 geoblog/geoblog.py create mode 100644 templates/archives.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f93baa --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# GeoBlog Plugin + +This plugin scans for `Location: , ` metadata in articles and displays them on a map using the included `archives.html` template. It also writes `articles.gpx` and `articles.kml` files with the same information for offline GPS devices and Apps. + +## Installation + +Copy the `geoblog` folder to your local Pelican plug-ins folder and activate the plug-in in your `pelicanconf.py` file: + +``` +PLUGINS = ['geoblog'] +``` + +Copy the `archives.html` file to your Pelican theme's `templates` folder. + +If you want to use it for `categories` or other `index.html` derived template files, make sure to adjust the loop accordingly: + +``` +{% for article in articles %} +``` + +## Use + +Add location coordinates as decimal latitude and longitude values to your article metadata header. The map will display markers that show a pop up with the clickable title of the article when clicked. + +``` +Location: 12.009621864420991, 79.81141615530888 +``` \ No newline at end of file diff --git a/geoblog/__init__.py b/geoblog/__init__.py new file mode 100644 index 0000000..b85c1ac --- /dev/null +++ b/geoblog/__init__.py @@ -0,0 +1 @@ +from .geoblog import * \ No newline at end of file diff --git a/geoblog/geoblog.py b/geoblog/geoblog.py new file mode 100644 index 0000000..85cb54b --- /dev/null +++ b/geoblog/geoblog.py @@ -0,0 +1,81 @@ +from pelican import signals +from xml.dom import minidom + +settings = {} +collection = {} +gpx_root = ''' +''' +kml_root = ''' +''' + +def init(self): + settings['files'] = self.settings.get('OUTPUT_PATH') + +def generate_latlon(generator): + for article in generator.articles: + if "location" in article.metadata: + lat, lon = [float(i) for i in article.metadata['location'].replace(' ', '').split(',')] + article.metadata['lat'] = lat + article.metadata['lon'] = lon + +def make_files(void): + print(collection) + makeGPX() + makeKML() + +def makeKML(): + if len(collection) > 0: + root = minidom.parseString(kml_root) + folder = root.createElement('name') + foldername = root.createTextNode( 'articles' ) + folder.appendChild( foldername ) + root.childNodes[0].childNodes[0].childNodes[0].appendChild( folder ) + for place in collection: + placemark = root.createElement('Placemark') + name = root.createElement('name') + title = root.createTextNode( collection[place]['name'] + ' (' + collection[place]['url'] +')') + name.appendChild( title ) + placemark.appendChild( name ) + point = root.createElement('Point') + coordinates = root.createElement('coordinates') + lat, lon = collection[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(settings['files'] + '/articles.kml', 'w') as xml_file: + root.writexml(xml_file, indent="", addindent=" ", newl='\n') + +def makeGPX(): + if len(collection) > 0: + root = minidom.parseString(kml_root) + for place in collection: + lat, lon = collection[place]['coordinates'].replace(' ', '').split(',') + wpt = root.createElement('wpt') + wpt.setAttribute('lat', lat) + wpt.setAttribute('lon', lon) + name = root.createElement('name') + title = root.createTextNode( collection[place]['name'] + ' (' + collection[place]['url'] +')' ) + name.appendChild( title ) + wpt.appendChild( name ) + root.childNodes[0].appendChild( wpt ) + with open(settings['files'] + '/articles.gpx', 'w') as xml_file: + root.writexml(xml_file, indent="", addindent=" ", newl='\n') + +def geodata(generator): + for article in generator.articles: + if "location" in article.metadata: + collection.update({ + article.slug : { + "url" : generator.settings['SITEURL'] + '/' + article.url, + "name": article.title, + "coordinates": article.metadata["location"], + } + }) + +def register(): + signals.initialized.connect(init) + signals.article_generator_finalized.connect(generate_latlon) + signals.article_generator_finalized.connect(geodata) + signals.finalized.connect(make_files) \ No newline at end of file diff --git a/templates/archives.html b/templates/archives.html new file mode 100644 index 0000000..05cc1a2 --- /dev/null +++ b/templates/archives.html @@ -0,0 +1,118 @@ + + + + + + + + {{ SITENAME }} + + + + + + + + + + +
+ + +