lib/jekyll-maps/location_finder.rb in jekyll-maps-2.0.3 vs lib/jekyll-maps/location_finder.rb in jekyll-maps-2.0.4

- old
+ new

@@ -5,21 +5,34 @@ @documents = [] @options = options end def find(site, page) - if @options[:filters].empty? - @documents << page if location?(page) + if @options[:attributes][:latitude] && @options[:attributes][:longitude] + return [location_from_options(page)] + elsif @options[:filters].empty? + @documents << page if with_location?(page) else site.collections.each { |_, collection| filter(collection.docs) } site_data(site).each { |_, items| traverse(items) } end - convert + documents_to_locations end private + def location_from_options(page) + { + :latitude => @options[:attributes][:latitude], + :longitude => @options[:attributes][:longitude], + :title => @options[:attributes][:marker_title] || page["title"], + :url => @options[:attributes][:marker_url] || fetch_url(page), + :image => @options[:attributes][:marker_img] || page["image"] || "" + } + end + + private def site_data(site) return {} unless data_source? path = @options[:filters]["src"].scan(%r!_data\/([^\/]+)!).join(".") return site.data if path.empty? @@ -46,16 +59,16 @@ end private def filter(docs) docs.each do |doc| - @documents << doc if location?(doc) && match_filters?(doc) + @documents << doc if with_location?(doc) && match_filters?(doc) end end private - def location?(doc) + def with_location?(doc) !doc["location"].nil? && !doc["location"].empty? end private def match_filters?(doc) @@ -68,26 +81,39 @@ end end end private - def convert - @documents.map do |document| - { - :latitude => document["location"]["latitude"], - :longitude => document["location"]["longitude"], - :title => document["title"], - :url => fetch_url(document), - :image => document["image"] || "" - } + def documents_to_locations + locations = [] + @documents.each do |document| + if document["location"].is_a?(Array) + document["location"].each do |location| + locations.push(convert(document, location)) + end + else + locations.push(convert(document, document["location"])) + end end + locations end private + def convert(document, location) + { + :latitude => location["latitude"], + :longitude => location["longitude"], + :title => location["title"] || document["title"], + :url => location["url"] || fetch_url(document), + :image => location["image"] || document["image"] || "" + } + end + + private def fetch_url(document) return document["url"] if document.is_a?(Hash) && document.key?("url") return document.url if document.respond_to? :url - return "" + "" end end end end