From f79f9d60a97dcc3a700045d7438266bda77cea51 Mon Sep 17 00:00:00 2001 From: Jeff Kolesky Date: Thu, 17 Mar 2016 14:01:04 -0700 Subject: [PATCH] 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