Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --preview flag for rendering posts that are not published (#374 & #41) #513

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/jekyll
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ opts = OptionParser.new do |opts|
end
end

opts.on("--preview", "Publish posts that are from the future or marked unpublished") do
options['preview'] = true
end

opts.on("--url [URL]", "Set custom site.url") do |url|
options['url'] = url
end
Expand Down
47 changes: 34 additions & 13 deletions lib/jekyll/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
module Jekyll

class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files,
attr_accessor :config, :layouts, :posts, :unpublished_posts, :pages, :static_files,
:categories, :exclude, :include, :source, :dest, :lsi, :pygments,
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts,
:preview

attr_accessor :converters, :generators

Expand All @@ -26,6 +27,7 @@ def initialize(config)
self.include = config['include'] || []
self.future = config['future']
self.limit_posts = config['limit_posts'] || nil
self.preview = config['preview']

self.reset
self.setup
Expand All @@ -47,17 +49,18 @@ def process
#
# Returns nothing
def reset
self.time = if self.config['time']
Time.parse(self.config['time'].to_s)
else
Time.now
end
self.layouts = {}
self.posts = []
self.pages = []
self.static_files = []
self.categories = Hash.new { |hash, key| hash[key] = [] }
self.tags = Hash.new { |hash, key| hash[key] = [] }
self.time = if self.config['time']
Time.parse(self.config['time'].to_s)
else
Time.now
end
self.layouts = {}
self.posts = []
self.unpublished_posts = []
self.pages = []
self.static_files = []
self.categories = Hash.new { |hash, key| hash[key] = [] }
self.tags = Hash.new { |hash, key| hash[key] = [] }

if !self.limit_posts.nil? && self.limit_posts < 1
raise ArgumentError, "Limit posts must be nil or >= 1"
Expand Down Expand Up @@ -169,6 +172,8 @@ def read_posts(dir)
self.posts << post
post.categories.each { |c| self.categories[c] << post }
post.tags.each { |c| self.tags[c] << post }
else
self.unpublished_posts << post
end
end
end
Expand Down Expand Up @@ -198,6 +203,12 @@ def render
self.posts.each do |post|
post.render(self.layouts, site_payload)
end

if self.preview
self.unpublished_posts.sort.each do |post|
post.render(self.layouts, site_payload)
end
end

self.pages.each do |page|
page.render(self.layouts, site_payload)
Expand All @@ -224,6 +235,11 @@ def cleanup
self.posts.each do |post|
files << post.destination(self.dest)
end
if self.preview
self.unpublished_posts.each do |post|
files << post.destination(self.dest)
end
end
self.pages.each do |page|
files << page.destination(self.dest)
end
Expand All @@ -248,6 +264,11 @@ def write
self.posts.each do |post|
post.write(self.dest)
end
if self.preview
self.unpublished_posts.each do |post|
post.write(self.dest)
end
end
self.pages.each do |page|
page.write(self.dest)
end
Expand Down
21 changes: 21 additions & 0 deletions test/test_generated_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,25 @@ class TestGeneratedSite < Test::Unit::TestCase
end
end
end

context "generating with preview" do
setup do
clear_dest
stub(Jekyll).configuration do
Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'preview' => true})
end

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

should "publishes unpublished posts" do
published = Dir[dest_dir('publish_test/2008/02/02/*.html')].map {|f| File.basename(f)}

assert_equal 2, published.size
assert_equal %w{ not-published.html published.html}, published
end

end
end