Class: Jekyll::StaticFile

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

Constant Summary collapse

@@mtimes =

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

{}

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.



14
15
16
17
18
19
20
21
22
# File 'lib/jekyll/static_file.rb', line 14

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

Instance Attribute Details

#extnameObject (readonly)

Returns the value of attribute extname.



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

def extname
  @extname
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



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

def relative_path
  @relative_path
end

Class Method Details

.reset_cacheObject

Reset the mtimes cache (for testing purposes).

Returns nothing.



92
93
94
95
# File 'lib/jekyll/static_file.rb', line 92

def self.reset_cache
  @@mtimes = {}
  nil
end

Instance Method Details

#defaultsObject

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



137
138
139
# File 'lib/jekyll/static_file.rb', line 137

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.



34
35
36
# File 'lib/jekyll/static_file.rb', line 34

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

#destination_rel_dirObject



38
39
40
41
42
43
44
# File 'lib/jekyll/static_file.rb', line 38

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)


58
59
60
# File 'lib/jekyll/static_file.rb', line 58

def modified?
  @@mtimes[path] != mtime
end

#modified_timeObject



46
47
48
# File 'lib/jekyll/static_file.rb', line 46

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

#mtimeObject

Returns last modification time for this file.



51
52
53
# File 'lib/jekyll/static_file.rb', line 51

def mtime
  modified_time.to_i
end

#pathObject

Returns source file path.



25
26
27
# File 'lib/jekyll/static_file.rb', line 25

def path
  File.join(*[@base, @dir, @name].compact)
end

#placeholdersObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/jekyll/static_file.rb', line 105

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

#to_liquidObject



97
98
99
100
101
102
103
# File 'lib/jekyll/static_file.rb', line 97

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

#typeObject

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



131
132
133
# File 'lib/jekyll/static_file.rb', line 131

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.



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

def url
  @url ||= if @collection.nil?
             relative_path
           else
             ::Jekyll::URL.new({
               :template => @collection.url_template,
               :placeholders => placeholders
             })
           end.to_s.gsub(/\/$/, '')
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).



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jekyll/static_file.rb', line 75

def write(dest)
  dest_path = destination(dest)

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

  FileUtils.mkdir_p(File.dirname(dest_path))
  FileUtils.rm(dest_path) if File.exist?(dest_path)
  FileUtils.cp(path, dest_path)
  File.utime(@@mtimes[path], @@mtimes[path], 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)


66
67
68
# File 'lib/jekyll/static_file.rb', line 66

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