From b80a0cb5cea2424922ee3ce5184f9af5c46ec196 Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Wed, 2 Mar 2016 14:54:47 -0800 Subject: [PATCH] Adds collection_tag This tag mirrors the post_tag functionality but for collections instead of just posts. --- lib/jekyll/tags/collection_url.rb | 47 ++++++++++++++++++++ test/test_tags.rb | 71 +++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 lib/jekyll/tags/collection_url.rb diff --git a/lib/jekyll/tags/collection_url.rb b/lib/jekyll/tags/collection_url.rb new file mode 100644 index 00000000..bdd9d398 --- /dev/null +++ b/lib/jekyll/tags/collection_url.rb @@ -0,0 +1,47 @@ +module Jekyll + module Tags + class CollectionUrl < Liquid::Tag + TagName = 'collection_url' + MATCHER = /^([\w-]+)\s+\/?(.*)$/ + + def initialize(tag_name, markup, tokens) + super + orig_markup = markup.strip + all, @collection, @item_name = *orig_markup.match(MATCHER) + unless all + raise ArgumentError.new <<-eos +Could not parse the collection or item "#{markup}" in tag '#{TagName}'. + +Valid syntax: #{TagName} +eos + end + + @path_regex = /^\/#{@item_name}$/ + end + + def render(context) + site = context.registers[:site] + + if site.collections[@collection] + site.collections[@collection].docs.each do |p| + return p.url if p.cleaned_relative_path.match(@path_regex) + end + + raise ArgumentError.new <<-eos +Could not find item "#{@item_name}" in collection "#{@collection}" in tag '#{TagName}'. + +Make sure the item exists and the name is correct. +eos + else + raise ArgumentError.new <<-eos +Could not find collection "#{@collection}" in tag '#{TagName}' + +Make sure the collection exists and the name is correct. +eos + end + end + end + end +end + +Liquid::Template.register_tag(Jekyll::Tags::CollectionUrl::TagName, Jekyll::Tags::CollectionUrl) diff --git a/test/test_tags.rb b/test/test_tags.rb index f263e9d8..83ae9f87 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -14,6 +14,10 @@ class TestTags < JekyllUnitTest if override['read_posts'] site.posts.docs.concat(PostReader.new(site).read_posts('')) end + if override['read_collections'] + # puts "reading collections" + CollectionReader.new(site).read + end info = { :filters => [Jekyll::Filters], :registers => { :site => site } } @converter = site.converters.find { |c| c.class == converter_class } @@ -476,6 +480,73 @@ CONTENT end end + context "simple page with collection linking" do + setup do + content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + end + + should "not cause an error" do + refute_match /markdown\-html\-error/, @result + end + + should "have the url to the \"yaml_with_dots\" item" do + assert_match %r{/methods/yaml_with_dots}, @result + end + end + + context "simple page with nested collection linking" do + setup do + content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + end + + should "not cause an error" do + refute_match /markdown\-html\-error/, @result + end + + should "have the url to the \"sanitized_path\" item" do + assert_match %r{1\s/methods/sanitized_path}, @result + assert_match %r{2\s/methods/sanitized_path}, @result + end + + should "have the url to the \"site/generate\" item" do + assert_match %r{3\s/methods/site/generate}, @result + assert_match %r{4\s/methods/site/generate}, @result + end + end + + context "simple page with invalid collection linking" do + should "cause an error" do + content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + end + end + end + context "include tag with parameters" do context "with symlink'd include" do