Skip to content
Browse files

Merge remote branch 'cblunt/limit_posts'

  • Loading branch information...
2 parents c1a7662 + d97bcad commit 054b796b9fda01fa028f8777dba885c5771b73b3 @mojombo mojombo committed Nov 17, 2010
Showing with 65 additions and 3 deletions.
  1. +11 −0 bin/jekyll
  2. +2 −2 features/create_sites.feature
  3. +16 −0 features/site_configuration.feature
  4. +8 −1 lib/jekyll/site.rb
  5. +28 −0 test/test_generated_site.rb
View
11 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
View
4 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"
+ And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
View
16 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: <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
View
9 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
View
28 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

0 comments on commit 054b796

Please sign in to comment.
Something went wrong with that request. Please try again.