Merge remote branch 'cblunt/limit_posts'
This commit is contained in:
commit
054b796b9f
11
bin/jekyll
11
bin/jekyll
|
@ -78,6 +78,17 @@ opts = OptionParser.new do |opts|
|
||||||
end
|
end
|
||||||
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|
|
opts.on("--url [URL]", "Set custom site.url") do |url|
|
||||||
options['url'] = url
|
options['url'] = url
|
||||||
end
|
end
|
||||||
|
|
|
@ -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/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 "_includes/jekyll.textile" file that contains "Jekyll"
|
||||||
And I have an "index.html" page that contains "Basic Site with include tag: {% include about.textile %}"
|
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
|
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"
|
|
@ -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 "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 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"
|
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
|
||||||
|
|
|
@ -3,7 +3,8 @@ module Jekyll
|
||||||
class Site
|
class Site
|
||||||
attr_accessor :config, :layouts, :posts, :pages, :static_files,
|
attr_accessor :config, :layouts, :posts, :pages, :static_files,
|
||||||
:categories, :exclude, :source, :dest, :lsi, :pygments,
|
: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
|
attr_accessor :converters, :generators
|
||||||
|
|
||||||
# Initialize the site
|
# Initialize the site
|
||||||
|
@ -22,6 +23,7 @@ module Jekyll
|
||||||
self.permalink_style = config['permalink'].to_sym
|
self.permalink_style = config['permalink'].to_sym
|
||||||
self.exclude = config['exclude'] || []
|
self.exclude = config['exclude'] || []
|
||||||
self.future = config['future']
|
self.future = config['future']
|
||||||
|
self.limit_posts = config['limit_posts'] || nil
|
||||||
|
|
||||||
self.reset
|
self.reset
|
||||||
self.setup
|
self.setup
|
||||||
|
@ -39,6 +41,8 @@ module Jekyll
|
||||||
self.static_files = []
|
self.static_files = []
|
||||||
self.categories = Hash.new { |hash, key| hash[key] = [] }
|
self.categories = Hash.new { |hash, key| hash[key] = [] }
|
||||||
self.tags = 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
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
@ -122,6 +126,9 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
self.posts.sort!
|
self.posts.sort!
|
||||||
|
|
||||||
|
# limit the posts if :limit_posts option is set
|
||||||
|
self.posts = self.posts[-limit_posts, limit_posts] if limit_posts
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate
|
def generate
|
||||||
|
|
|
@ -41,4 +41,32 @@ class TestGeneratedSite < Test::Unit::TestCase
|
||||||
assert File.exists?(dest_dir('/contacts.html'))
|
assert File.exists?(dest_dir('/contacts.html'))
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue