Sha256: 829698b0ce0b77cde4f3e99757e2affd5193d119496cb5e3aeb005135434aa50

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

module Jekyll
  class StaticFile
    # The cache of last modification times [path] -> mtime.
    @@mtimes = Hash.new

    # 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.
    def initialize(site, base, dir, name)
      @site = site
      @base = base
      @dir  = dir
      @name = name
    end

    # Returns source file path.
    def path
      File.join(@base, @dir, @name)
    end

    # Returns the source file path relative to the site source
    def relative_path
      @relative_path ||= path.sub(/\A#{@site.source}/, '')
    end

    # Obtain destination path.
    #
    # dest - The String path to the destination dir.
    #
    # Returns destination file path.
    def destination(dest)
      File.join(dest, @dir, @name)
    end

    # Returns last modification time for this file.
    def mtime
      File.stat(path).mtime.to_i
    end

    # Is source path modified?
    #
    # Returns true if modified since last write.
    def modified?
      @@mtimes[path] != mtime
    end

    # 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).
    def write(dest)
      dest_path = destination(dest)

      return false if File.exist?(dest_path) and !modified?
      @@mtimes[path] = mtime

      FileUtils.mkdir_p(File.dirname(dest_path))
      FileUtils.cp(path, dest_path)

      true
    end

    # Reset the mtimes cache (for testing purposes).
    #
    # Returns nothing.
    def self.reset_cache
      @@mtimes = Hash.new
      nil
    end

    def to_liquid
      {
        "path"          => relative_path,
        "modified_time" => mtime.to_s,
        "extname"       => File.extname(relative_path)
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jekyll-2.0.2 lib/jekyll/static_file.rb
jekyll-2.0.1 lib/jekyll/static_file.rb
jekyll-2.0.0 lib/jekyll/static_file.rb
jekyll-2.0.0.rc1 lib/jekyll/static_file.rb
jekyll-2.0.0.alpha.3 lib/jekyll/static_file.rb
jekyll-2.0.0.alpha.2 lib/jekyll/static_file.rb