make non-data properties/methods accessible to Liquid

per https://github.com/jekyll/jekyll/pull/1849/files#r8490593
This commit is contained in:
Aidan Feldman 2013-12-22 03:38:13 -05:00
parent c2b750448e
commit 2bfafb3b33
10 changed files with 82 additions and 5 deletions

View File

@ -177,7 +177,11 @@ module Jekyll
# #
# Returns the String value or nil if the property isn't included. # Returns the String value or nil if the property isn't included.
def [](property) def [](property)
data[property] if self.class::ATTRIBUTES_FOR_LIQUID.include?(property)
send(property)
else
data[property]
end
end end
end end
end end

View File

@ -9,9 +9,11 @@ module Jekyll
# Attributes for Liquid templates # Attributes for Liquid templates
ATTRIBUTES_FOR_LIQUID = %w[ ATTRIBUTES_FOR_LIQUID = %w[
url
content content
dir
name
path path
url
] ]
# Initialize a new Page. # Initialize a new Page.

View File

@ -9,6 +9,7 @@ module Jekyll
EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[ EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[
title title
url url
dir
date date
id id
categories categories

View File

@ -0,0 +1,11 @@
---
categories: foo bar baz
foo: bar
layout: default
tags: ay bee cee
title: Properties Post
---
All the properties.
Plus an excerpt.

View File

@ -0,0 +1,8 @@
---
foo: bar
layout: default
permalink: /properties/
title: Properties Page
---
All the properties.

View File

@ -120,7 +120,7 @@ class TestFilters < Test::Unit::TestCase
case g["name"] case g["name"]
when "default" when "default"
assert g["items"].is_a?(Array), "The list of grouped items for 'default' is not an Array." assert g["items"].is_a?(Array), "The list of grouped items for 'default' is not an Array."
assert_equal 3, g["items"].size assert_equal 4, g["items"].size
when "nil" when "nil"
assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array." assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array."
assert_equal 2, g["items"].size assert_equal 2, g["items"].size

View File

@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
end end
should "ensure post count is as expected" do should "ensure post count is as expected" do
assert_equal 36, @site.posts.size assert_equal 37, @site.posts.size
end end
should "insert site.posts into the index" do should "insert site.posts into the index" do

View File

@ -47,6 +47,29 @@ class TestPage < Test::Unit::TestCase
assert_equal "deal.with.dots", @page.basename assert_equal "deal.with.dots", @page.basename
end end
should "make properties accessible through #[]" do
page = setup_page('properties.html')
attrs = {
content: "All the properties.\n",
dir: "/properties/",
excerpt: nil,
foo: 'bar',
layout: 'default',
name: "properties.html",
path: "properties.html",
permalink: '/properties/',
published: nil,
title: 'Properties Page',
url: "/properties/"
}
attrs.each do |attr, val|
attr_str = attr.to_s
result = page[attr_str]
assert_equal val, result, "For <page[\"#{attr_str}\"]>:"
end
end
context "with pretty url style" do context "with pretty url style" do
setup do setup do
@site.permalink_style = :pretty @site.permalink_style = :pretty

View File

@ -25,6 +25,34 @@ class TestPost < Test::Unit::TestCase
assert !Post.valid?("blah") assert !Post.valid?("blah")
end end
should "make properties accessible through #[]" do
post = setup_post('2013-12-20-properties.text')
attrs = {
categories: %w(foo bar baz),
content: "All the properties.\n\nPlus an excerpt.\n",
date: Time.new(2013, 12, 20),
dir: "/foo/bar/baz/2013/12/20",
excerpt: "All the properties.\n\n",
foo: 'bar',
id: "/foo/bar/baz/2013/12/20/properties",
layout: 'default',
name: nil,
# path: "properties.html",
permalink: nil,
published: nil,
tags: %w(ay bee cee),
title: 'Properties Post',
url: "/foo/bar/baz/2013/12/20/properties.html"
}
attrs.each do |attr, val|
attr_str = attr.to_s
result = post[attr_str]
assert_equal val, result, "For <post[\"#{attr_str}\"]>:"
end
end
context "processing posts" do context "processing posts" do
setup do setup do
@post = Post.allocate @post = Post.allocate

View File

@ -176,7 +176,7 @@ class TestSite < Test::Unit::TestCase
assert_equal posts.size - @num_invalid_posts, @site.posts.size assert_equal posts.size - @num_invalid_posts, @site.posts.size
assert_equal categories, @site.categories.keys.sort assert_equal categories, @site.categories.keys.sort
assert_equal 4, @site.categories['foo'].size assert_equal 5, @site.categories['foo'].size
end end
context 'error handling' do context 'error handling' do