make non-data properties/methods accessible to Liquid
per https://github.com/jekyll/jekyll/pull/1849/files#r8490593
This commit is contained in:
parent
c2b750448e
commit
2bfafb3b33
|
@ -177,7 +177,11 @@ module Jekyll
|
|||
#
|
||||
# Returns the String value or nil if the property isn't included.
|
||||
def [](property)
|
||||
if self.class::ATTRIBUTES_FOR_LIQUID.include?(property)
|
||||
send(property)
|
||||
else
|
||||
data[property]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,9 +9,11 @@ module Jekyll
|
|||
|
||||
# Attributes for Liquid templates
|
||||
ATTRIBUTES_FOR_LIQUID = %w[
|
||||
url
|
||||
content
|
||||
dir
|
||||
name
|
||||
path
|
||||
url
|
||||
]
|
||||
|
||||
# Initialize a new Page.
|
||||
|
|
|
@ -9,6 +9,7 @@ module Jekyll
|
|||
EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[
|
||||
title
|
||||
url
|
||||
dir
|
||||
date
|
||||
id
|
||||
categories
|
||||
|
|
|
@ -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.
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
foo: bar
|
||||
layout: default
|
||||
permalink: /properties/
|
||||
title: Properties Page
|
||||
---
|
||||
|
||||
All the properties.
|
|
@ -120,7 +120,7 @@ class TestFilters < Test::Unit::TestCase
|
|||
case g["name"]
|
||||
when "default"
|
||||
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"
|
||||
assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array."
|
||||
assert_equal 2, g["items"].size
|
||||
|
|
|
@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "ensure post count is as expected" do
|
||||
assert_equal 36, @site.posts.size
|
||||
assert_equal 37, @site.posts.size
|
||||
end
|
||||
|
||||
should "insert site.posts into the index" do
|
||||
|
|
|
@ -47,6 +47,29 @@ class TestPage < Test::Unit::TestCase
|
|||
assert_equal "deal.with.dots", @page.basename
|
||||
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
|
||||
setup do
|
||||
@site.permalink_style = :pretty
|
||||
|
|
|
@ -25,6 +25,34 @@ class TestPost < Test::Unit::TestCase
|
|||
assert !Post.valid?("blah")
|
||||
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
|
||||
setup do
|
||||
@post = Post.allocate
|
||||
|
|
|
@ -176,7 +176,7 @@ class TestSite < Test::Unit::TestCase
|
|||
|
||||
assert_equal posts.size - @num_invalid_posts, @site.posts.size
|
||||
assert_equal categories, @site.categories.keys.sort
|
||||
assert_equal 4, @site.categories['foo'].size
|
||||
assert_equal 5, @site.categories['foo'].size
|
||||
end
|
||||
|
||||
context 'error handling' do
|
||||
|
|
Loading…
Reference in New Issue