Skip to content

Commit

Permalink
Ensure posts hashes are built properly before all HAML/HTML file gene…
Browse files Browse the repository at this point in the history
…ration.

This is needed to handle multiple blogs (i.e., multiple _posts
directories) properly so that the global set of "posts" across the entire
site are built properly before any code in HAML files or Liquid templates
attempt to access variables.  Unexpected and surprising results occur when
there are multiple _posts directories throughout the tree otherwise.

Also, sorting categories seemed wrong to me, since I don't want the
directory order to change from what I set up for the blog URL.
  • Loading branch information
bkuhn committed Aug 17, 2009
1 parent 9e923f8 commit 0ab97f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/jekyll/post.rb
Expand Up @@ -137,7 +137,7 @@ def generated_path
"month" => date.strftime("%m"),
"day" => date.strftime("%d"),
"title" => slug,
"categories" => categories.sort.join('/')
"categories" => categories.join('/')
}.inject(template) { |result, token|
result.gsub(/:#{token.first}/, token.last)
}.gsub("//", "/")
Expand Down
28 changes: 25 additions & 3 deletions lib/jekyll/site.rb
Expand Up @@ -123,6 +123,8 @@ def textile(content)
def process
self.reset
self.read_layouts
self.recurse_handle_posts
self.coordinate_posts
self.transform_pages
self.transform_sass if self.sass
self.write_posts
Expand Down Expand Up @@ -154,6 +156,7 @@ def read_posts(dir)

# first pass processes, but does not yet render post content
entries.each do |f|
print "Creating from dir: ", dir, " the post ", f, "\n"
if Post.valid?(f)
post = Post.new(self, self.source, dir, f)

Expand All @@ -164,7 +167,9 @@ def read_posts(dir)
end
end
end
end

def coordinate_posts()
self.posts.sort!

# second pass renders each post now that full site payload is available
Expand All @@ -188,6 +193,24 @@ def write_posts
end
end

def recurse_handle_posts(dir = '')
base = File.join(self.source, dir)
entries = filter_entries(Dir.entries(base))
directories = entries.select { |e| File.directory?(File.join(base, e)) }

# we need to make sure to process _posts *first* otherwise they
# might not be available yet to other templates as {{ site.posts }}
if directories.include?('_posts')
FileUtils.mkdir_p(File.join(self.dest, dir))
read_posts(dir)
end
directories.each do |newDir|
if File.directory?(File.join(base, newDir))
next if self.dest.sub(/\/$/, '') == File.join(base, newDir)
recurse_handle_posts(File.join(dir, newDir))
end
end
end
# Copy all regular files from <source> to <dest>/ ignoring
# any files/directories that are hidden or backup files (start
# with "." or "#" or end with "~") or contain site content (start with "_")
Expand All @@ -203,11 +226,10 @@ def transform_pages(dir = '')
directories = entries.select { |e| File.directory?(File.join(base, e)) }
files = entries.reject { |e| File.directory?(File.join(base, e)) }

# we need to make sure to process _posts *first* otherwise they
# might not be available yet to other templates as {{ site.posts }}
# we need to remove _posts here, they were previously processed by
# recurse_handle_posts
if directories.include?('_posts')
directories.delete('_posts')
read_posts(dir)
end
[directories, files].each do |entries|
entries.each do |f|
Expand Down

0 comments on commit 0ab97f7

Please sign in to comment.