Skip to content

Commit 8055314

Browse files
committed
google-map: use on page location by default
- change options syntax
1 parent a8b028f commit 8055314

File tree

10 files changed

+103
-29
lines changed

10 files changed

+103
-29
lines changed

lib/jekyll-maps.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "securerandom"
2+
require "ostruct"
23
require "jekyll-maps/google_map_api"
34
require "jekyll-maps/google_map_tag"
45
require "jekyll-maps/location_finder"

lib/jekyll-maps/location_finder.rb

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@ def initialize(options)
77
end
88

99
def find(site, page)
10-
if @options[:flags][:on_page]
10+
if @options[:filters].empty?
1111
@documents << page if location?(page)
1212
else
1313
site.collections.each { |_, collection| filter(collection.docs) }
14-
site.data.each { |_, docs| filter(docs) }
14+
site_data(site).each { |_, items| traverse(items) }
1515
end
1616

1717
convert
1818
end
1919

20+
private
21+
def site_data(site)
22+
return {} unless data_source?
23+
24+
path = @options[:filters]["src"].scan(%r!_data\/([^\/]+)!).join(".")
25+
return site.data if path.empty?
26+
27+
data = OpenStruct.new(site.data)
28+
data[path]
29+
end
30+
31+
private
32+
def data_source?
33+
filters = @options[:filters]
34+
filters.key?("src") && filters["src"].start_with?("_data")
35+
end
36+
37+
private
38+
def traverse(items)
39+
return filter(items) if items.is_a?(Array)
40+
41+
items.each { |_, children| traverse(children) } if items.is_a?(Hash)
42+
end
43+
2044
private
2145
def filter(docs)
2246
docs.each do |doc|
@@ -31,8 +55,13 @@ def location?(doc)
3155

3256
private
3357
def match_filters?(doc)
34-
@options[:filters].each do |key, value|
35-
return false if doc[key].nil? || doc[key] != value
58+
@options[:filters].each do |filter, value|
59+
if filter == "src"
60+
return true unless doc.respond_to?(:relative_path)
61+
return false unless doc.relative_path.start_with?(value)
62+
elsif doc[filter].nil? || doc[filter] != value
63+
return false
64+
end
3665
end
3766
end
3867

lib/jekyll-maps/options_parser.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module Jekyll
22
module Maps
33
class OptionsParser
4-
OPTIONS_SYNTAX = %r!([^\s]+)\s*:\s*([^\s]+)!
4+
OPTIONS_SYNTAX = %r!([^\s]+)\s*=\s*['"]+([^\s'"]+)['"]+!
55
ALLOWED_FLAGS = %w(
66
no_cluster
7-
on_page
87
).freeze
98
ALLOWED_ATTRIBUTES = %w(
109
id
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- title: Paris
2+
url: http://google.fr
3+
location:
4+
latitude: 48.8587741
5+
longitude: 2.2074741
6+
7+
- title: Not a place
8+
url: http://yahoo.com

spec/fixtures/_data/places.yml renamed to spec/fixtures/_data/spain/places.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
- title: Paris
2-
url: http://google.fr
3-
location:
4-
latitude: 48.8587741
5-
longitude: 2.2074741
6-
71
- title: Madrid
82
url: http://google.es
93
location:

spec/fixtures/page.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ layout: default
55

66
** Jekyll Maps Plugin **
77

8-
{% google_map width:600 height:400 %}
8+
{% google_map src="_posts" width="600" height="400" %}

spec/fixtures/page_without_layout.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ title: Test Page
44

55
** Jekyll Maps Plugin **
66

7-
{% google_map width:100% height:100% %}
7+
{% google_map src="_posts" width="100%" height="100%" %}

spec/google_map_tag_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@
6262
let(:tag) { "google_map" }
6363

6464
context "render all attributes" do
65-
let(:options) { "id:foo width:100 height:50% class:baz,bar ignored:bad zoom:5" }
65+
let(:options) do
66+
"id='foo' width='100' height='50%' class='baz,bar' ignored='bad' zoom='5'"
67+
end
6668
let(:output) do
6769
Liquid::Template.parse("{% #{tag} #{options} %}").render!(context, {})
6870
end
@@ -79,7 +81,7 @@
7981
end
8082

8183
context "render default dimensions" do
82-
let(:options) { "id:foo" }
84+
let(:options) { "id='foo'" }
8385
let(:output) do
8486
Liquid::Template.parse("{% #{tag} #{options} %}").render!(context, {})
8587
end

spec/location_finder_spec.rb

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
site.process
99
end
1010

11-
context "looking for locations" do
12-
let(:options) { Jekyll::Maps::OptionsParser.parse("") }
11+
context "looking for locations in posts" do
12+
let(:options) { Jekyll::Maps::OptionsParser.parse("src='_posts'") }
1313
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
1414
let(:actual) { finder.find(site, page) }
1515

@@ -18,23 +18,56 @@
1818
expect(actual).to all(include(:latitude, :longitude, :title, :url))
1919
end
2020

21+
it "finds location in post" do
22+
expect(actual.find { |l| l[:title] == "London" }).to be_a(Hash)
23+
end
24+
2125
it "skips posts without location" do
2226
actual.each do |location|
2327
expect(location).not_to include(:title => "post without location")
2428
end
2529
end
30+
end
31+
32+
context "looking for locations in custom collections" do
33+
let(:options) { Jekyll::Maps::OptionsParser.parse("src='_my_collection'") }
34+
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
35+
let(:actual) { finder.find(site, page) }
2636

2737
it "finds location in custom collections" do
2838
expect(actual.find { |l| l[:title] == "Tokyo" }).to be_a(Hash)
2939
end
40+
end
41+
42+
context "looking for locations in data files with deep source" do
43+
let(:options) { Jekyll::Maps::OptionsParser.parse("src='_data/france'") }
44+
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
45+
let(:actual) { finder.find(site, page) }
46+
47+
it "finds location from France" do
48+
expect(actual.find { |l| l[:title] == "Paris" }).to be_a(Hash)
49+
end
50+
51+
it "doesn't find location from Spain" do
52+
actual.each do |location|
53+
expect(location).not_to include(:title => "Madird")
54+
end
55+
end
56+
end
57+
58+
context "looking for locations in data files with shallow source" do
59+
let(:options) { Jekyll::Maps::OptionsParser.parse("src='_data'") }
60+
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
61+
let(:actual) { finder.find(site, page) }
3062

31-
it "finds location in data files" do
63+
it "finds locations in all data files" do
3264
expect(actual.find { |l| l[:title] == "Paris" }).to be_a(Hash)
65+
expect(actual.find { |l| l[:title] == "Madrid" }).to be_a(Hash)
3366
end
3467
end
3568

3669
context "filtering locations" do
37-
let(:options) { Jekyll::Maps::OptionsParser.parse("country:de") }
70+
let(:options) { Jekyll::Maps::OptionsParser.parse("src='_posts' country='de'") }
3871
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
3972
let(:actual) { finder.find(site, page) }
4073

@@ -46,10 +79,10 @@
4679
end
4780
end
4881

49-
context "looking for on_page locations" do
82+
context "by default look for locations on current page" do
5083
let(:location) { { "location" => { "latitude" => 1, "longitude" => -1 } } }
5184
let(:page) { make_page(location) }
52-
let(:options) { Jekyll::Maps::OptionsParser.parse("on_page") }
85+
let(:options) { Jekyll::Maps::OptionsParser.parse("") }
5386
let(:finder) { Jekyll::Maps::LocationFinder.new(options) }
5487
let(:actual) { finder.find(site, page) }
5588

spec/options_parser_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe Jekyll::Maps::OptionsParser do
44
context "parses filters" do
55
it "ignores extra whitespaces" do
6-
actual = Jekyll::Maps::OptionsParser.parse(" foo_key : bar moo : baz")
6+
actual = Jekyll::Maps::OptionsParser.parse(" foo_key = 'bar' moo = 'baz'")
77
expected = {
88
"foo_key" => "bar",
99
"moo" => "baz"
@@ -12,8 +12,17 @@
1212
expect(actual[:filters]).to eq(expected)
1313
end
1414

15+
it "parses double quotes" do
16+
actual = Jekyll::Maps::OptionsParser.parse('foo="bar"')
17+
expected = {
18+
"foo" => "bar"
19+
}
20+
21+
expect(actual[:filters]).to eq(expected)
22+
end
23+
1524
it "parses single argument" do
16-
actual = Jekyll::Maps::OptionsParser.parse("foo:bar")
25+
actual = Jekyll::Maps::OptionsParser.parse("foo='bar'")
1726
expected = {
1827
"foo" => "bar"
1928
}
@@ -22,7 +31,7 @@
2231
end
2332

2433
it "parses multiple arguments" do
25-
actual = Jekyll::Maps::OptionsParser.parse("foo:bar moo:baz")
34+
actual = Jekyll::Maps::OptionsParser.parse("foo='bar' moo='baz'")
2635
expected = {
2736
"foo" => "bar",
2837
"moo" => "baz"
@@ -32,7 +41,7 @@
3241
end
3342

3443
it "parses multiple values in argument" do
35-
actual = Jekyll::Maps::OptionsParser.parse("foo:bar,baz")
44+
actual = Jekyll::Maps::OptionsParser.parse("foo='bar,baz'")
3645
expected = {
3746
"foo" => %w(bar baz)
3847
}
@@ -44,7 +53,7 @@
4453
context "parses attributes" do
4554
it "parses predefined attributes" do
4655
actual = Jekyll::Maps::OptionsParser.parse(
47-
"id:foo width:100 height:50% class:my-css-class,another-class"
56+
"id='foo' width='100' height='50%' class='my-css-class,another-class'"
4857
)
4958
expected = {
5059
:id => "foo",
@@ -59,9 +68,8 @@
5968

6069
context "parses flags" do
6170
it "parses all allowed flags correctly" do
62-
actual = Jekyll::Maps::OptionsParser.parse("on_page no_cluster")
71+
actual = Jekyll::Maps::OptionsParser.parse("no_cluster")
6372
expected = {
64-
:on_page => true,
6573
:no_cluster => true
6674
}
6775

0 commit comments

Comments
 (0)