From 04313561ec8fe133c29301a70618518714b35af2 Mon Sep 17 00:00:00 2001 From: Anatoliy Yastreb Date: Sun, 17 Jul 2016 13:46:48 +0300 Subject: [PATCH] google-map: implement google map tag --- lib/jekyll-maps/google_map.html | 58 +++++++++++++++++++ lib/jekyll-maps/google_map_tag.rb | 57 +++++++++++++++++- spec/fixtures/_posts/2016-07-01-london.md | 9 +++ spec/fixtures/_posts/2016-07-02-berlin.md | 9 +++ .../fixtures/_posts/2016-07-03-no-location.md | 5 ++ spec/fixtures/page_filter_attribute.md | 7 +++ spec/fixtures/page_no_options.md | 7 +++ spec/jekyll_google_map_spec.rb | 35 +++++++++++ spec/spec_helper.rb | 33 +++++++++++ 9 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 lib/jekyll-maps/google_map.html create mode 100644 spec/fixtures/_posts/2016-07-01-london.md create mode 100644 spec/fixtures/_posts/2016-07-02-berlin.md create mode 100644 spec/fixtures/_posts/2016-07-03-no-location.md create mode 100644 spec/fixtures/page_filter_attribute.md create mode 100644 spec/fixtures/page_no_options.md create mode 100644 spec/jekyll_google_map_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/lib/jekyll-maps/google_map.html b/lib/jekyll-maps/google_map.html new file mode 100644 index 0000000..c18fcc6 --- /dev/null +++ b/lib/jekyll-maps/google_map.html @@ -0,0 +1,58 @@ +
+ + diff --git a/lib/jekyll-maps/google_map_tag.rb b/lib/jekyll-maps/google_map_tag.rb index cfd6b52..94ce1aa 100644 --- a/lib/jekyll-maps/google_map_tag.rb +++ b/lib/jekyll-maps/google_map_tag.rb @@ -1,11 +1,64 @@ module Jekyll module Maps class GoogleMapTag < Liquid::Tag - def initialize(_tag_name, text, _tokens) + attr_accessor :context + + def initialize(_, args, _) + unless args.empty? + @filter_key = args.split(":").first.strip + @filter_value = args.split(":").last.strip + end + super end - def render(_context) + def render(context) + @context = context + template.render!({ "locations" => locations.to_json }) + end + + private + def locations + filter_posts.map do |post| + { + :latitude => post["location"]["latitude"], + :longitude => post["location"]["longitude"], + :title => post["title"], + :url => post.url + } + end + end + + def filter_posts + posts = context.registers[:site].posts.docs.reject do |post| + post["location"].nil? || post["location"].empty? + end + if @filter_key + posts.reject do |post| + post[@filter_key].nil? || post[@filter_key] != @filter_value + end + else + posts + end + end + + private + def template + @template ||= Liquid::Template.parse template_contents + end + + private + def template_contents + @template_contents ||= begin + File.read(template_path) + end + end + + private + def template_path + @template_path ||= begin + File.expand_path "./google_map.html", File.dirname(__FILE__) + end end end end diff --git a/spec/fixtures/_posts/2016-07-01-london.md b/spec/fixtures/_posts/2016-07-01-london.md new file mode 100644 index 0000000..2706208 --- /dev/null +++ b/spec/fixtures/_posts/2016-07-01-london.md @@ -0,0 +1,9 @@ +--- +title: London +country: gb +location: + latitude: 51.5285582 + longitude: -0.2416807 +--- + +London is a capital of Great Britain. diff --git a/spec/fixtures/_posts/2016-07-02-berlin.md b/spec/fixtures/_posts/2016-07-02-berlin.md new file mode 100644 index 0000000..9463b6d --- /dev/null +++ b/spec/fixtures/_posts/2016-07-02-berlin.md @@ -0,0 +1,9 @@ +--- +title: Berlin +country: de +location: + latitude: 52.5072111 + longitude: 13.1449604 +--- + +Berlin is in Germany. diff --git a/spec/fixtures/_posts/2016-07-03-no-location.md b/spec/fixtures/_posts/2016-07-03-no-location.md new file mode 100644 index 0000000..4f8bc20 --- /dev/null +++ b/spec/fixtures/_posts/2016-07-03-no-location.md @@ -0,0 +1,5 @@ +--- +title: post without location +--- + +Nothing to see here. diff --git a/spec/fixtures/page_filter_attribute.md b/spec/fixtures/page_filter_attribute.md new file mode 100644 index 0000000..942fa89 --- /dev/null +++ b/spec/fixtures/page_filter_attribute.md @@ -0,0 +1,7 @@ +--- +title: Only include posts with matching attribute +--- + +**With Filters** + +{% google_map country:de %} diff --git a/spec/fixtures/page_no_options.md b/spec/fixtures/page_no_options.md new file mode 100644 index 0000000..fd8eff2 --- /dev/null +++ b/spec/fixtures/page_no_options.md @@ -0,0 +1,7 @@ +--- +title: Simple map without options +--- + +**Jekyll Google Maps Plugin** + +{% google_map %} diff --git a/spec/jekyll_google_map_spec.rb b/spec/jekyll_google_map_spec.rb new file mode 100644 index 0000000..8007065 --- /dev/null +++ b/spec/jekyll_google_map_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe Jekyll::Maps::GoogleMapTag do + let(:site) { make_site } + + before do + site.process + end + + context "tag without options" do + let(:content) { File.read(dest_dir("page_no_options.html")) } + + it "builds javascript" do + expect(content).to match(%r!jekyllMaps!) + end + + it "finds posts with location" do + expect(content).to match(%r!Berlin!) + expect(content).to match(%r!London!) + end + + it "skips posts without location" do + expect(content).not_to match(%r!post without location!) + end + end + + context "tag with attribute filter" do + let(:content) { File.read(dest_dir("page_filter_attribute.html")) } + + it "finds only post with matching attribute" do + expect(content).to match(%r!Berlin!) + expect(content).not_to match(%r!London!) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..2e5e8b4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,33 @@ +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +require "jekyll" +require "jekyll-maps" + +Jekyll.logger.log_level = :error + +RSpec.configure do |config| + config.run_all_when_everything_filtered = true + config.filter_run :focus + config.order = "random" + + SOURCE_DIR = File.expand_path("../fixtures", __FILE__) + DEST_DIR = File.expand_path("../dest", __FILE__) + + def source_dir(*files) + File.join(SOURCE_DIR, *files) + end + + def dest_dir(*files) + File.join(DEST_DIR, *files) + end + + CONFIG_DEFAULTS = { + "source" => source_dir, + "destination" => dest_dir, + "gems" => ["jekyll-maps"] + }.freeze + + def make_site(options = {}) + site_config = Jekyll.configuration CONFIG_DEFAULTS.merge(options) + Jekyll::Site.new(site_config) + end +end