From b80a0cb5cea2424922ee3ce5184f9af5c46ec196 Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Wed, 2 Mar 2016 14:54:47 -0800 Subject: [PATCH 1/4] 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 From f79f9d60a97dcc3a700045d7438266bda77cea51 Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Thu, 17 Mar 2016 14:01:04 -0700 Subject: [PATCH 2/4] Changes `collection_url` tag to `link` tag --- lib/jekyll/tags/collection_url.rb | 47 ------------------------------- lib/jekyll/tags/link.rb | 29 +++++++++++++++++++ test/test_tags.rb | 33 +++++++++------------- 3 files changed, 43 insertions(+), 66 deletions(-) delete mode 100644 lib/jekyll/tags/collection_url.rb create mode 100644 lib/jekyll/tags/link.rb diff --git a/lib/jekyll/tags/collection_url.rb b/lib/jekyll/tags/collection_url.rb deleted file mode 100644 index bdd9d398..00000000 --- a/lib/jekyll/tags/collection_url.rb +++ /dev/null @@ -1,47 +0,0 @@ -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/lib/jekyll/tags/link.rb b/lib/jekyll/tags/link.rb new file mode 100644 index 00000000..9ab76167 --- /dev/null +++ b/lib/jekyll/tags/link.rb @@ -0,0 +1,29 @@ +module Jekyll + module Tags + class Link < Liquid::Tag + TagName = 'link' + + def initialize(tag_name, relative_path, tokens) + super + + @relative_path = relative_path.strip + end + + def render(context) + site = context.registers[:site] + + site.docs_to_write.each do |document| + return document.url if document.relative_path == @relative_path + end + + raise ArgumentError.new <<-eos +Could not find document "#{@relative_path}" in tag '#{TagName}'. + +Make sure the document exists and the path is correct. +eos + end + end + end +end + +Liquid::Template.register_tag(Jekyll::Tags::Link::TagName, Jekyll::Tags::Link) diff --git a/test/test_tags.rb b/test/test_tags.rb index 83ae9f87..6d9f7963 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -15,7 +15,6 @@ class TestTags < JekyllUnitTest site.posts.docs.concat(PostReader.new(site).read_posts('')) end if override['read_collections'] - # puts "reading collections" CollectionReader.new(site).read end @@ -480,16 +479,16 @@ CONTENT end end - context "simple page with collection linking" do + context "simple page with linking" do setup do content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + create_post(content, {'source' => source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => { 'output' => true }}, 'read_collections' => true}) end should "not cause an error" do @@ -501,19 +500,17 @@ CONTENT end end - context "simple page with nested collection linking" do + context "simple page with nested linking" do setup do content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + create_post(content, {'source' => source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => { 'output' => true }}, 'read_collections' => true}) end should "not cause an error" do @@ -522,27 +519,25 @@ CONTENT 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 + assert_match %r{2\s/methods/site/generate}, @result end end - context "simple page with invalid collection linking" do + context "simple page with invalid linking" do should "cause an error" do content = < source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true}) + create_post(content, {'source' => source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => { 'output' => true }}, 'read_collections' => true}) end end end From 4b32bd2b1345d3071a150f5fcddf5b042f81580a Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Thu, 17 Mar 2016 14:26:13 -0700 Subject: [PATCH 3/4] Updates code styling per PR --- lib/jekyll/tags/link.rb | 7 ++----- test/test_tags.rb | 8 ++------ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/jekyll/tags/link.rb b/lib/jekyll/tags/link.rb index 9ab76167..f81e43b4 100644 --- a/lib/jekyll/tags/link.rb +++ b/lib/jekyll/tags/link.rb @@ -16,11 +16,8 @@ module Jekyll return document.url if document.relative_path == @relative_path end - raise ArgumentError.new <<-eos -Could not find document "#{@relative_path}" in tag '#{TagName}'. - -Make sure the document exists and the path is correct. -eos + raise ArgumentError, "Could not find document '#{@relative_path}' in tag '#{TagName}'.\n\n" \ + "Make sure the document exists and the path is correct." end end end diff --git a/test/test_tags.rb b/test/test_tags.rb index 6d9f7963..84594188 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -11,12 +11,8 @@ class TestTags < JekyllUnitTest def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown) site = fixture_site({"highlighter" => "rouge"}.merge(override)) - if override['read_posts'] - site.posts.docs.concat(PostReader.new(site).read_posts('')) - end - if override['read_collections'] - CollectionReader.new(site).read - end + site.posts.docs.concat(PostReader.new(site).read_posts('')) if override['read_posts'] + CollectionReader.new(site).read if override['read_collections'] info = { :filters => [Jekyll::Filters], :registers => { :site => site } } @converter = site.converters.find { |c| c.class == converter_class } From 54fcc977250c899d50d899134c7f44d8223748cf Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Thu, 24 Mar 2016 08:54:31 -0700 Subject: [PATCH 4/4] Checks for link file extension in tests --- test/test_tags.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_tags.rb b/test/test_tags.rb index 84594188..548df475 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -492,7 +492,7 @@ CONTENT end should "have the url to the \"yaml_with_dots\" item" do - assert_match %r{/methods/yaml_with_dots}, @result + assert_match %r{/methods/yaml_with_dots\.html}, @result end end @@ -514,11 +514,11 @@ CONTENT end should "have the url to the \"sanitized_path\" item" do - assert_match %r{1\s/methods/sanitized_path}, @result + assert_match %r{1\s/methods/sanitized_path\.html}, @result end should "have the url to the \"site/generate\" item" do - assert_match %r{2\s/methods/site/generate}, @result + assert_match %r{2\s/methods/site/generate\.html}, @result end end