Skip to content

Commit

Permalink
Added limit-posts option to site configuration.
Browse files Browse the repository at this point in the history
  * Added unit tests for limit-posts.
  * Added feature for limit-posts.
  * Added --limit_posts option to bin/jekyll options parser
  • Loading branch information
cblunt committed Sep 12, 2010
1 parent 3fa9af1 commit f688c9d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bin/jekyll
Expand Up @@ -78,6 +78,17 @@ opts = OptionParser.new do |opts|
end
end

opts.on("--limit_posts [MAX_POSTS]", "Limit the number of posts to publish") do |limit_posts|
begin
options['limit_posts'] = limit_posts.to_i
raise ArgumentError if options['limit_posts'] < 1
rescue
puts 'you must specify a number of posts by page bigger than 0'
exit 0
end
end


opts.on("--url [URL]", "Set custom site.url") do |url|
options['url'] = url
end
Expand Down
4 changes: 2 additions & 2 deletions features/create_sites.feature
Expand Up @@ -89,6 +89,6 @@ Feature: Create sites
And I have an "_includes/about.textile" file that contains "Generated by {% include jekyll.textile %}"
And I have an "_includes/jekyll.textile" file that contains "Jekyll"
And I have an "index.html" page that contains "Basic Site with include tag: {% include about.textile %}"
When I run jekyll
When I debug jekyll
Then the _site directory should exist
And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
16 changes: 16 additions & 0 deletions features/site_configuration.feature
Expand Up @@ -101,3 +101,19 @@ Feature: Site configuration
And I should see "Page Layout: 2 on 2010-01-01" in "_site/index.html"
And I should see "Post Layout: <p>content for entry1.</p>" in "_site/2007/12/31/entry1.html"
And I should see "Post Layout: <p>content for entry2.</p>" in "_site/2020/01/31/entry2.html"

Scenario: Limit the number of posts generated by most recent date
Given I have a _posts directory
And I have a configuration file with:
| key | value |
| limit_posts | 2 |
And I have the following posts:
| title | date | content |
| Apples | 3/27/2009 | An article about apples |
| Oranges | 4/1/2009 | An article about oranges |
| Bananas | 4/5/2009 | An article about bananas |
When I run jekyll
Then the _site directory should exist
And the "_site/2009/04/05/bananas.html" file should exist
And the "_site/2009/04/01/oranges.html" file should exist
And the "_site/2009/03/27/apples.html" file should not exist
9 changes: 8 additions & 1 deletion lib/jekyll/site.rb
Expand Up @@ -3,7 +3,8 @@ module Jekyll
class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files,
:categories, :exclude, :source, :dest, :lsi, :pygments,
:permalink_style, :tags, :time, :future, :safe, :plugins
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts

attr_accessor :converters, :generators

# Initialize the site
Expand All @@ -22,6 +23,7 @@ def initialize(config)
self.permalink_style = config['permalink'].to_sym
self.exclude = config['exclude'] || []
self.future = config['future']
self.limit_posts = config['limit_posts'] || nil

self.reset
self.setup
Expand All @@ -39,6 +41,8 @@ def reset
self.static_files = []
self.categories = Hash.new { |hash, key| hash[key] = [] }
self.tags = Hash.new { |hash, key| hash[key] = [] }

raise ArgumentError, "Limit posts must be nil or >= 1" if !self.limit_posts.nil? && self.limit_posts < 1
end

def setup
Expand Down Expand Up @@ -122,6 +126,9 @@ def read_posts(dir)
end

self.posts.sort!

# limit the posts if :limit_posts option is set
self.posts = self.posts[-limit_posts, limit_posts] if limit_posts
end

def generate
Expand Down
28 changes: 28 additions & 0 deletions test/test_generated_site.rb
Expand Up @@ -41,4 +41,32 @@ class TestGeneratedSite < Test::Unit::TestCase
assert File.exists?(dest_dir('/contacts.html'))
end
end

context "generating limited posts" do
setup do
clear_dest
stub(Jekyll).configuration do
Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 5})
end

@site = Site.new(Jekyll.configuration)
@site.process
@index = File.read(dest_dir('index.html'))
end

should "generate only the specified number of posts" do
assert_equal 5, @site.posts.size
end

should "ensure limit posts is 1 or more" do
assert_raise ArgumentError do
clear_dest
stub(Jekyll).configuration do
Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'limit_posts' => 0})
end

@site = Site.new(Jekyll.configuration)
end
end
end
end

0 comments on commit f688c9d

Please sign in to comment.