Changes `collection_url` tag to `link` tag

This commit is contained in:
Jeff Kolesky 2016-03-17 14:01:04 -07:00
parent b80a0cb5ce
commit f79f9d60a9
3 changed files with 43 additions and 66 deletions

View File

@ -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} <collection> <item>
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)

29
lib/jekyll/tags/link.rb Normal file
View File

@ -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)

View File

@ -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 = <<CONTENT
---
title: Collection linking
title: linking
---
{% collection_url methods yaml_with_dots %}
{% link _methods/yaml_with_dots.md %}
CONTENT
create_post(content, {'source' => 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 = <<CONTENT
---
title: Collection linking
title: linking
---
- 1 {% collection_url methods sanitized_path %}
- 2 {% collection_url methods /sanitized_path %}
- 3 {% collection_url methods site/generate %}
- 4 {% collection_url methods /site/generate %}
- 1 {% link _methods/sanitized_path.md %}
- 2 {% link _methods/site/generate.md %}
CONTENT
create_post(content, {'source' => 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 = <<CONTENT
---
title: Invalid collection name linking
title: Invalid linking
---
{% collection_url methods non-existent-collection-item %}
{% link non-existent-collection-item %}
CONTENT
assert_raises ArgumentError do
create_post(content, {'source' => 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