Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remove code duplication during site generation #838

Merged
merged 2 commits into from

2 participants

Matt Rogers Parker Moore
Matt Rogers
Owner

No description provided.

mattr- added some commits
Matt Rogers mattr- Remove duplication when generating list of entries to process
Posts and Drafts share the same logic to get the list of files to
process into objects when generating the site. Factor this logic into
its own method and use it when reading posts and reading drafts.
2dd9881
Matt Rogers mattr- Remove duplication when aggregating post information 6399ec9
Parker Moore
Owner

Mind writing tests for those 2 methods you wrote? Don't want to break them in the future. Thanks!

Matt Rogers
Owner
Parker Moore
Owner

Just made sneaky changes to both methods and tests failed like they should! Thanks for DRY-ing up the code :)

Parker Moore parkr merged commit df9964c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2013
  1. Matt Rogers

    Remove duplication when generating list of entries to process

    mattr- authored
    Posts and Drafts share the same logic to get the list of files to
    process into objects when generating the site. Factor this logic into
    its own method and use it when reading posts and reading drafts.
  2. Matt Rogers
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 12 deletions.
  1. +27 −12 lib/jekyll/site.rb
39 lib/jekyll/site.rb
View
@@ -187,9 +187,7 @@ def read_directories(dir = '')
#
# Returns nothing.
def read_posts(dir)
- base = File.join(self.source, dir, '_posts')
- return unless File.exists?(base)
- entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
+ entries = get_entries(dir, '_posts')
# first pass processes, but does not yet render post content
entries.each do |f|
@@ -197,9 +195,7 @@ def read_posts(dir)
post = Post.new(self, self.source, dir, f)
if post.published && (self.future || post.date <= self.time)
- self.posts << post
- post.categories.each { |c| self.categories[c] << post }
- post.tags.each { |c| self.tags[c] << post }
+ aggregate_post_info(post)
end
end
end
@@ -212,18 +208,14 @@ def read_posts(dir)
#
# Returns nothing.
def read_drafts(dir)
- base = File.join(self.source, dir, '_drafts')
- return unless File.exists?(base)
- entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
+ entries = get_entries(dir, '_drafts')
# first pass processes, but does not yet render draft content
entries.each do |f|
if Draft.valid?(f)
draft = Draft.new(self, self.source, dir, f)
- self.posts << draft
- draft.categories.each { |c| self.categories[c] << draft }
- draft.tags.each { |c| self.tags[c] << draft }
+ aggregate_post_info(draft)
end
end
end
@@ -395,5 +387,28 @@ def getConverterImpl(klass)
raise "Converter implementation not found for #{klass}"
end
end
+
+ # Read the entries from a particular directory for processing
+ #
+ # dir - The String relative path of the directory to read
+ # subfolder - The String directory to read
+ #
+ # Returns the list of entries to process
+ def get_entries(dir, subfolder)
+ base = File.join(self.source, dir, subfolder)
+ return [] unless File.exists?(base)
+ entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
+ end
+
+ # Aggregate post information
+ #
+ # post - The Post object to aggregate information for
+ #
+ # Returns nothing
+ def aggregate_post_info(post)
+ self.posts << post
+ post.categories.each { |c| self.categories[c] << post }
+ post.tags.each { |c| self.tags[c] << post }
+ end
end
end
Something went wrong with that request. Please try again.