This commit is contained in:
NN Solex 2023-05-02 16:48:51 +03:00
commit ab1f420086
4 changed files with 227 additions and 0 deletions

27
README.md Normal file
View file

@ -0,0 +1,27 @@
# GeoBlog Plugin
This plugin scans for `Location: <latitude>, <longitude>` 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
```

1
geoblog/__init__.py Normal file
View file

@ -0,0 +1 @@
from .geoblog import *

81
geoblog/geoblog.py Normal file
View file

@ -0,0 +1,81 @@
from pelican import signals
from xml.dom import minidom
settings = {}
collection = {}
gpx_root = '''<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="nfc.flux.vision"></gpx>'''
kml_root = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"><Document><Folder></Folder></Document></kml>'''
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)

118
templates/archives.html Normal file
View file

@ -0,0 +1,118 @@
<!DOCTYPE html>
<html lang="{{ DEFAULT_LANG }}">
<head>
<base target="_top">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ SITENAME }}</title>
<link rel="shortcut icon" type="image/x-icon" href="/images/favicon.ico" />
<link rel="stylesheet" href="{{ SITEURL }}/theme/local.css">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.3/dist/leaflet.css" integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=" crossorigin=""/>
<style>
html, body { height: 100%; margin: 0; padding: 0; }
#map {
height: 100%;
width: 100vw;
}
#map img {
border: none
}
.info {
padding: 6px 8px;
background: white;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.info h4 {
font-size: 1.6em;
margin: 0 0 5px;
color: #777;
}
.info span {
line-height: 18px;
vertical-align: top;
}
</style>
<script src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js" integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=" crossorigin=""></script>
</head>
<body>
<script>
var mapArticles = {
"type": "FeatureCollection",
"title": "{{ SITENAME }}",
"features": [
{% for article in dates %}
{% if article.metadata['lat'] and article.metadata['lon'] %}
{
"type": "Feature",
"id": "{{ article.slug }}",
"geometry": {
"type": "Point",
"coordinates": [{{ article.metadata['lon'] }}, {{ article.metadata['lat'] }}]
},
"properties": {
"title": "{{ article.title }}",
"url": "{{ SITEURL }}/{{ article.url }}"
},
},
{% endif %}
{% endfor %}
]
};
</script>
<div id='map'></div>
<script>
const showArticles = L.geoJSON(mapArticles, {
pointToLayer(feature, latlng) { return L.marker(latlng);},
onEachFeature
});
const map = L.map('map').fitBounds(showArticles.getBounds());
const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
showArticles.addTo(map);
function onEachFeature(feature, layer) {
let popupContent = ``;
if (feature.properties && feature.properties.title) {
popupContent += '<a href="'+feature.properties.url+'">' + feature.properties.title + '</a>';
}
layer.bindPopup(popupContent);
}
var info = L.control({position: 'topright'});
var download = L.control({position: 'bottomleft'});
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this._div.innerHTML = '<h4>' + mapArticles["title"] + '</h4>';
return this._div;
};
download.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this._div.innerHTML = 'Download <a href="{{ SITEURL }}/articles.gpx">GPX</a> <a href="{{ SITEURL }}/articles.kml">KML</a>';
return this._div;
};
info.addTo(map);
download.addTo(map);
</script>
</body>
</html>