From 2bfafb3b33b801212f390d8fec1c386106e78b5b Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Sun, 22 Dec 2013 03:38:13 -0500 Subject: [PATCH] make non-data properties/methods accessible to Liquid per https://github.com/jekyll/jekyll/pull/1849/files#r8490593 --- lib/jekyll/convertible.rb | 6 +++- lib/jekyll/page.rb | 4 ++- lib/jekyll/post.rb | 1 + test/source/_posts/2013-12-20-properties.text | 11 ++++++++ test/source/properties.html | 8 ++++++ test/test_filters.rb | 2 +- test/test_generated_site.rb | 2 +- test/test_page.rb | 23 +++++++++++++++ test/test_post.rb | 28 +++++++++++++++++++ test/test_site.rb | 2 +- 10 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 test/source/_posts/2013-12-20-properties.text create mode 100644 test/source/properties.html diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index cdbe48c3..6b9894a3 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -177,7 +177,11 @@ module Jekyll # # Returns the String value or nil if the property isn't included. def [](property) - data[property] + if self.class::ATTRIBUTES_FOR_LIQUID.include?(property) + send(property) + else + data[property] + end end end end diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index dd602a03..469dbb53 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -9,9 +9,11 @@ module Jekyll # Attributes for Liquid templates ATTRIBUTES_FOR_LIQUID = %w[ - url content + dir + name path + url ] # Initialize a new Page. diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index da64af93..b8e277b1 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -9,6 +9,7 @@ module Jekyll EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[ title url + dir date id categories diff --git a/test/source/_posts/2013-12-20-properties.text b/test/source/_posts/2013-12-20-properties.text new file mode 100644 index 00000000..94ded598 --- /dev/null +++ b/test/source/_posts/2013-12-20-properties.text @@ -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. diff --git a/test/source/properties.html b/test/source/properties.html new file mode 100644 index 00000000..fd40c45c --- /dev/null +++ b/test/source/properties.html @@ -0,0 +1,8 @@ +--- +foo: bar +layout: default +permalink: /properties/ +title: Properties Page +--- + +All the properties. diff --git a/test/test_filters.rb b/test/test_filters.rb index 304c9fca..42b0a030 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -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 diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 801675bf..866f753c 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -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 diff --git a/test/test_page.rb b/test/test_page.rb index e3190563..1147a63d 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -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 :" + end + end + context "with pretty url style" do setup do @site.permalink_style = :pretty diff --git a/test/test_post.rb b/test/test_post.rb index a61fc674..418e60d7 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -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 :" + end + end + context "processing posts" do setup do @post = Post.allocate diff --git a/test/test_site.rb b/test/test_site.rb index 35359a2a..ccd021a0 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -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