Permalink
Browse files

Adding tags to the site payload. Derived mostly from Henrik's impleme…

…ntation in 072d9e7
  • Loading branch information...
1 parent 99098dd commit 86b1450234be7b4a530629124800edcdf9e78bfe @qrush qrush committed May 18, 2009
Showing with 23 additions and 5 deletions.
  1. +10 −0 features/site_data.feature
  2. +9 −5 lib/jekyll/site.rb
  3. +4 −0 test/test_site.rb
View
10 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"
View
14 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 @@ def initialize(config)
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 @@ def read_posts(dir)
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 @@ def read_posts(dir)
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 @@ def site_payload
{"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
View
4 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

0 comments on commit 86b1450

Please sign in to comment.