jekyll-geocode 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +21 -0
  4. data/README.md +68 -0
  5. data/Rakefile +2 -0
  6. data/lib/jekyll_geocode.rb +114 -0
  7. metadata +83 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a800dd5ba8b97233bf43ad3cb8e47ba0dd2b7f4
4
+ data.tar.gz: 0dcf7e2d742bfeec6e1174ac6ec8e7cde0a7ae55
5
+ SHA512:
6
+ metadata.gz: c448b250b9aa67f690f8613cb459bf8557a6180d98e41aba159fe8ef7f9c979f7949014d8a562843fe4e6c9e1ed063007b3fbbc5b2fb2152773c2df87f792051
7
+ data.tar.gz: 8a3ca5bef05ccc9de67de4c8d4eeba1f6306a37804f8a284ee81e267210d594a2e032dcce029479929f856bebaf6d86265f7e8994e1ee20d22f3d0609b82b2f1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-post-files.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nicolas Hoizey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Jekyll Geocode
2
+
3
+ Making a interactive maps with adresses is impossible you need the coordinates.
4
+ Making a file writing by redactors with directly the coordinates of your data is impossible or too slow.
5
+
6
+ So you need a processus for generating coordinates form addresses by calling a service.
7
+
8
+ For speed reasons you‘d rather writing the coordinates in files during the build. You will probably generate a JSON file with coordinates in your HTML page displaying map.
9
+
10
+ I took the [Nominatim](https://nominatim.openstreetmap.org/) open source service from Open Street Map for calling the data.
11
+
12
+ ## Example of YAML : _data/members.yml
13
+
14
+ ```yaml
15
+ - name: "John Doe"
16
+ address: "rue Mendès France"
17
+ postcode: 76190
18
+ city: "Yvetot"
19
+ region: "normandy"
20
+ country: "france"
21
+ - name: "Samuel Le Bihan"
22
+ address: "place du général de Gaulle France"
23
+ postcode: 76000
24
+ city: "Rouen"
25
+ region: "Normandy"
26
+ country: "france"
27
+ ```
28
+
29
+ > For key matching, avoid accent in name or create a key in members.yml (ex: geoname) without space and accent.
30
+
31
+ ## Example of configuration _config.yml
32
+
33
+ ```yaml
34
+ jekyll_geocode:
35
+ file-name: members.yml # Name of the YML store inside _data with a list of datas (required)
36
+ file-path: _data # Path of the YML file, folder of your data (_data) by default (optional)
37
+ name: name # Name of the field from the YML that will gave the name of the generated file (the name will be downcase and space replace by a dash) (required)
38
+ address: address # Name of the address (street + city) or only street field (if city field exists) from the YML (required)
39
+ postcode: postcode # Postcode from the YML (optional)
40
+ city: city # Name of the town field from the YML, if you have a separated field address and city (optional)
41
+ region: region # Name of the region, county or state or all in the same field from the YML (optional)
42
+ country: country # Name of the country from the YML (optional)
43
+ cache: true # Test if a file already exist
44
+ outputfile: place.yml # Give the name of the file for an yml output (otherwise it will be JSON) <= usefull for jekyll-map
45
+ ```
46
+ > The service is very slow (when you have lot of entries) but you can generate files with the cache option. Be carefull, ignore this files in your git tracking files !
47
+
48
+ ## Example of loop with JSON : map.html with google map
49
+
50
+ ```liquid
51
+ {% for row in site.data.members | sort: 'name' %}
52
+ {% assign geoname = row.name | replace: ' ', '-' | downcase %}
53
+ var point = new google.maps.LatLng({% for coordinates in site.data.[geoname] %}{{ coordinates.lat }}, {{ coordinates.lon }}{% endfor %});
54
+ {% endfor %}
55
+ ```
56
+
57
+ ## Example of loop with YAML : map.html with google map
58
+ ```liquid
59
+ {% for row in site.data.members | sort: 'name' %}
60
+ {% assign geoname = row.name | replace: ' ', '-' | downcase %}
61
+ {% assign places = site.data.place | where:"title", geoname %}
62
+ {% for coordinates in places %}
63
+ var point = new google.maps.LatLng({{ coordinates.location.latitude }}, {{ coordinates.location.longitude }});
64
+ {% endfor %}
65
+ {% endfor %}
66
+ ```
67
+
68
+ This plugin is used in a production website and working well. You are invited for improvement proposals.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,114 @@
1
+ require "rubygems"
2
+ require 'json'
3
+ require 'yaml'
4
+ require 'open-uri'
5
+ require "i18n"
6
+
7
+ module Jekyll_Get
8
+ class Generator < Jekyll::Generator
9
+ safe true
10
+ priority :highest
11
+
12
+ def generate(site)
13
+
14
+ #regEx for removing empty line
15
+ regEx = /^[\s]*$\n/
16
+
17
+ #force locale
18
+ I18n.config.available_locales = :en
19
+
20
+ #Get Geo service
21
+ geo_service = 'https://nominatim.openstreetmap.org/?format=json&q='
22
+
23
+ #Get Config
24
+ config = site.config['jekyll_geocode']
25
+
26
+ if !config
27
+ return
28
+ end
29
+ if !config.kind_of?(Array)
30
+ filename = site.config['jekyll_geocode']['file-name']
31
+ filepath = site.config['jekyll_geocode']['file-path']
32
+ outputfile = site.config['jekyll_geocode']['outputfile']
33
+ geo_name = site.config['jekyll_geocode']['name']
34
+ geo_address = site.config['jekyll_geocode']['address']
35
+ geo_postcode = site.config['jekyll_geocode']['postcode']
36
+ geo_city = site.config['jekyll_geocode']['city']
37
+ geo_region = site.config['jekyll_geocode']['region']
38
+ geo_country = site.config['jekyll_geocode']['country']
39
+ end
40
+
41
+ # Define data source
42
+ if !filepath
43
+ data_source = (site.config['data_source'])
44
+ else
45
+ data_source = (filepath)
46
+ end
47
+
48
+ #Path
49
+ path_yaml = "#{data_source}/#{outputfile}"
50
+
51
+ # if File.file?(path_yaml) && outputfile
52
+ # File.open(path_yaml, 'w') {|file| file.truncate(0) }
53
+ # end
54
+
55
+ # Load YML file
56
+ members = YAML.load_file("#{data_source}/#{filename}")
57
+
58
+ # Loop YML file
59
+ members.each do |d|
60
+ # Test if a JSON file exists for performance issues
61
+ if !File.file?("#{data_source}/#{d[geo_name]}.json")
62
+ geo_name_field = d[geo_name].downcase.tr(" ", "-")
63
+ if d[geo_postcode]
64
+ geo_postcode_field = ",#{d[geo_postcode]}"
65
+ end
66
+ if d[geo_city]
67
+ geo_city_field = ",#{d[geo_city]}"
68
+ end
69
+ if d[geo_region]
70
+ geo_region_field = ",#{d[geo_region]}"
71
+ end
72
+ if d[geo_country]
73
+ geo_country_field = ",#{d[geo_country]}"
74
+ end
75
+ json = URI.encode("#{geo_service}#{d[geo_address]}#{geo_postcode_field}#{geo_city_field}#{geo_region_field}#{geo_country_field}&limit=1")
76
+ source = JSON.load(open(json))
77
+
78
+ # Loop for an YML output
79
+ if outputfile
80
+ source.each do |coordinates|
81
+ data = [ "title" => "#{d[geo_name]}", "url" => "#places-01", "data_set" => "01", "location" => { "latitude" => "#{coordinates["lat"]}","longitude" => "#{coordinates["lon"]}" } ]
82
+ data_yml = data.to_yaml
83
+ # Test if there is any yaml files and create file
84
+ if !File.file?(path_yaml)
85
+ File.open(path_yaml, "w") {|f| f.write(data_yml) }
86
+ end
87
+ # Test if there is yaml files and add data recursively
88
+ if File.file?(path_yaml)
89
+ File.open(path_yaml, 'a') { |f|
90
+ data_yml_simple = data_yml.gsub("---", "").gsub(regEx, '')
91
+ f.puts data_yml_simple
92
+ }
93
+ # Load YML file and remove duplicate entries
94
+ file_yaml = YAML.load(File.open(path_yaml))
95
+ file_yaml_uniq = file_yaml.uniq { |s| s.first }
96
+ File.open(path_yaml, "w") {|f| f.write(file_yaml_uniq.to_yaml) }
97
+ end
98
+ end
99
+ # Loop for an JSON output
100
+ else
101
+ site.data[geo_name_field] = source
102
+ #Create a JSON files if cache is enabled
103
+ if site.config['jekyll_geocode']['cache']
104
+ path_json = "#{data_source}/#{geo_name_field}.json"
105
+ open(path_json, 'wb') do |file|
106
+ file << JSON.generate(site.data[geo_name_field])
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-geocode
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Bertrand Keller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.12'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.12'
47
+ description: " Geocode addresses from a YAML for drawing maps (with https://nominatim.openstreetmap.org)\n"
48
+ email:
49
+ - bertrand.keller@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/jekyll_geocode.rb
59
+ homepage: https://bertrandkeller.github.io/jekyll-geocode/
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.6.7
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Geocode addresses from a YAML for drawing maps
83
+ test_files: []