Refactored site.rb, I tried to clean a bunch of the methods code climate was complaining about

This commit is contained in:
John Piasetzki 2013-05-23 22:06:15 -04:00
parent 8eec9f7759
commit b5d4a96149
1 changed files with 22 additions and 38 deletions

View File

@ -138,34 +138,19 @@ module Jekyll
entries = Dir.chdir(base) { filter_entries(Dir.entries('.')) }
self.read_posts(dir)
if self.show_drafts
self.read_drafts(dir)
end
self.read_drafts(dir) if self.show_drafts
self.posts.sort!
# limit the posts if :limit_posts option is set
if limit_posts > 0
limit = self.posts.length < limit_posts ? self.posts.length : limit_posts
self.posts = self.posts[-limit, limit]
end
limit_posts! if limit_posts > 0 # limit the posts if :limit_posts option is set
entries.each do |f|
f_abs = File.join(base, f)
f_rel = File.join(dir, f)
if File.directory?(f_abs)
next if self.dest.sub(/\/$/, '') == f_abs
read_directories(f_rel)
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)
else
first3 = File.open(f_abs) { |fd| fd.read(3) }
if first3 == "---"
# file appears to have a YAML header so process it as a page
pages << Page.new(self, self.source, dir, f)
else
# otherwise treat it as a static file
static_files << StaticFile.new(self, self.source, dir, f)
end
static_files << StaticFile.new(self, self.source, dir, f)
end
end
end
@ -255,14 +240,8 @@ module Jekyll
# files to be written
files = Set.new
self.posts.each do |post|
files << post.destination(self.dest)
end
self.pages.each do |page|
files << page.destination(self.dest)
end
self.static_files.each do |sf|
files << sf.destination(self.dest)
[self.posts, self.pages, self.static_files].flatten.each do |item|
files << item.destination(self.dest)
end
# adding files' parent directories
@ -290,14 +269,8 @@ module Jekyll
#
# Returns nothing.
def write
self.posts.each do |post|
post.write(self.dest)
end
self.pages.each do |page|
page.write(self.dest)
end
self.static_files.each do |sf|
sf.write(self.dest)
[self.posts, self.pages, self.static_files].flatten.each do |item|
item.write(self.dest)
end
end
@ -430,5 +403,16 @@ module Jekyll
@deprecated_relative_permalinks = true
end
end
private
def has_yaml_header?(file)
"---" == File.open(file) { |fd| fd.read(3) }
end
def limit_posts!
limit = self.posts.length < limit_posts ? self.posts.length : limit_posts
self.posts = self.posts[-limit, limit]
end
end
end