split process handling into phases to allow pages to have access to full and complete site payload and added some test improvements
This commit is contained in:
parent
b5916caf4b
commit
0cb1ebcda1
|
@ -26,6 +26,7 @@ require 'jekyll/filters'
|
||||||
require 'jekyll/tags/highlight'
|
require 'jekyll/tags/highlight'
|
||||||
require 'jekyll/tags/include'
|
require 'jekyll/tags/include'
|
||||||
require 'jekyll/albino'
|
require 'jekyll/albino'
|
||||||
|
require 'jekyll/static_file'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
# Default options. Overriden by values in _config.yml or command-line opts.
|
# Default options. Overriden by values in _config.yml or command-line opts.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
|
||||||
class Site
|
class Site
|
||||||
attr_accessor :config, :layouts, :posts, :categories, :exclude,
|
attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude,
|
||||||
:source, :dest, :lsi, :pygments, :permalink_style, :tags
|
:source, :dest, :lsi, :pygments, :permalink_style, :tags
|
||||||
|
|
||||||
# Initialize the site
|
# Initialize the site
|
||||||
|
@ -25,6 +25,8 @@ module Jekyll
|
||||||
def reset
|
def reset
|
||||||
self.layouts = {}
|
self.layouts = {}
|
||||||
self.posts = []
|
self.posts = []
|
||||||
|
self.pages = []
|
||||||
|
self.static_files = []
|
||||||
self.categories = Hash.new { |hash, key| hash[key] = [] }
|
self.categories = Hash.new { |hash, key| hash[key] = [] }
|
||||||
self.tags = Hash.new { |hash, key| hash[key] = [] }
|
self.tags = Hash.new { |hash, key| hash[key] = [] }
|
||||||
end
|
end
|
||||||
|
@ -87,21 +89,29 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
# Do the actual work of processing the site and generating the
|
# Do the actual work of processing the site and generating the
|
||||||
# real deal.
|
# real deal. Now has 4 phases; reset, read, render, write. This allows
|
||||||
|
# rendering to have full site payload available.
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def process
|
def process
|
||||||
self.reset
|
self.reset
|
||||||
self.read_layouts
|
self.read
|
||||||
self.transform_pages
|
self.render
|
||||||
self.write_posts
|
self.write
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read all the files in <source>/_layouts into memory for later use.
|
def read
|
||||||
|
self.read_layouts # existing implementation did this at top level only so preserved that
|
||||||
|
self.read_directories
|
||||||
|
end
|
||||||
|
|
||||||
|
# Read all the files in <source>/<dir>/_layouts and create a new Layout
|
||||||
|
# object with each one.
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def read_layouts
|
def read_layouts(dir = '')
|
||||||
base = File.join(self.source, "_layouts")
|
base = File.join(self.source, dir, "_layouts")
|
||||||
|
return unless File.exists?(base)
|
||||||
entries = []
|
entries = []
|
||||||
Dir.chdir(base) { entries = filter_entries(Dir['*.*']) }
|
Dir.chdir(base) { entries = filter_entries(Dir['*.*']) }
|
||||||
|
|
||||||
|
@ -109,17 +119,16 @@ module Jekyll
|
||||||
name = f.split(".")[0..-2].join(".")
|
name = f.split(".")[0..-2].join(".")
|
||||||
self.layouts[name] = Layout.new(self, base, f)
|
self.layouts[name] = Layout.new(self, base, f)
|
||||||
end
|
end
|
||||||
rescue Errno::ENOENT => e
|
|
||||||
# ignore missing layout dir
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read all the files in <base>/_posts and create a new Post object with each one.
|
# Read all the files in <source>/<dir>/_posts and create a new Post
|
||||||
|
# object with each one.
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def read_posts(dir)
|
def read_posts(dir)
|
||||||
base = File.join(self.source, dir, '_posts')
|
base = File.join(self.source, dir, '_posts')
|
||||||
entries = []
|
return unless File.exists?(base)
|
||||||
Dir.chdir(base) { entries = filter_entries(Dir['**/*']) }
|
entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
|
||||||
|
|
||||||
# first pass processes, but does not yet render post content
|
# first pass processes, but does not yet render post content
|
||||||
entries.each do |f|
|
entries.each do |f|
|
||||||
|
@ -135,10 +144,11 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
self.posts.sort!
|
self.posts.sort!
|
||||||
|
end
|
||||||
|
|
||||||
# second pass renders each post now that full site payload is available
|
def render
|
||||||
self.posts.each do |post|
|
[self.posts, self.pages].flatten.each do |convertible|
|
||||||
post.render(self.layouts, site_payload)
|
convertible.render(self.layouts, site_payload)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a} }
|
self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a} }
|
||||||
|
@ -147,55 +157,50 @@ module Jekyll
|
||||||
# ignore missing layout dir
|
# ignore missing layout dir
|
||||||
end
|
end
|
||||||
|
|
||||||
# Write each post to <dest>/<year>/<month>/<day>/<slug>
|
# Write static files, pages and posts
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def write_posts
|
def write
|
||||||
self.posts.each do |post|
|
self.posts.each do |post|
|
||||||
post.write(self.dest)
|
post.write(self.dest)
|
||||||
end
|
end
|
||||||
|
self.pages.each do |page|
|
||||||
|
page.write(self.dest)
|
||||||
|
end
|
||||||
|
self.static_files.each do |sf|
|
||||||
|
sf.write(self.dest)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Copy all regular files from <source> to <dest>/ ignoring
|
# Reads the directories and finds posts, pages and static files that will
|
||||||
# any files/directories that are hidden or backup files (start
|
# become part of the valid site according to the rules in +filter_entries+.
|
||||||
# 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
|
# The +dir+ String is a relative path used to call this method
|
||||||
# recursively as it descends through directories
|
# recursively as it descends through directories
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def transform_pages(dir = '')
|
def read_directories(dir = '')
|
||||||
base = File.join(self.source, dir)
|
base = File.join(self.source, dir)
|
||||||
entries = filter_entries(Dir.entries(base))
|
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)) || File.symlink?(File.join(base, e)) }
|
|
||||||
|
|
||||||
# we need to make sure to process _posts *first* otherwise they
|
self.read_posts(dir)
|
||||||
# might not be available yet to other templates as {{ site.posts }}
|
|
||||||
if directories.include?('_posts')
|
|
||||||
directories.delete('_posts')
|
|
||||||
read_posts(dir)
|
|
||||||
end
|
|
||||||
|
|
||||||
[directories, files].each do |entries|
|
entries.each do |f|
|
||||||
entries.each do |f|
|
f_abs = File.join(base, f)
|
||||||
if File.directory?(File.join(base, f))
|
f_rel = File.join(dir, f)
|
||||||
next if self.dest.sub(/\/$/, '') == File.join(base, f)
|
if File.directory?(f_abs)
|
||||||
transform_pages(File.join(dir, f))
|
next if self.dest.sub(/\/$/, '') == f_abs
|
||||||
elsif Pager.pagination_enabled?(self.config, f)
|
read_directories(f_rel)
|
||||||
|
elsif !File.symlink?(f_abs)
|
||||||
|
if Pager.pagination_enabled?(self.config, f)
|
||||||
paginate_posts(f, dir)
|
paginate_posts(f, dir)
|
||||||
else
|
else
|
||||||
first3 = File.open(File.join(self.source, dir, f)) { |fd| fd.read(3) }
|
first3 = File.open(f_abs) { |fd| fd.read(3) }
|
||||||
if first3 == "---"
|
if first3 == "---"
|
||||||
# file appears to have a YAML header so process it as a page
|
# file appears to have a YAML header so process it as a page
|
||||||
page = Page.new(self, self.source, dir, f)
|
pages << Page.new(self, self.source, dir, f)
|
||||||
page.render(self.layouts, site_payload)
|
|
||||||
page.write(self.dest)
|
|
||||||
else
|
else
|
||||||
# otherwise copy the file without transforming it
|
# otherwise treat it as a static file
|
||||||
FileUtils.mkdir_p(File.join(self.dest, dir))
|
static_files << StaticFile.new(self, self.source, dir, f)
|
||||||
FileUtils.cp(File.join(self.source, dir, f), File.join(self.dest, dir, f))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -228,19 +233,19 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
# Filter out any files/directories that are hidden or backup files (start
|
# Filter out any files/directories that are hidden or backup files (start
|
||||||
# with "." or "#" 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
|
# or are excluded in the site configuration, unless they are web server
|
||||||
# '.htaccess'
|
# files such as '.htaccess'
|
||||||
def filter_entries(entries)
|
def filter_entries(entries)
|
||||||
entries = entries.reject do |e|
|
entries = entries.reject do |e|
|
||||||
unless ['_posts', '.htaccess'].include?(e)
|
unless ['.htaccess'].include?(e)
|
||||||
['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || self.exclude.include?(e)
|
['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || self.exclude.include?(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
|
# Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
|
||||||
# and adds more wite-wide data
|
# and adds more site-wide data
|
||||||
#
|
#
|
||||||
# {"paginator" => { "page" => <Number>,
|
# {"paginator" => { "page" => <Number>,
|
||||||
# "per_page" => <Number>,
|
# "per_page" => <Number>,
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
module Jekyll
|
||||||
|
|
||||||
|
class StaticFile
|
||||||
|
# Initialize a new StaticFile.
|
||||||
|
# +site+ is the Site
|
||||||
|
# +base+ is the String path to the <source>
|
||||||
|
# +dir+ is the String path between <source> and the file
|
||||||
|
# +name+ is the String filename of the file
|
||||||
|
#
|
||||||
|
# Returns <StaticFile>
|
||||||
|
def initialize(site, base, dir, name)
|
||||||
|
@site = site
|
||||||
|
@base = base
|
||||||
|
@dir = dir
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Write the static file to the destination directory.
|
||||||
|
# +dest+ is the String path to the destination dir
|
||||||
|
#
|
||||||
|
# Returns nothing
|
||||||
|
def write(dest)
|
||||||
|
FileUtils.mkdir_p(File.join(dest, @dir))
|
||||||
|
FileUtils.cp(File.join(@base, @dir, @name), File.join(dest, @dir, @name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -19,11 +19,17 @@ class TestSite < Test::Unit::TestCase
|
||||||
before_posts = @site.posts.length
|
before_posts = @site.posts.length
|
||||||
before_layouts = @site.layouts.length
|
before_layouts = @site.layouts.length
|
||||||
before_categories = @site.categories.length
|
before_categories = @site.categories.length
|
||||||
|
before_tags = @site.tags.length
|
||||||
|
before_pages = @site.pages.length
|
||||||
|
before_static_files = @site.static_files.length
|
||||||
|
|
||||||
@site.process
|
@site.process
|
||||||
assert_equal before_posts, @site.posts.length
|
assert_equal before_posts, @site.posts.length
|
||||||
assert_equal before_layouts, @site.layouts.length
|
assert_equal before_layouts, @site.layouts.length
|
||||||
assert_equal before_categories, @site.categories.length
|
assert_equal before_categories, @site.categories.length
|
||||||
|
assert_equal before_tags, @site.tags.length
|
||||||
|
assert_equal before_pages, @site.pages.length
|
||||||
|
assert_equal before_static_files, @site.static_files.length
|
||||||
end
|
end
|
||||||
|
|
||||||
should "read layouts" do
|
should "read layouts" do
|
||||||
|
@ -52,10 +58,10 @@ class TestSite < Test::Unit::TestCase
|
||||||
should "filter entries" do
|
should "filter entries" do
|
||||||
ent1 = %w[foo.markdown bar.markdown baz.markdown #baz.markdown#
|
ent1 = %w[foo.markdown bar.markdown baz.markdown #baz.markdown#
|
||||||
.baz.markdow foo.markdown~]
|
.baz.markdow foo.markdown~]
|
||||||
ent2 = %w[.htaccess _posts bla.bla]
|
ent2 = %w[.htaccess _posts _pages bla.bla]
|
||||||
|
|
||||||
assert_equal %w[foo.markdown bar.markdown baz.markdown], @site.filter_entries(ent1)
|
assert_equal %w[foo.markdown bar.markdown baz.markdown], @site.filter_entries(ent1)
|
||||||
assert_equal ent2, @site.filter_entries(ent2)
|
assert_equal %w[.htaccess bla.bla], @site.filter_entries(ent2)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "filter entries with exclude" do
|
should "filter entries with exclude" do
|
||||||
|
|
Loading…
Reference in New Issue