From 86b1450234be7b4a530629124800edcdf9e78bfe Mon Sep 17 00:00:00 2001 From: Nick Quaranto Date: Mon, 18 May 2009 18:27:12 -0400 Subject: [PATCH] Adding tags to the site payload. Derived mostly from Henrik's implementation in 072d9e7 --- features/site_data.feature | 10 ++++++++++ lib/jekyll/site.rb | 14 +++++++++----- test/test_site.rb | 4 ++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/features/site_data.feature b/features/site_data.feature index d9d5c775..46d36cac 100644 --- a/features/site_data.feature +++ b/features/site_data.feature @@ -49,3 +49,13 @@ Feature: Site data When I run jekyll Then the _site directory should exist And I should see "Awesome Hack" in "_site/index.html" + + Scenario: Use site.tags variable + Given I have a _posts directory + And I have an "index.html" page that contains "{% for post in site.tags.beer %} {{ post.content }} {% endfor %}" + And I have the following posts: + | title | date | tag | content | + | Delicious Beer | 3/26/2009 | beer | 1) Yuengling | + When I run jekyll + Then the _site directory should exist + And I should see "Yuengling" in "_site/index.html" diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 9964cc69..abf19409 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -1,8 +1,8 @@ module Jekyll class Site - attr_accessor :config, :layouts, :posts, :categories, :exclude - attr_accessor :source, :dest, :lsi, :pygments, :permalink_style + attr_accessor :config, :layouts, :posts, :categories, :exclude, + :source, :dest, :lsi, :pygments, :permalink_style, :tags # Initialize the site # +config+ is a Hash containing site configurations details @@ -25,7 +25,8 @@ module Jekyll def reset self.layouts = {} self.posts = [] - self.categories = Hash.new { |hash, key| hash[key] = Array.new } + self.categories = Hash.new { |hash, key| hash[key] = [] } + self.tags = Hash.new { |hash, key| hash[key] = [] } end def setup @@ -126,6 +127,7 @@ module Jekyll if post.published self.posts << post post.categories.each { |c| self.categories[c] << post } + post.tags.each { |c| self.tags[c] << post } end end end @@ -137,7 +139,8 @@ module Jekyll post.render(self.layouts, site_payload) end - self.categories.values.map { |cats| cats.sort! { |a, b| b <=> a} } + self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a} } + self.tags.values.map { |ps| ps.sort! { |a, b| b <=> a} } rescue Errno::ENOENT => e # ignore missing layout dir end @@ -219,7 +222,8 @@ module Jekyll {"site" => { "time" => Time.now, "posts" => self.posts.sort { |a,b| b <=> a }, - "categories" => post_attr_hash('categories')}} + "categories" => post_attr_hash('categories'), + "tags" => post_attr_hash('tags')}} end # Filter out any files/directories that are hidden or backup files (start diff --git a/test/test_site.rb b/test/test_site.rb index a99b8c6d..80bf0468 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -9,6 +9,10 @@ class TestSite < Test::Unit::TestCase @site = Site.new(Jekyll.configuration) end + should "have an empty tag hash by default" do + assert_equal Hash.new, @site.tags + end + should "reset data before processing" do clear_dest @site.process