Merge pull request #1849 from afeld/sort-attributes

This commit is contained in:
Matt Rogers 2013-12-30 20:31:45 -08:00
commit a2fd8ba7c3
11 changed files with 137 additions and 33 deletions

View File

@ -58,3 +58,16 @@ Feature: Embed filters
Then the _site directory should exist
And I should see "By <p><em>Obi-wan</em></p>" in "_site/2009/03/27/star-wars.html"
Scenario: Sort by an arbitrary variable
Given I have a _layouts directory
And I have the following page:
| title | layout | value | content |
| Page-1 | default | 8 | Something |
And I have the following page:
| title | layout | value | content |
| Page-2 | default | 6 | Something |
And I have a default layout that contains "{{ site.pages | sort:'value' | map:'title' | join:', ' }}"
When I run jekyll
Then the _site directory should exist
And I should see exactly "Page-2, Page-1" in "_site/page-1.html"
And I should see exactly "Page-2, Page-1" in "_site/page-2.html"

View File

@ -1,3 +1,22 @@
def file_content_from_hash(input_hash)
matter_hash = input_hash.reject { |k, v| k == "content" }
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp
content = if input_hash['input'] && input_hash['filter']
"{{ #{input_hash['input']} | #{input_hash['filter']} }}"
else
input_hash['content']
end
<<EOF
---
#{matter}
---
#{content}
EOF
end
Before do
FileUtils.mkdir(TEST_DIR)
Dir.chdir(TEST_DIR)
@ -62,42 +81,28 @@ Given /^I have an? (.*) directory$/ do |dir|
FileUtils.mkdir_p(dir)
end
Given /^I have the following (draft|post)s?(?: (in|under) "([^"]+)")?:$/ do |status, direction, folder, table|
table.hashes.each do |post|
title = slug(post['title'])
ext = post['type'] || 'textile'
Given /^I have the following (draft|page|post)s?(?: (in|under) "([^"]+)")?:$/ do |status, direction, folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash['title'])
ext = input_hash['type'] || 'textile'
before, after = location(folder, direction)
if "draft" == status
folder_post = '_drafts'
case status
when "draft"
dest_folder = '_drafts'
filename = "#{title}.#{ext}"
elsif "post" == status
parsed_date = Time.xmlschema(post['date']) rescue Time.parse(post['date'])
folder_post = '_posts'
when "page"
dest_folder = ''
filename = "#{title}.#{ext}"
when "post"
parsed_date = Time.xmlschema(input_hash['date']) rescue Time.parse(input_hash['date'])
dest_folder = '_posts'
filename = "#{parsed_date.strftime('%Y-%m-%d')}-#{title}.#{ext}"
end
path = File.join(before, folder_post, after, filename)
matter_hash = {}
%w(title layout tag tags category categories published author path date permalink).each do |key|
matter_hash[key] = post[key] if post[key]
end
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp
content = if post['input'] && post['filter']
"{{ #{post['input']} | #{post['filter']} }}"
else
post['content']
end
path = File.join(before, dest_folder, after, filename)
File.open(path, 'w') do |f|
f.write <<EOF
---
#{matter}
---
#{content}
EOF
f.write file_content_from_hash(input_hash)
end
end
end

View File

@ -171,5 +171,18 @@ module Jekyll
f.write(self.output)
end
end
# Accessor for data properties by Liquid.
#
# property - The String name of the property to retrieve.
#
# 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

View File

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

View File

@ -9,6 +9,7 @@ module Jekyll
EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[
title
url
dir
date
id
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"]
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

View File

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

View File

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

View File

@ -157,7 +157,7 @@ class TestSite < Test::Unit::TestCase
should "sort pages alphabetically" do
stub.proxy(Dir).entries { |entries| entries.reverse }
@site.process
sorted_pages = %w(.htaccess about.html bar.html contacts.html deal.with.dots.html foo.md index.html index.html sitemap.xml symlinked-file)
sorted_pages = %w(.htaccess about.html bar.html contacts.html deal.with.dots.html foo.md index.html index.html properties.html sitemap.xml symlinked-file)
assert_equal sorted_pages, @site.pages.map(&:name)
end
@ -183,7 +183,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