jekyll-maps 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d991367839c14b8f67ff64821219fc0ebcf9290
4
- data.tar.gz: ab2ab30d54e9f1ed120f377f0350cc53d5f52d12
3
+ metadata.gz: 0e93f2ab559d3a815471eb4785a05b30239a88b7
4
+ data.tar.gz: 742dd19383883194546ff1d121ef1bce0209c28e
5
5
  SHA512:
6
- metadata.gz: 34ee237acea3325e5c7fa62defb1f41b829586bafa05135c917cd54e8ab085fe98fdf177cd1f1299b8467843777c24662ad841331cabe119e94371310538ec62
7
- data.tar.gz: e31ce264d925ae834ae80e0ae9fb2e0b8b1cee5ab15cf94bd1a38c0e4d0b074521aa2f3095c1f1cb619304c470d52b206df9317ae8377ad831fa8a5ecf1a4bfb
6
+ metadata.gz: 16e36155f5f2f59d08b2e6a93dd2378aade5d95897a161b6849778b86c41c08916f06e0b38d3dec349c99a86f4120eb2604ed7a44682562dc2143c4dfb03be69
7
+ data.tar.gz: 0b203611ebfef3cf6f7d970ae5bc81dcb62b0e653c6eb3157357978569fa30283e091b8b2d5391b226c98f6d9a178c3b19fdbbcfd9e79f93831c9c43457fb90b
data/.codeclimate.yml ADDED
@@ -0,0 +1,26 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ - javascript
9
+ fixme:
10
+ enabled: true
11
+ rubocop:
12
+ enabled: true
13
+ eslint:
14
+ enabled: true
15
+ ratings:
16
+ paths:
17
+ - "**.inc"
18
+ - "**.js"
19
+ - "**.jsx"
20
+ - "**.module"
21
+ - "**.php"
22
+ - "**.py"
23
+ - "**.rb"
24
+ exclude_paths:
25
+ - script/
26
+ - spec/
data/History.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 1.1.0 / 2016-07-19
2
+
3
+ * add multiple maps to single page
4
+ * load external JavaScript asynchronously
5
+ * configure map element's id, CSS class and dimensions
6
+
1
7
  ## 1.0.2 / 2016-07-18
2
8
 
3
9
  * fix script loading
data/README.md CHANGED
@@ -4,5 +4,6 @@
4
4
  [![Build Status](https://travis-ci.org/ayastreb/jekyll-maps.svg?branch=master)](https://travis-ci.org/ayastreb/jekyll-maps)
5
5
  [![Code Climate](https://codeclimate.com/github/ayastreb/jekyll-maps/badges/gpa.svg)](https://codeclimate.com/github/ayastreb/jekyll-maps)
6
6
  [![Test Coverage](https://codeclimate.com/github/ayastreb/jekyll-maps/badges/coverage.svg)](https://codeclimate.com/github/ayastreb/jekyll-maps/coverage)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/ayastreb/jekyll-maps.svg)](https://gemnasium.com/github.com/ayastreb/jekyll-maps)
7
8
 
8
9
  Google Maps support in Jekyll blog to easily embed maps with posts' locations
data/jekyll-maps.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_dependency "jekyll", "~> 3.0"
20
20
 
21
- spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rake", "~> 11.0"
22
22
  spec.add_development_dependency "rspec", "~> 3.0"
23
23
  spec.add_development_dependency "rubocop", "~> 0.41"
24
24
  end
@@ -0,0 +1,89 @@
1
+ var jekyllMaps = (function () {
2
+ 'use strict';
3
+
4
+ return {
5
+ data: [],
6
+ maps: [],
7
+ initializeMap: initializeMap,
8
+ initializeCluster: initializeCluster,
9
+ register: register,
10
+ render: render,
11
+ processCluster: processCluster
12
+ };
13
+
14
+ /**
15
+ * Setup Google Maps options and call renderer.
16
+ */
17
+ function initializeMap() {
18
+ this.options = {
19
+ mapTypeId: google.maps.MapTypeId.ROADMAP,
20
+ center: new google.maps.LatLng(0, 0),
21
+ zoom: 3
22
+ }
23
+ this.mapReady = true;
24
+ this.render();
25
+ }
26
+
27
+ /**
28
+ * Register map data to be rendered once Google Maps API is loaded.
29
+ *
30
+ * @param String id
31
+ * @param Array locations
32
+ */
33
+ function register(id, locations) {
34
+ this.data.push({id: id, locations: locations});
35
+ this.render();
36
+ }
37
+
38
+ /**
39
+ * Render maps data if Google Maps API is loaded.
40
+ */
41
+ function render() {
42
+ if (!this.mapReady) return;
43
+
44
+ while (this.data.length > 0) {
45
+ var data = this.data.pop(),
46
+ bounds = new google.maps.LatLngBounds(),
47
+ map = new google.maps.Map(document.getElementById(data.id), this.options),
48
+ markers = data.locations.map(createMarker);
49
+
50
+ map.fitBounds(bounds);
51
+ this.maps.push({map: map, markers: markers});
52
+ this.processCluster();
53
+ }
54
+
55
+ function createMarker(location) {
56
+ var position = new google.maps.LatLng(location.latitude, location.longitude),
57
+ marker = new google.maps.Marker({
58
+ position: position,
59
+ title: location.title,
60
+ url: location.url,
61
+ map: map
62
+ });
63
+
64
+ bounds.extend(position);
65
+ marker.addListener('click', function () {
66
+ window.location.href = this.url
67
+ });
68
+
69
+ return marker;
70
+ }
71
+ }
72
+
73
+ function initializeCluster() {
74
+ this.clusterReady = true;
75
+ this.processCluster();
76
+ }
77
+
78
+ function processCluster() {
79
+ if (!this.clusterReady) return;
80
+
81
+ while (this.maps.length > 0) {
82
+ var obj = this.maps.pop();
83
+ new MarkerClusterer(obj.map, obj.markers, {
84
+ gridSize: 25,
85
+ imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
86
+ });
87
+ }
88
+ }
89
+ }());
@@ -0,0 +1,50 @@
1
+ module Jekyll
2
+ module Maps
3
+ class GoogleMapApi
4
+ HEAD_END_TAG = %r!</.*head.*>!
5
+
6
+ class << self
7
+ def prepend_api_code(doc)
8
+ if doc.output =~ HEAD_END_TAG
9
+ # Insert API code before header's end if this document has one.
10
+ doc.output.gsub!(HEAD_END_TAG, %(#{api_code}#{Regexp.last_match}))
11
+ else
12
+ doc.output.prepend(api_code)
13
+ end
14
+ end
15
+
16
+ private
17
+ def api_code
18
+ <<HTML
19
+ <script type='text/javascript'>
20
+ #{js_lib_contents}
21
+ </script>
22
+ <script async defer src='https://maps.googleapis.com/maps/api/js?callback=jekyllMaps.initializeMap'></script>
23
+ <script async defer src='https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/src/markerclusterer.js'
24
+ onload='jekyllMaps.initializeCluster()'></script>
25
+ HTML
26
+ end
27
+
28
+ private
29
+ def js_lib_contents
30
+ @js_lib_contents ||= begin
31
+ File.read(js_lib_path)
32
+ end
33
+ end
34
+
35
+ private
36
+ def js_lib_path
37
+ @js_lib_path ||= begin
38
+ File.expand_path("./google_map_api.js", File.dirname(__FILE__))
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ Jekyll::Hooks.register [:pages, :documents], :post_render do |doc|
47
+ if doc.output =~ %r!#{Jekyll::Maps::GoogleMapTag::JS_LIB_NAME}!
48
+ Jekyll::Maps::GoogleMapApi.prepend_api_code(doc)
49
+ end
50
+ end
@@ -1,35 +1,51 @@
1
1
  module Jekyll
2
2
  module Maps
3
3
  class GoogleMapTag < Liquid::Tag
4
+ JS_LIB_NAME = "jekyllMaps".freeze
5
+ DEFAULT_MAP_WIDTH = 600
6
+ DEFAULT_MAP_HEIGHT = 400
7
+
4
8
  def initialize(_, args, _)
5
- options = OptionsParser.parse(args)
6
- @finder = LocationFinder.new(options)
9
+ @args = OptionsParser.parse(args)
10
+ @finder = LocationFinder.new(@args)
7
11
  super
8
12
  end
9
13
 
10
14
  def render(context)
11
- template.render!({
12
- "locations" => @finder.find(context.registers[:site]).to_json
13
- })
15
+ locations = @finder.find(context.registers[:site])
16
+ @args[:attributes][:id] ||= SecureRandom.uuid
17
+
18
+ <<HTML
19
+ <div #{render_attributes}></div>
20
+ <script type='text/javascript'>
21
+ #{JS_LIB_NAME}.register('#{@args[:attributes][:id]}', #{locations.to_json});
22
+ </script>
23
+ HTML
14
24
  end
15
25
 
16
26
  private
17
- def template
18
- @template ||= Liquid::Template.parse(template_contents)
27
+ def render_attributes
28
+ attributes = []
29
+ attributes << "id='#{@args[:attributes][:id]}'"
30
+ attributes << render_dimensions
31
+ attributes << render_class if @args[:attributes][:class]
32
+ attributes.join(" ")
19
33
  end
20
34
 
21
35
  private
22
- def template_contents
23
- @template_contents ||= begin
24
- File.read(template_path)
25
- end
36
+ def render_dimensions
37
+ width = @args[:attributes][:width] || DEFAULT_MAP_WIDTH
38
+ height = @args[:attributes][:height] || DEFAULT_MAP_HEIGHT
39
+ width_unit = width.to_s.include?("%") ? "" : "px"
40
+ height_unit = height.to_s.include?("%") ? "" : "px"
41
+ %(style='width:#{width}#{width_unit};height:#{height}#{height_unit};')
26
42
  end
27
43
 
28
44
  private
29
- def template_path
30
- @template_path ||= begin
31
- File.expand_path("./google_map.html", File.dirname(__FILE__))
32
- end
45
+ def render_class
46
+ css = @args[:attributes][:class]
47
+ css = css.join(" ") if css.is_a?(Array)
48
+ %(class='#{css}')
33
49
  end
34
50
  end
35
51
  end
@@ -2,29 +2,29 @@ module Jekyll
2
2
  module Maps
3
3
  class LocationFinder
4
4
  def initialize(options)
5
- @options = options
5
+ @documents = []
6
+ @options = options
6
7
  end
7
8
 
8
9
  def find(site)
9
- matching_documents(site).map do |document|
10
+ site.collections.each { |_, collection| filter(collection.docs) }
11
+ site.data.each { |_, docs| filter(docs) }
12
+
13
+ @documents.map do |document|
10
14
  {
11
15
  :latitude => document["location"]["latitude"],
12
16
  :longitude => document["location"]["longitude"],
13
17
  :title => document["title"],
14
- :url => document.url
18
+ :url => document["url"] || document.url
15
19
  }
16
20
  end
17
21
  end
18
22
 
19
23
  private
20
- def matching_documents(site)
21
- documents = []
22
- site.collections.each do |_, collection|
23
- collection.docs.each do |doc|
24
- documents << doc if location?(doc) && match_filters?(doc)
25
- end
24
+ def filter(docs)
25
+ docs.each do |doc|
26
+ @documents << doc if location?(doc) && match_filters?(doc)
26
27
  end
27
- documents
28
28
  end
29
29
 
30
30
  private
@@ -1,14 +1,27 @@
1
1
  module Jekyll
2
2
  module Maps
3
3
  class OptionsParser
4
+ OPTIONS_SYNTAX = %r!([^\s]+)\s*:\s*([^\s]+)!
5
+ ALLOWED_ATTRIBUTES = %w(
6
+ id
7
+ width
8
+ height
9
+ class
10
+ ).freeze
11
+
4
12
  class << self
5
13
  def parse(raw_options)
6
- options = { :filters => {} }
7
- raw_options.strip.split(" ").each do |pair|
8
- key = pair.split(":").first.strip
9
- value = pair.split(":").last.strip
14
+ options = {
15
+ :attributes => {},
16
+ :filters => {}
17
+ }
18
+ raw_options.scan(OPTIONS_SYNTAX).each do |key, value|
10
19
  value = value.split(",") if value.include?(",")
11
- options[:filters][key] = value
20
+ if ALLOWED_ATTRIBUTES.include?(key)
21
+ options[:attributes][key.to_sym] = value
22
+ else
23
+ options[:filters][key] = value
24
+ end
12
25
  end
13
26
  options
14
27
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Maps
3
- VERSION = "1.0.2".freeze
3
+ VERSION = "1.1.0".freeze
4
4
  end
5
5
  end
data/lib/jekyll-maps.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "securerandom"
2
+ require "jekyll-maps/google_map_api"
1
3
  require "jekyll-maps/google_map_tag"
2
4
  require "jekyll-maps/location_finder"
3
5
  require "jekyll-maps/options_parser"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-maps
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoliy Yastreb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-18 00:00:00.000000000 Z
11
+ date: 2016-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '11.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '11.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".codeclimate.yml"
76
77
  - ".gitignore"
77
78
  - ".rubocop.yml"
78
79
  - ".travis.yml"
@@ -83,7 +84,8 @@ files:
83
84
  - Rakefile
84
85
  - jekyll-maps.gemspec
85
86
  - lib/jekyll-maps.rb
86
- - lib/jekyll-maps/google_map.html
87
+ - lib/jekyll-maps/google_map_api.js
88
+ - lib/jekyll-maps/google_map_api.rb
87
89
  - lib/jekyll-maps/google_map_tag.rb
88
90
  - lib/jekyll-maps/location_finder.rb
89
91
  - lib/jekyll-maps/options_parser.rb
@@ -1,53 +0,0 @@
1
- <div id='google-map'></div>
2
-
3
- <script type='text/javascript'>
4
- var jekyllMaps = (function () {
5
- 'use strict';
6
-
7
- return {
8
- loadScript: function (url) {
9
- var script = document.createElement('script');
10
- script.type = 'text/javascript';
11
- script.src = url;
12
- document.body.appendChild(script);
13
- },
14
- initialize: function () {
15
- this.options = {
16
- center: new google.maps.LatLng(0, 0),
17
- mapTypeId: google.maps.MapTypeId.ROADMAP,
18
- zoom: 3
19
- }
20
- this.map = new google.maps.Map(document.getElementById('google-map'), this.options);
21
- this.showMarkers({{ locations }});
22
- },
23
- showMarkers: function (locations) {
24
- var map = this.map,
25
- bounds = new google.maps.LatLngBounds(),
26
- markers = locations.map(function (location) {
27
- var position = new google.maps.LatLng(location.latitude, location.longitude);
28
- var marker = new google.maps.Marker({
29
- position: position,
30
- title: location.title,
31
- url: location.url,
32
- map: map
33
- });
34
-
35
- marker.addListener('click', function () {
36
- window.location.href = this.url
37
- });
38
- bounds.extend(position);
39
-
40
- return marker;
41
- });
42
-
43
- new MarkerClusterer(map, markers, {
44
- gridSize: 25,
45
- imagePath: 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
46
- });
47
- map.fitBounds(bounds);
48
- },
49
- };
50
- }());
51
- </script>
52
- <script src='https://googlemaps.github.io/js-marker-clusterer/src/markerclusterer.js'></script>
53
- <script src='https://maps.googleapis.com/maps/api/js?callback=jekyllMaps.initialize'></script>