From 0ec9a1330a29907c60f2b3431cd137454fc681f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Arild=20M=C3=A6land?= Date: Sun, 22 Feb 2009 19:09:16 +0100 Subject: [PATCH 1/3] Made it possible to enter categories from YAML as an array. --- lib/jekyll/post.rb | 8 +++++++- .../_posts/2009-01-27-array-categories.textile | 10 ++++++++++ test/test_post.rb | 14 ++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 test/source/_posts/2009-01-27-array-categories.textile diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 0e2af904..7d2bcad5 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -49,7 +49,13 @@ module Jekyll if self.data.has_key?('category') self.categories << self.data['category'] elsif self.data.has_key?('categories') - self.categories = self.data['categories'].split + # Look for categories in the YAML-header, either specified as + # an array or a string. + if self.data['categories'].kind_of? String + self.categories = self.data['categories'].split + else + self.categories = self.data['categories'] + end end end end diff --git a/test/source/_posts/2009-01-27-array-categories.textile b/test/source/_posts/2009-01-27-array-categories.textile new file mode 100644 index 00000000..575843d6 --- /dev/null +++ b/test/source/_posts/2009-01-27-array-categories.textile @@ -0,0 +1,10 @@ +--- +layout: default +title: Array categories in YAML +categories: +- foo +- bar +- baz +--- + +Best *post* ever diff --git a/test/test_post.rb b/test/test_post.rb index 0a118df3..4627d43c 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -87,10 +87,16 @@ class TestPost < Test::Unit::TestCase end def test_yaml_categories - p = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', "2009-01-27-categories.textile") - assert p.categories.include?('foo') - assert p.categories.include?('bar') - assert p.categories.include?('baz') + p1 = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', + "2009-01-27-categories.textile") + p2 = Post.new(File.join(File.dirname(__FILE__), *%w[source]), '', + "2009-01-27-array-categories.textile") + + [p1, p2].each do |p| + assert p.categories.include?('foo') + assert p.categories.include?('bar') + assert p.categories.include?('baz') + end end def test_render From 25d4951f0b98da589dd3a12abef458613623ee6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Arild=20M=C3=A6land?= Date: Sun, 22 Feb 2009 22:58:47 +0100 Subject: [PATCH 2/3] Also ignore Emacs autosave files --- lib/jekyll/site.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 9b96ee6e..4ff5123d 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -93,7 +93,7 @@ module Jekyll # Copy all regular files from to / ignoring # any files/directories that are hidden or backup files (start - # with "." or end with "~") or contain site content (start with "_") + # with "." or "#" or end with "~") or contain site content (start with "_") # unless they are "_posts" directories or web server files such as # '.htaccess' # The +dir+ String is a relative path used to call this method @@ -105,7 +105,7 @@ module Jekyll entries = Dir.entries(base) entries = entries.reject { |e| e[-1..-1] == '~' } entries = entries.reject do |e| - (e != '_posts') and ['.', '_'].include?(e[0..0]) unless ['.htaccess'].include?(e) + (e != '_posts') and ['.', '_', '#'].include?(e[0..0]) unless ['.htaccess'].include?(e) end directories = entries.select { |e| File.directory?(File.join(base, e)) } files = entries.reject { |e| File.directory?(File.join(base, e)) } From fab8442432f473ba647c682608bc8ff9ced6cca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Arild=20M=C3=A6land?= Date: Wed, 25 Feb 2009 21:44:07 +0100 Subject: [PATCH 3/3] Factored the filtering code into a method --- lib/jekyll/site.rb | 39 +++++++++++++++++++++------------------ test/test_site.rb | 9 +++++++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 4ff5123d..18dccc87 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -28,16 +28,14 @@ module Jekyll self.transform_pages self.write_posts end - - # Read all the files in /_layouts except backup files - # (end with "~") into memory for later use. + + # Read all the files in /_layouts into memory for later use. # # Returns nothing def read_layouts base = File.join(self.source, "_layouts") - entries = Dir.entries(base) - entries = entries.reject { |e| e[-1..-1] == '~' } - entries = entries.reject { |e| File.directory?(File.join(base, e)) } + entries = [] + Dir.chdir(base) { entries = filter_entries(Dir['*.*']) } entries.each do |f| name = f.split(".")[0..-2].join(".") @@ -47,17 +45,13 @@ module Jekyll # ignore missing layout dir end - # Read all the files in /_posts except backup files (end with "~") - # and create a new Post object with each one. + # Read all the files in /_posts and create a new Post object with each one. # # Returns nothing def read_posts(dir) base = File.join(self.source, dir, '_posts') - entries = [] - Dir.chdir(base) { entries = Dir['**/*'] } - entries = entries.reject { |e| e[-1..-1] == '~' } - entries = entries.reject { |e| File.directory?(File.join(base, e)) } + Dir.chdir(base) { entries = filter_entries(Dir['**/*']) } # first pass processes, but does not yet render post content entries.each do |f| @@ -102,11 +96,7 @@ module Jekyll # Returns nothing def transform_pages(dir = '') base = File.join(self.source, dir) - entries = Dir.entries(base) - entries = entries.reject { |e| e[-1..-1] == '~' } - entries = entries.reject do |e| - (e != '_posts') and ['.', '_', '#'].include?(e[0..0]) unless ['.htaccess'].include?(e) - end + entries = filter_entries(Dir.entries(base)) directories = entries.select { |e| File.directory?(File.join(base, e)) } files = entries.reject { |e| File.directory?(File.join(base, e)) } @@ -165,6 +155,19 @@ module Jekyll "topics" => post_attr_hash('topics') }} end - end + # Filter out any files/directories that are hidden or backup files (start + # with "." or "#" or end with "~") or contain site content (start with "_") + # unless they are "_posts" directories or web server files such as + # '.htaccess' + def filter_entries(entries) + entries = entries.reject do |e| + unless ['_posts', '.htaccess'].include?(e) + # Reject backup/hidden + ['.', '_', '#'].include?(e[0..0]) or e[-1..-1] == '~' + end + end + end + + end end diff --git a/test/test_site.rb b/test/test_site.rb index e19eefcd..62f05fdf 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -33,4 +33,13 @@ class TestSite < Test::Unit::TestCase assert_equal categories, @s.categories.keys.sort assert_equal 3, @s.categories['foo'].size end + + def test_filter_entries + ent1 = %w[foo.markdown bar.markdown baz.markdown #baz.markdown# + .baz.markdow foo.markdown~] + ent2 = %w[.htaccess _posts bla.bla] + + assert_equal %w[foo.markdown bar.markdown baz.markdown], @s.filter_entries(ent1) + assert_equal ent2, @s.filter_entries(ent2) + end end