Merge pull request #5019 from ayastreb/static_file
Merge pull request 5019
This commit is contained in:
commit
5c03e1da3f
|
@ -10,7 +10,6 @@ AllCops:
|
||||||
- lib/jekyll/document.rb
|
- lib/jekyll/document.rb
|
||||||
- lib/jekyll/regenerator.rb
|
- lib/jekyll/regenerator.rb
|
||||||
- lib/jekyll/renderer.rb
|
- lib/jekyll/renderer.rb
|
||||||
- lib/jekyll/static_file.rb
|
|
||||||
- lib/jekyll/utils.rb
|
- lib/jekyll/utils.rb
|
||||||
- bin/**/*
|
- bin/**/*
|
||||||
- benchmark/**/*
|
- benchmark/**/*
|
||||||
|
|
|
@ -1,16 +1,25 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class StaticFile
|
class StaticFile
|
||||||
# The cache of last modification times [path] -> mtime.
|
|
||||||
@@mtimes = {}
|
|
||||||
|
|
||||||
attr_reader :relative_path, :extname
|
attr_reader :relative_path, :extname
|
||||||
|
|
||||||
|
class << self
|
||||||
|
# The cache of last modification times [path] -> mtime.
|
||||||
|
def mtimes
|
||||||
|
@mtimes ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset_cache
|
||||||
|
@mtimes = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Initialize a new StaticFile.
|
# Initialize a new StaticFile.
|
||||||
#
|
#
|
||||||
# site - The Site.
|
# site - The Site.
|
||||||
# base - The String path to the <source>.
|
# base - The String path to the <source>.
|
||||||
# dir - The String path between <source> and the file.
|
# dir - The String path between <source> and the file.
|
||||||
# name - The String filename of the file.
|
# name - The String filename of the file.
|
||||||
|
# rubocop: disable ParameterLists
|
||||||
def initialize(site, base, dir, name, collection = nil)
|
def initialize(site, base, dir, name, collection = nil)
|
||||||
@site = site
|
@site = site
|
||||||
@base = base
|
@base = base
|
||||||
|
@ -20,6 +29,7 @@ module Jekyll
|
||||||
@relative_path = File.join(*[@dir, @name].compact)
|
@relative_path = File.join(*[@dir, @name].compact)
|
||||||
@extname = File.extname(@name)
|
@extname = File.extname(@name)
|
||||||
end
|
end
|
||||||
|
# rubocop: enable ParameterLists
|
||||||
|
|
||||||
# Returns source file path.
|
# Returns source file path.
|
||||||
def path
|
def path
|
||||||
|
@ -56,7 +66,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns true if modified since last write.
|
# Returns true if modified since last write.
|
||||||
def modified?
|
def modified?
|
||||||
@@mtimes[path] != mtime
|
self.class.mtimes[path] != mtime
|
||||||
end
|
end
|
||||||
|
|
||||||
# Whether to write the file to the filesystem
|
# Whether to write the file to the filesystem
|
||||||
|
@ -64,7 +74,7 @@ module Jekyll
|
||||||
# Returns true unless the defaults for the destination path from
|
# Returns true unless the defaults for the destination path from
|
||||||
# _config.yml contain `published: false`.
|
# _config.yml contain `published: false`.
|
||||||
def write?
|
def write?
|
||||||
defaults.fetch('published', true)
|
defaults.fetch("published", true)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Write the static file to the destination directory (if modified).
|
# Write the static file to the destination directory (if modified).
|
||||||
|
@ -76,28 +86,15 @@ module Jekyll
|
||||||
dest_path = destination(dest)
|
dest_path = destination(dest)
|
||||||
|
|
||||||
return false if File.exist?(dest_path) && !modified?
|
return false if File.exist?(dest_path) && !modified?
|
||||||
@@mtimes[path] = mtime
|
self.class.mtimes[path] = mtime
|
||||||
|
|
||||||
FileUtils.mkdir_p(File.dirname(dest_path))
|
FileUtils.mkdir_p(File.dirname(dest_path))
|
||||||
FileUtils.rm(dest_path) if File.exist?(dest_path)
|
FileUtils.rm(dest_path) if File.exist?(dest_path)
|
||||||
if @site.safe || Jekyll.env == "production"
|
copy_file(dest_path)
|
||||||
FileUtils.cp(path, dest_path)
|
|
||||||
else
|
|
||||||
FileUtils.copy_entry(path, dest_path)
|
|
||||||
end
|
|
||||||
File.utime(@@mtimes[path], @@mtimes[path], dest_path)
|
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Reset the mtimes cache (for testing purposes).
|
|
||||||
#
|
|
||||||
# Returns nothing.
|
|
||||||
def self.reset_cache
|
|
||||||
@@mtimes = {}
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_liquid
|
def to_liquid
|
||||||
{
|
{
|
||||||
"extname" => extname,
|
"extname" => extname,
|
||||||
|
@ -109,11 +106,11 @@ module Jekyll
|
||||||
def placeholders
|
def placeholders
|
||||||
{
|
{
|
||||||
:collection => @collection.label,
|
:collection => @collection.label,
|
||||||
:path => relative_path[
|
:path => relative_path[
|
||||||
@collection.relative_directory.size..relative_path.size],
|
@collection.relative_directory.size..relative_path.size],
|
||||||
:output_ext => '',
|
:output_ext => "",
|
||||||
:name => '',
|
:name => "",
|
||||||
:title => ''
|
:title => ""
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -125,10 +122,10 @@ module Jekyll
|
||||||
relative_path
|
relative_path
|
||||||
else
|
else
|
||||||
::Jekyll::URL.new({
|
::Jekyll::URL.new({
|
||||||
:template => @collection.url_template,
|
:template => @collection.url_template,
|
||||||
:placeholders => placeholders
|
:placeholders => placeholders
|
||||||
})
|
})
|
||||||
end.to_s.gsub(/\/$/, '')
|
end.to_s.gsub(%r!/$!, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the type of the collection if present, nil otherwise.
|
# Returns the type of the collection if present, nil otherwise.
|
||||||
|
@ -141,5 +138,15 @@ module Jekyll
|
||||||
def defaults
|
def defaults
|
||||||
@defaults ||= @site.frontmatter_defaults.all url, type
|
@defaults ||= @site.frontmatter_defaults.all url, type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def copy_file(dest_path)
|
||||||
|
if @site.safe || Jekyll.env == "production"
|
||||||
|
FileUtils.cp(path, dest_path)
|
||||||
|
else
|
||||||
|
FileUtils.copy_entry(path, dest_path)
|
||||||
|
end
|
||||||
|
File.utime(self.class.mtimes[path], self.class.mtimes[path], dest_path)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue