From 5b2480c8ba093f550f46dd3aa3eecbb624f4e5c3 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 19 Dec 2013 01:58:46 -0500 Subject: [PATCH 1/7] refactor file+matter creation in cucumber steps --- features/step_definitions/jekyll_steps.rb | 42 +++++++++++++---------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 43c7e31d..e3e3945d 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -1,3 +1,25 @@ +def file_content_from_hash(input_hash) + matter_hash = {} + %w(title layout tag tags category categories published author path date permalink).each do |key| + matter_hash[key] = input_hash[key] if input_hash[key] + end + 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 + + < Date: Thu, 19 Dec 2013 02:19:49 -0500 Subject: [PATCH 2/7] refactor step to support page creation --- features/step_definitions/jekyll_steps.rb | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index e3e3945d..8ff7500e 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -1,8 +1,5 @@ def file_content_from_hash(input_hash) - matter_hash = {} - %w(title layout tag tags category categories published author path date permalink).each do |key| - matter_hash[key] = input_hash[key] if input_hash[key] - end + 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'] @@ -84,24 +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) + path = File.join(before, dest_folder, after, filename) File.open(path, 'w') do |f| - f.write file_content_from_hash(post) + f.write file_content_from_hash(input_hash) end end end From 35712dcadc781770c0fc2e5176d79e3aed9ecd6f Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 19 Dec 2013 03:35:57 -0500 Subject: [PATCH 3/7] add failing test for sorting pages by custom variables --- features/embed_filters.feature | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/features/embed_filters.feature b/features/embed_filters.feature index d61901c8..077d6349 100644 --- a/features/embed_filters.feature +++ b/features/embed_filters.feature @@ -58,3 +58,18 @@ Feature: Embed filters Then the _site directory should exist And I should see "By

Obi-wan

" 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:', ' }}" + # And I have a default layout that contains "{% assign pages = site.pages | sort:'value' %}{% for pg in pages %}{{ pg.value }}" + # And I have a default layout that contains "{% for pg in site.pages %}{{ pg.value }}{% endfor %}" + 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" From 8c1706b66e2c147d8ada59188f2315d74a97f486 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 19 Dec 2013 03:36:48 -0500 Subject: [PATCH 4/7] clean up custom variable sort feature --- features/embed_filters.feature | 2 -- 1 file changed, 2 deletions(-) diff --git a/features/embed_filters.feature b/features/embed_filters.feature index 077d6349..9d513ec2 100644 --- a/features/embed_filters.feature +++ b/features/embed_filters.feature @@ -67,8 +67,6 @@ Feature: Embed filters | title | layout | value | content | | Page-2 | default | 6 | Something | And I have a default layout that contains "{{ site.pages | sort:'value' | map:'title' | join:', ' }}" - # And I have a default layout that contains "{% assign pages = site.pages | sort:'value' %}{% for pg in pages %}{{ pg.value }}" - # And I have a default layout that contains "{% for pg in site.pages %}{{ pg.value }}{% endfor %}" When I run jekyll Then the _site directory should exist And I should see exactly "Page-2, Page-1" in "_site/page-1.html" From c2b750448e4389d5e766b77ce43f7e89ad0a9bee Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 19 Dec 2013 17:14:51 -0500 Subject: [PATCH 5/7] allow data attribute access by Liquid on Convertible items --- lib/jekyll/convertible.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 815c36b0..cdbe48c3 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -170,5 +170,14 @@ 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) + data[property] + end end end From 2bfafb3b33b801212f390d8fec1c386106e78b5b Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Sun, 22 Dec 2013 03:38:13 -0500 Subject: [PATCH 6/7] 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 From 9d7c5245300ccacfc687c7d2d7140bfeb908ac61 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Sun, 22 Dec 2013 19:33:00 -0500 Subject: [PATCH 7/7] fix file sorting test --- test/test_site.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_site.rb b/test/test_site.rb index aa22e8a6..3a64f43d 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -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