lib/jekyll-maps/location_finder.rb in jekyll-maps-1.1.6 vs lib/jekyll-maps/location_finder.rb in jekyll-maps-2.0.0

- old
+ new

@@ -5,21 +5,45 @@ @documents = [] @options = options end def find(site, page) - if @options[:flags][:on_page] + if @options[:filters].empty? @documents << page if location?(page) else site.collections.each { |_, collection| filter(collection.docs) } - site.data.each { |_, docs| filter(docs) } + site_data(site).each { |_, items| traverse(items) } end convert end private + def site_data(site) + return {} unless data_source? + + path = @options[:filters]["src"].scan(%r!_data\/([^\/]+)!).join(".") + return site.data if path.empty? + + data = OpenStruct.new(site.data) + data[path] + end + + private + def data_source? + filters = @options[:filters] + filters.key?("src") && filters["src"].start_with?("_data") + end + + private + def traverse(items) + return filter(items) if items.is_a?(Array) + + items.each { |_, children| traverse(children) } if items.is_a?(Hash) + end + + private def filter(docs) docs.each do |doc| @documents << doc if location?(doc) && match_filters?(doc) end end @@ -29,11 +53,16 @@ !doc["location"].nil? && !doc["location"].empty? end private def match_filters?(doc) - @options[:filters].each do |key, value| - return false if doc[key].nil? || doc[key] != value + @options[:filters].each do |filter, value| + if filter == "src" + return true unless doc.respond_to?(:relative_path) + return false unless doc.relative_path.start_with?(value) + elsif doc[filter].nil? || doc[filter] != value + return false + end end end private def convert