Use site.in_source_dir

This commit is contained in:
Alfred Xing 2014-11-23 15:45:49 -08:00
parent 2a5cf11ee2
commit dc30114605
10 changed files with 55 additions and 43 deletions

View File

@ -21,6 +21,7 @@ require 'time'
require 'English' require 'English'
require 'pathname' require 'pathname'
require 'logger' require 'logger'
require 'set'
# 3rd party # 3rd party
require 'safe_yaml/load' require 'safe_yaml/load'

View File

@ -168,6 +168,14 @@ module Jekyll
true true
end end
# Determine whether to regenerate the file based on metadata.
#
# Returns true if file needs to be regenerated
def regenerate?
site.metadata.regenerate?(site.in_source_dir(relative_path)) ||
data['regenerate']
end
# Determine whether the file should be placed into layouts. # Determine whether the file should be placed into layouts.
# #
# Returns false if the document is an asset file. # Returns false if the document is an asset file.
@ -209,8 +217,8 @@ module Jekyll
# Add layout to dependency tree # Add layout to dependency tree
site.metadata.add_dependency( site.metadata.add_dependency(
Jekyll.sanitized_path(site.source, path), site.in_source_dir(path),
Jekyll.sanitized_path(site.source, layout.path) site.in_source_dir(layout.path)
) )
if layout = layouts[layout.data["layout"]] if layout = layouts[layout.data["layout"]]

View File

@ -105,6 +105,14 @@ module Jekyll
!(coffeescript_file? || yaml_file?) !(coffeescript_file? || yaml_file?)
end end
# Determine whether the document should be regenerated based on metadata.
#
# Returns true if the document needs to be regenerated.
def regenerate?
site.metadata.regenerate?(path, write?) ||
data['regenerate']
end
# Determine whether the file should be placed into layouts. # Determine whether the file should be placed into layouts.
# #
# Returns false if the document is either an asset file or a yaml file, # Returns false if the document is either an asset file or a yaml file,

View File

@ -29,7 +29,7 @@ module Jekyll
@site = site @site = site
@base = base @base = base
@name = name @name = name
@path = Jekyll.sanitized_path(site.source, File.join(base, name)) @path = site.in_source_dir(base, name)
self.data = {} self.data = {}

View File

@ -1,12 +1,14 @@
require 'set'
module Jekyll module Jekyll
class Metadata class Metadata
attr_reader :site, :metadata attr_reader :site, :metadata, :cache
def initialize(site) def initialize(site)
@site = site @site = site
# Configuration options
@full_rebuild = site.config['full_rebuild']
@disabled = site.config['no_metadata']
# Read metadata from file # Read metadata from file
read_metadata read_metadata
@ -20,48 +22,48 @@ module Jekyll
def add(path) def add(path)
return true if not File.exist? path return true if not File.exist? path
@metadata[path] = { metadata[path] = {
"mtime" => File.mtime(path), "mtime" => File.mtime(path),
"deps" => [] "deps" => []
} }
@cache[path] = true cache[path] = true
end end
# Force a path to regenerate # Force a path to regenerate
# #
# Returns true. # Returns true.
def force(path) def force(path)
@cache[path] = true cache[path] = true
end end
# Clear the metadata and cache # Clear the metadata and cache
# #
# Returns nothing # Returns nothing
def clear def clear
@metadata = {} metadata = {}
@cache = {} cache = {}
end end
# Checks if a path should be regenerated # Checks if a path should be regenerated
# #
# Returns a boolean. # Returns a boolean.
def regenerate?(path, add = true) def regenerate?(path, add = true)
return true if site.config['no_metadata'] return true if @disabled
# Check for path in cache # Check for path in cache
if @cache.has_key? path if cache.has_key? path
return @cache[path] return cache[path]
end end
# Check path that exists in metadata # Check path that exists in metadata
if (data = @metadata[path]) if data = metadata[path]
data["deps"].each do |dependency| data["deps"].each do |dependency|
if regenerate?(dependency) if regenerate?(dependency)
return @cache[dependency] = @cache[path] = true return cache[dependency] = cache[path] = true
end end
end end
if data["mtime"] == File.mtime(path) if data["mtime"].eql? File.mtime(path)
return @cache[path] = false return cache[path] = false
else else
return !add || add(path) return !add || add(path)
end end
@ -75,9 +77,9 @@ module Jekyll
# #
# Returns nothing. # Returns nothing.
def add_dependency(path, dependency) def add_dependency(path, dependency)
return if (@metadata[path].nil? || site.config['no_metadata']) return if (metadata[path].nil? || @disabled)
@metadata[path]["deps"] << dependency unless @metadata[path]["deps"].include? dependency metadata[path]["deps"] << dependency unless metadata[path]["deps"].include? dependency
regenerate? dependency regenerate? dependency
end end
@ -86,7 +88,7 @@ module Jekyll
# Returns nothing. # Returns nothing.
def write def write
File.open(metadata_file, 'w') do |f| File.open(metadata_file, 'w') do |f|
f.write(@metadata.to_yaml) f.write(metadata.to_yaml)
end end
end end
@ -94,7 +96,7 @@ module Jekyll
# #
# Returns the String path of the file. # Returns the String path of the file.
def metadata_file def metadata_file
Jekyll.sanitized_path(site.source, '.jekyll-metadata') site.in_source_dir('.jekyll-metadata')
end end
private private
@ -104,7 +106,7 @@ module Jekyll
# #
# Returns the read metadata. # Returns the read metadata.
def read_metadata def read_metadata
@metadata = if !(site.config['full_rebuild'] || site.config['no_metadata']) && File.file?(metadata_file) @metadata = if !(@full_rebuild || @disabled) && File.file?(metadata_file)
SafeYAML.load(File.read(metadata_file)) SafeYAML.load(File.read(metadata_file))
else else
{} {}

View File

@ -140,8 +140,8 @@ module Jekyll
# Add layout to dependency tree # Add layout to dependency tree
site.metadata.add_dependency( site.metadata.add_dependency(
Jekyll.sanitized_path(site.source, document.path), site.in_source_dir(document.path),
Jekyll.sanitized_path(site.source, layout.path) site.in_source_dir(layout.path)
) if document.write? ) if document.write?
if layout = site.layouts[layout.data["layout"]] if layout = site.layouts[layout.data["layout"]]

View File

@ -11,7 +11,7 @@ module Jekyll
:gems, :plugin_manager :gems, :plugin_manager
attr_accessor :converters, :generators attr_accessor :converters, :generators
attr_accessor :metadata attr_reader :metadata
# Public: Initialize a new Site. # Public: Initialize a new Site.
# #
@ -293,19 +293,13 @@ module Jekyll
collections.each do |label, collection| collections.each do |label, collection|
collection.docs.each do |document| collection.docs.each do |document|
document.output = Jekyll::Renderer.new(self, document).run if ( document.output = Jekyll::Renderer.new(self, document).run if document.regenerate?
metadata.regenerate?(document.path, document.write?) ||
document.data['regenerate']
)
end end
end end
payload = site_payload payload = site_payload
[posts, pages].flatten.each do |page_or_post| [posts, pages].flatten.each do |page_or_post|
page_or_post.render(layouts, payload) if ( page_or_post.render(layouts, payload) if page_or_post.regenerate?
metadata.regenerate?(Jekyll.sanitized_path(source, page_or_post.relative_path)) ||
page_or_post.data['regenerate']
)
end end
rescue Errno::ENOENT => e rescue Errno::ENOENT => e
# ignore missing layout dir # ignore missing layout dir
@ -323,12 +317,9 @@ module Jekyll
# Returns nothing. # Returns nothing.
def write def write
each_site_file { |item| each_site_file { |item|
item.write(dest) if ( item.write(dest) if item.regenerate?
metadata.regenerate?(Jekyll.sanitized_path(source, item.path)) ||
(item.respond_to?(:data) && item.data['regenerate'])
)
} }
metadata.write unless site.config['no_metadata'] metadata.write unless config['no_metadata']
end end
# Construct a Hash of Posts indexed by the specified Post attribute. # Construct a Hash of Posts indexed by the specified Post attribute.

View File

@ -67,6 +67,8 @@ module Jekyll
true true
end end
alias_method :regenerate?, :write?
# Write the static file to the destination directory (if modified). # Write the static file to the destination directory (if modified).
# #
# dest - The String path to the destination dir. # dest - The String path to the destination dir.

View File

@ -117,7 +117,7 @@ eos
# Add include to dependency tree # Add include to dependency tree
if context.registers[:page] and context.registers[:page].has_key? "path" if context.registers[:page] and context.registers[:page].has_key? "path"
site.metadata.add_dependency( site.metadata.add_dependency(
Jekyll.sanitized_path(site.source, context.registers[:page]["path"]), site.in_source_dir(context.registers[:page]["path"]),
path path
) )
end end

View File

@ -245,9 +245,9 @@ class TestDocument < Test::Unit::TestCase
"output" => true "output" => true
} }
}, },
"source" => source_dir, "source" => source_dir,
"destination" => dest_dir, "destination" => dest_dir,
"full_rebuild" => true "full_rebuild" => true
})) }))
@site.process @site.process
@document = @site.collections["slides"].files.find { |doc| doc.relative_path == "_slides/octojekyll.png" } @document = @site.collections["slides"].files.find { |doc| doc.relative_path == "_slides/octojekyll.png" }