Skip to content

Commit

Permalink
Merge commit 'fab8442432f473ba647c682608bc8ff9ced6cca2'
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Mar 10, 2009
2 parents 5e725eb + fab8442 commit 54d713b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
8 changes: 7 additions & 1 deletion lib/jekyll/post.rb
Expand Up @@ -49,7 +49,13 @@ def initialize(source, dir, name)
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
Expand Down
41 changes: 22 additions & 19 deletions lib/jekyll/site.rb
Expand Up @@ -28,16 +28,14 @@ def process
self.transform_pages
self.write_posts
end

# Read all the files in <source>/_layouts except backup files
# (end with "~") into memory for later use.

# Read all the files in <source>/_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(".")
Expand All @@ -47,17 +45,13 @@ def read_layouts
# ignore missing layout dir
end

# Read all the files in <base>/_posts except backup files (end with "~")
# and create a new Post object with each one.
# Read all the files in <base>/_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|
Expand Down Expand Up @@ -93,7 +87,7 @@ def write_posts

# Copy all regular files from <source> to <dest>/ 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
Expand All @@ -102,11 +96,7 @@ def write_posts
# 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)) }

Expand Down Expand Up @@ -165,6 +155,19 @@ def site_payload
"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
10 changes: 10 additions & 0 deletions 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
14 changes: 10 additions & 4 deletions test/test_post.rb
Expand Up @@ -87,10 +87,16 @@ def test_yaml_category
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
Expand Down
9 changes: 9 additions & 0 deletions test/test_site.rb
Expand Up @@ -33,4 +33,13 @@ def test_site_payload
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

0 comments on commit 54d713b

Please sign in to comment.