Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiei committed Jan 6, 2016
1 parent 4403c56 commit 78c14b3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
@@ -0,0 +1,34 @@
# hexo-generator-postmap

Generates a locations.xml and page showing an interactive Google map embed with your posts/pages marked on the map with pop up links to your posts/pages.

This package is still pretty rough around the edges and is not recommended for anything important yet.

You should tag posts by putting the lat and long in the location metadata:

```yaml
location: [50.092953, 14.445974]
```

## Install

``` bash
$ npm install hexo-generator-postmap --save
```

- Hexo 3: 0.1.x

## Options

You can configure this plugin in `_config.yml`.

``` yaml
postmap:
xmlpath: 'locations.xml',
mappagestub: 'mapofposts',
mappagetitle: 'Map of posts'
```

- **xmlpath** - Path to your locations.xml document.
- **mappagestub** - The url friendly name of your map page without slashes (eg mapofposts)
- **mappagetitle** - The human friendly name of your map page, used as the hexo title of page.
14 changes: 14 additions & 0 deletions index.js
@@ -0,0 +1,14 @@
var merge = require('utils-merge');
var pathFn = require('path');

var config = hexo.config.postmap = merge({
xmlpath: 'locations.xml',
mappagestub: 'postmap',
mappagetitle: 'Map of posts'
}, hexo.config.postmap);

if (!pathFn.extname(config.locationpath)){
config.locationpath += '.xml';
}

hexo.extend.generator.register('postmap', require('./lib/generator'));
39 changes: 39 additions & 0 deletions lib/generator.js
@@ -0,0 +1,39 @@
var ejs = require('ejs');
var pathFn = require('path');
var fs = require('fs');

var locationsSrc = pathFn.join(__dirname, '../locations.ejs');
var locationsTmpl = ejs.compile(fs.readFileSync(locationsSrc, 'utf8'));

var mapEmbedSrc = pathFn.join(__dirname, '../mapembed.ejs');
var mapEmbedTmpl = ejs.compile(fs.readFileSync(mapEmbedSrc, 'utf8'));

module.exports = function(locals) {
var config = this.config;

var posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
.filter(function(post) {
return post.location != null;
});

var xml = locationsTmpl({
config: config,
posts: posts
});

return [{
path: config.postmap.xmlpath,
data: xml
}, {
path: config.postmap.mappagestub + '/index.html',
data: {
'title': config.postmap.mappagetitle,
'content': mapEmbedTmpl({
'url': encodeURI(config.root + config.postmap.xmlpath)
}),
'description': 'A map of geo-tagged posts on ' + config.title
},
layout: ['page']
}];

};
13 changes: 13 additions & 0 deletions locations.ejs
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<locations>
<% var url = config.url + config.root %>
<% posts.forEach(function(post){ %>
<location
url="<%- encodeURI(url + post.path) %>"
lastmod="<%= post.updated.toISOString() || post.date.toISOString() %>"
lat="<%= post.location[0] %>"
lng="<%= post.location[1] %>"
title="<%= post.title %>">
</location>
<% }) %>
</locations>
68 changes: 68 additions & 0 deletions mapembed.ejs
@@ -0,0 +1,68 @@

<script src="https://maps.googleapis.com/maps/api/js"></script>

<script type="text/javascript">
function initialise() {
myLatlng = new google.maps.LatLng(54.559323, -3.321304);
mapOptions = {
zoom: 1,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
geocoder = new google.maps.Geocoder();
infoWindow = new google.maps.InfoWindow;
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
xmlUrl = "<%= url %>";
loadMarkers();
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
//request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function loadMarkers() {
map.markers = map.markers || []
downloadUrl(xmlUrl, function(data) {
var xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName("location");
for (var i = 0; i < markers.length; i++) {
var title = markers[i].getAttribute("title");
var url = markers[i].getAttribute("url");
var lastmod = markers[i].getAttribute("lastmod");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<div class='infowindow'><b>" + title + "</b> <br/><a href='" + url + "'>" + url + "</a><br/> " + lastmod + " <br/></div>";
var marker = new google.maps.Marker({
map: map,
position: point,
title: title
});
map.markers.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
google.maps.event.addDomListener(window, 'load', initialise);
</script>

<div id="map-canvas" style="height: 500px; width: 90%;"></div>
30 changes: 30 additions & 0 deletions package.json
@@ -0,0 +1,30 @@
{
"name": "hexo-generator-postmap",
"version": "0.1.0",
"description": "A plugin for the Hexo static site generator that generates an archive page with geo-tagged posts and pages on an interactive google map",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"hexo",
"maps",
"google",
"plugin"
],
"author": {
"name": "Jamie I",
"url" : "http://jamiei.com/"
},
"dependencies": {
"ejs": "^1.0.0",
"utils-merge": "^1.0.0"
},
"repository" :
{
"type" : "git",
"url" : "https://github.com/jamiei/hexo-generator-postmap.git"
},
"license": "MIT",
"readme": "README.md"
}

0 comments on commit 78c14b3

Please sign in to comment.