diff --git a/bin/jekyll b/bin/jekyll index 94a5c090bb2..9916a82810d 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -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 diff --git a/features/create_sites.feature b/features/create_sites.feature index 8517e28d036..a7b5d9b1295 100644 --- a/features/create_sites.feature +++ b/features/create_sites.feature @@ -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" \ No newline at end of file + And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html" diff --git a/features/site_configuration.feature b/features/site_configuration.feature index 6f1c600e96d..38a315aec2d 100644 --- a/features/site_configuration.feature +++ b/features/site_configuration.feature @@ -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:

content for entry1.

" in "_site/2007/12/31/entry1.html" And I should see "Post Layout:

content for entry2.

" 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 diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index d158e33b7bd..331b6c07a25 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index bfb86daa05e..cba75649a2a 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -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