From 22e1e5f28ca5e5ab0130a4e62bdecff8960803a9 Mon Sep 17 00:00:00 2001 From: liufengyun Date: Sun, 12 Jan 2014 17:40:14 +0800 Subject: [PATCH] make sure pages with published being false are not generated --- lib/jekyll/convertible.rb | 5 +++++ lib/jekyll/post.rb | 12 +----------- lib/jekyll/site.rb | 5 +++-- test/source/unpublished.html | 7 +++++++ test/test_generated_site.rb | 4 ++++ test/test_page.rb | 5 +++++ test/test_post.rb | 4 ++-- 7 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 test/source/unpublished.html diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index f838d29b..ea0bcbbf 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -21,6 +21,11 @@ module Jekyll self.content || '' end + # Whether the file is published or not, as indicated in YAML front-matter + def published? + !(self.data.has_key?('published') && self.data['published'] == false) + end + # Returns merged option hash for File.read of self.site (if exists) # and a given param def merged_file_read_opts(opts) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 23e131ee..49214615 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -35,7 +35,7 @@ module Jekyll attr_accessor :site attr_accessor :data, :extracted_excerpt, :content, :output, :ext - attr_accessor :date, :slug, :published, :tags, :categories + attr_accessor :date, :slug, :tags, :categories attr_reader :name @@ -60,20 +60,10 @@ module Jekyll self.date = Time.parse(self.data["date"].to_s) end - self.published = self.published? - self.populate_categories self.populate_tags end - def published? - if self.data.has_key?('published') && self.data['published'] == false - false - else - true - end - end - def populate_categories if self.categories.empty? self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.to_s.downcase} diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index c3d68507..d201f025 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -170,7 +170,8 @@ module Jekyll f_rel = File.join(dir, f) read_directories(f_rel) unless self.dest.sub(/\/$/, '') == f_abs elsif has_yaml_header?(f_abs) - pages << Page.new(self, self.source, dir, f) + page = Page.new(self, self.source, dir, f) + pages << page if page.published? else static_files << StaticFile.new(self, self.source, dir, f) end @@ -189,7 +190,7 @@ module Jekyll posts = read_content(dir, '_posts', Post) posts.each do |post| - if post.published && (self.future || post.date <= self.time) + if post.published? && (self.future || post.date <= self.time) aggregate_post_info(post) end end diff --git a/test/source/unpublished.html b/test/source/unpublished.html new file mode 100644 index 00000000..265b6062 --- /dev/null +++ b/test/source/unpublished.html @@ -0,0 +1,7 @@ +--- +layout: default +title: Not published! +published: false +--- + +This should *not* be published! diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 2afbb1a7..a27477a2 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -32,6 +32,10 @@ class TestGeneratedSite < Test::Unit::TestCase assert_equal "published.html", published.first end + should "hide unpublished page" do + assert !File.exists?(dest_dir('/unpublished.html')) + end + should "not copy _posts directory" do assert !File.exist?(dest_dir('_posts')) end diff --git a/test/test_page.rb b/test/test_page.rb index 1147a63d..ef2f175b 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -25,6 +25,11 @@ class TestPage < Test::Unit::TestCase assert_equal "/contacts.html", @page.url end + should "not published when published yaml is false" do + @page = setup_page("unpublished.html") + assert_equal false, @page.published? + end + context "in a directory hierarchy" do should "create url based on filename" do @page = setup_page('/contacts', 'bar.html') diff --git a/test/test_post.rb b/test/test_post.rb index 418e60d7..c159afe8 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -387,12 +387,12 @@ class TestPost < Test::Unit::TestCase context "initializing posts" do should "publish when published yaml is no specified" do post = setup_post("2008-02-02-published.textile") - assert_equal true, post.published + assert_equal true, post.published? end should "not published when published yaml is false" do post = setup_post("2008-02-02-not-published.textile") - assert_equal false, post.published + assert_equal false, post.published? end should "recognize date in yaml" do