Class: Jekyll::StaticFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jekyll/static_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, base, dir, name, collection = nil) ⇒ StaticFile

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



29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll/static_file.rb', line 29

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)
  @data = @site.frontmatter_defaults.all(relative_path, type)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/jekyll/static_file.rb', line 7

def data
  @data
end

#extnameObject (readonly)

Returns the value of attribute extname.



7
8
9
# File 'lib/jekyll/static_file.rb', line 7

def extname
  @extname
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/jekyll/static_file.rb', line 7

def name
  @name
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



7
8
9
# File 'lib/jekyll/static_file.rb', line 7

def relative_path
  @relative_path
end

Class Method Details

.mtimesObject

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



13
14
15
# File 'lib/jekyll/static_file.rb', line 13

def mtimes
  @mtimes ||= {}
end

.reset_cacheObject



17
18
19
# File 'lib/jekyll/static_file.rb', line 17

def reset_cache
  @mtimes = nil
end

Instance Method Details

#basenameObject



114
115
116
# File 'lib/jekyll/static_file.rb', line 114

def basename
  File.basename(name, extname)
end

#defaultsObject

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



150
151
152
# File 'lib/jekyll/static_file.rb', line 150

def defaults
  @defaults ||= @site.frontmatter_defaults.all url, type
end

#destination(dest) ⇒ Object

Obtain destination path.

dest - The String path to the destination dir.

Returns destination file path.



56
57
58
# File 'lib/jekyll/static_file.rb', line 56

def destination(dest)
  @site.in_dest_dir(*[dest, destination_rel_dir, @name].compact)
end

#destination_rel_dirObject



60
61
62
63
64
65
66
# File 'lib/jekyll/static_file.rb', line 60

def destination_rel_dir
  if @collection
    File.dirname(url)
  else
    @dir
  end
end

#modified?Boolean

Is source path modified?

Returns true if modified since last write.

Returns:

  • (Boolean)


80
81
82
# File 'lib/jekyll/static_file.rb', line 80

def modified?
  self.class.mtimes[path] != mtime
end

#modified_timeObject



68
69
70
# File 'lib/jekyll/static_file.rb', line 68

def modified_time
  @modified_time ||= File.stat(path).mtime
end

#mtimeObject

Returns last modification time for this file.



73
74
75
# File 'lib/jekyll/static_file.rb', line 73

def mtime
  modified_time.to_i
end

#pathObject

Returns source file path.



42
43
44
45
46
47
48
49
# File 'lib/jekyll/static_file.rb', line 42

def path
  # Static file is from a collection inside custom collections directory
  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

#placeholdersObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll/static_file.rb', line 118

def placeholders
  {
    :collection => @collection.label,
    :path       => relative_path[
      @collection.relative_directory.size..relative_path.size],
    :output_ext => "",
    :name       => "",
    :title      => "",
  }
end

#to_liquidObject



110
111
112
# File 'lib/jekyll/static_file.rb', line 110

def to_liquid
  @to_liquid ||= Drops::StaticFileDrop.new(self)
end

#typeObject

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



144
145
146
# File 'lib/jekyll/static_file.rb', line 144

def type
  @type ||= @collection.nil? ? nil : @collection.label.to_sym
end

#urlObject

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.



132
133
134
135
136
137
138
139
140
141
# File 'lib/jekyll/static_file.rb', line 132

def url
  @url ||= if @collection.nil?
             relative_path
           else
             ::Jekyll::URL.new({
               :template     => @collection.url_template,
               :placeholders => placeholders,
             })
           end.to_s.chomp("/")
end

#write(dest) ⇒ Object

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).



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jekyll/static_file.rb', line 97

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?Boolean

Whether to write the file to the filesystem

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/jekyll/static_file.rb', line 88

def write?
  defaults.fetch("published", true)
end