class Jekyll::StaticFile

Attributes

extname[R]
relative_path[R]

Public Class Methods

mtimes() click to toggle source

The cache of last modification times [path] -> mtime.

# File lib/jekyll/static_file.rb, line 7
def mtimes
  @mtimes ||= {}
end
new(site, base, dir, name, collection = nil) click to toggle source

Initialize a new StaticFile.

site - The Site. base - The String path to the <source>. dir - The String path between <source> and the file. name - The String filename of the file. rubocop: disable ParameterLists

# File lib/jekyll/static_file.rb, line 23
def initialize(site, base, dir, name, collection = nil)
  @site = site
  @base = base
  @dir  = dir
  @name = name
  @collection = collection
  @relative_path = File.join(*[@dir, @name].compact)
  @extname = File.extname(@name)
end
reset_cache() click to toggle source
# File lib/jekyll/static_file.rb, line 11
def reset_cache
  @mtimes = nil
end

Public Instance Methods

defaults() click to toggle source

Returns the front matter defaults defined for the file's URL and/or type as defined in _config.yml.

# File lib/jekyll/static_file.rb, line 138
def defaults
  @defaults ||= @site.frontmatter_defaults.all url, type
end
destination(dest) click to toggle source

Obtain destination path.

dest - The String path to the destination dir.

Returns destination file path.

# File lib/jekyll/static_file.rb, line 44
def destination(dest)
  @site.in_dest_dir(*[dest, destination_rel_dir, @name].compact)
end
destination_rel_dir() click to toggle source
# File lib/jekyll/static_file.rb, line 48
def destination_rel_dir
  if @collection
    File.dirname(url)
  else
    @dir
  end
end
modified?() click to toggle source

Is source path modified?

Returns true if modified since last write.

# File lib/jekyll/static_file.rb, line 68
def modified?
  self.class.mtimes[path] != mtime
end
modified_time() click to toggle source
# File lib/jekyll/static_file.rb, line 56
def modified_time
  @modified_time ||= File.stat(path).mtime
end
mtime() click to toggle source

Returns last modification time for this file.

# File lib/jekyll/static_file.rb, line 61
def mtime
  modified_time.to_i
end
path() click to toggle source

Returns source file path.

# File lib/jekyll/static_file.rb, line 35
def path
  File.join(*[@base, @dir, @name].compact)
end
placeholders() click to toggle source
# File lib/jekyll/static_file.rb, line 106
def placeholders
  {
    :collection => @collection.label,
    :path       => relative_path[
      @collection.relative_directory.size..relative_path.size],
    :output_ext => "",
    :name       => "",
    :title      => ""
  }
end
to_liquid() click to toggle source
# File lib/jekyll/static_file.rb, line 98
def to_liquid
  {
    "extname"       => extname,
    "modified_time" => modified_time,
    "path"          => File.join("", relative_path)
  }
end
type() click to toggle source

Returns the type of the collection if present, nil otherwise.

# File lib/jekyll/static_file.rb, line 132
def type
  @type ||= @collection.nil? ? nil : @collection.label.to_sym
end
url() click to toggle source

Applies a similar URL-building technique as Jekyll::Document that takes the collection's URL template into account. The default URL template can be overriden in the collection's configuration in _config.yml.

# File lib/jekyll/static_file.rb, line 120
def url
  @url ||= if @collection.nil?
             relative_path
           else
             ::Jekyll::URL.new({
               :template     => @collection.url_template,
               :placeholders => placeholders
             })
           end.to_s.gsub(%r/$!, "")
end
write(dest) click to toggle source

Write the static file to the destination directory (if modified).

dest - The String path to the destination dir.

Returns false if the file was not modified since last time (no-op).

# File lib/jekyll/static_file.rb, line 85
def write(dest)
  dest_path = destination(dest)

  return false if File.exist?(dest_path) && !modified?
  self.class.mtimes[path] = mtime

  FileUtils.mkdir_p(File.dirname(dest_path))
  FileUtils.rm(dest_path) if File.exist?(dest_path)
  copy_file(dest_path)

  true
end
write?() click to toggle source

Whether to write the file to the filesystem

Returns true unless the defaults for the destination path from _config.yml contain `published: false`.

# File lib/jekyll/static_file.rb, line 76
def write?
  defaults.fetch("published", true)
end

Private Instance Methods

copy_file(dest_path) click to toggle source
# File lib/jekyll/static_file.rb, line 143
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