class Jekyll::StaticFile
def basename
Generate "basename without extension" and strip away any trailing periods.
def basename @basename ||= File.basename(name, extname).gsub(%r!\.*\z!, "") end
def cleaned_relative_path
# => "/site/my-cool-avatar"
cleaned_relative_path
When `relative_path` is "_methods/site/my-cool-avatar...png":
Examples:
NOTE: `String#gsub!` removes all trailing periods (in comparison to `String#chomp!`)
and additionally removes any multiple periods in the string.
Generates a relative path with the collection's directory removed when applicable
Similar to Jekyll::Document#cleaned_relative_path.
def cleaned_relative_path @cleaned_relative_path ||= begin cleaned = relative_path[0..-extname.length - 1] cleaned.gsub!(%r!\.*\z!, "") cleaned.sub!(@collection.relative_directory, "") if @collection cleaned end end
def copy_file(dest_path)
def copy_file(dest_path) if @site.safe || Jekyll.env == "production" FileUtils.cp(path, dest_path) else FileUtils.copy_entry(path, dest_path) end unless File.symlink?(dest_path) File.utime(self.class.mtimes[path], self.class.mtimes[path], dest_path) end end
def data
def data @data ||= @site.frontmatter_defaults.all(relative_path, type) end
def defaults
Returns the front matter defaults defined for the file's URL and/or type
def defaults @defaults ||= @site.frontmatter_defaults.all url, type end
def destination(dest)
dest - The String path to the destination dir.
Obtain destination path.
def destination(dest) @destination ||= {} @destination[dest] ||= @site.in_dest_dir(dest, Jekyll::URL.unescape_path(url)) end
def destination_rel_dir
def destination_rel_dir if @collection File.dirname(url) else @dir end end
def initialize(site, base, dir, name, collection = nil)
name - The String filename of the file.
dir - The String path between
base - The String path to the
site - The Site.
Initialize a new StaticFile.
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) @type = @collection&.label&.to_sym end
def inspect
Returns a debug string on inspecting the static file.
def inspect "#<#{self.class} @relative_path=#{relative_path.inspect}>" end
def modified?
Is source path modified?
def modified? self.class.mtimes[path] != mtime end
def modified_time
def modified_time @modified_time ||= File.stat(path).mtime end
def mtime
def mtime modified_time.to_i end
def mtimes
def mtimes @mtimes ||= {} end
def path
def path @path ||= if !@collection.nil? && !@site.config["collections_dir"].empty? File.join(*[@base, @site.config["collections_dir"], @dir, @name].compact) else File.join(*[@base, @dir, @name].compact) end end
def placeholders
def placeholders { :collection => @collection.label, :path => cleaned_relative_path, :output_ext => "", :name => basename, :title => "", } end
def reset_cache
def reset_cache @mtimes = nil end
def to_liquid
def to_liquid @to_liquid ||= Drops::StaticFileDrop.new(self) end
def url
the collection's URL template into account. The default URL template can
Applies a similar URL-building technique as Jekyll::Document that takes
def url @url ||= begin base = if @collection.nil? cleaned_relative_path else Jekyll::URL.new( :template => @collection.url_template, :placeholders => placeholders ) end.to_s.chomp("/") base << extname end end
def write(dest)
dest - The String path to the destination dir.
Write the static file to the destination directory (if modified).
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
def write?
Returns true unless the defaults for the destination path from
Whether to write the file to the filesystem
def write? publishable = defaults.fetch("published", true) return publishable unless @collection publishable && @collection.write? end