Class: Jekyll::StaticFile

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

Constant Summary

@@mtimes =

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

Hash.new

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
# 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)
end

Instance Attribute Details

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



90
91
92
93
# File 'lib/jekyll/static_file.rb', line 90

def self.reset_cache
  @@mtimes = Hash.new
  nil
end

Instance Method Details

#destination(dest) ⇒ Object

Obtain destination path.

dest - The String path to the destination dir.

Returns destination file path.



37
38
39
# File 'lib/jekyll/static_file.rb', line 37

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

#destination_rel_dirObject



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

def destination_rel_dir
  if @collection
    @dir.gsub(/\A_/, '')
  else
    @dir
  end
end

#extnameObject



28
29
30
# File 'lib/jekyll/static_file.rb', line 28

def extname
  File.extname(path)
end

#modified?Boolean

Is source path modified?

Returns true if modified since last write.



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

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

#mtimeObject

Returns last modification time for this file.



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

def mtime
  File.stat(path).mtime.to_i
end

#pathObject

Returns source file path.



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

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

#to_liquidObject



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

def to_liquid
  {
    "path"          => File.join("", relative_path),
    "modified_time" => mtime.to_s,
    "extname"       => File.extname(relative_path)
  }
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).



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

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



64
65
66
# File 'lib/jekyll/static_file.rb', line 64

def write?
  true
end