Adds collection_tag
This tag mirrors the post_tag functionality but for collections instead of just posts.
This commit is contained in:
parent
81c88d6833
commit
b80a0cb5ce
|
@ -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} <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)
|
|
@ -14,6 +14,10 @@ class TestTags < JekyllUnitTest
|
||||||
if override['read_posts']
|
if override['read_posts']
|
||||||
site.posts.docs.concat(PostReader.new(site).read_posts(''))
|
site.posts.docs.concat(PostReader.new(site).read_posts(''))
|
||||||
end
|
end
|
||||||
|
if override['read_collections']
|
||||||
|
# puts "reading collections"
|
||||||
|
CollectionReader.new(site).read
|
||||||
|
end
|
||||||
|
|
||||||
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }
|
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }
|
||||||
@converter = site.converters.find { |c| c.class == converter_class }
|
@converter = site.converters.find { |c| c.class == converter_class }
|
||||||
|
@ -476,6 +480,73 @@ CONTENT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "simple page with collection linking" do
|
||||||
|
setup do
|
||||||
|
content = <<CONTENT
|
||||||
|
---
|
||||||
|
title: Collection linking
|
||||||
|
---
|
||||||
|
|
||||||
|
{% collection_url methods yaml_with_dots %}
|
||||||
|
CONTENT
|
||||||
|
create_post(content, {'source' => 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 = <<CONTENT
|
||||||
|
---
|
||||||
|
title: Collection 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 %}
|
||||||
|
CONTENT
|
||||||
|
create_post(content, {'source' => 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 = <<CONTENT
|
||||||
|
---
|
||||||
|
title: Invalid collection name linking
|
||||||
|
---
|
||||||
|
|
||||||
|
{% collection_url methods non-existent-collection-item %}
|
||||||
|
CONTENT
|
||||||
|
|
||||||
|
assert_raises ArgumentError do
|
||||||
|
create_post(content, {'source' => source_dir, 'destination' => dest_dir, 'collections' => { 'methods' => {}}, 'read_collections' => true})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "include tag with parameters" do
|
context "include tag with parameters" do
|
||||||
|
|
||||||
context "with symlink'd include" do
|
context "with symlink'd include" do
|
||||||
|
|
Loading…
Reference in New Issue