Class: Jekyll::StaticFile
- Inherits:
-
Object
- Object
- Jekyll::StaticFile
- Defined in:
- lib/jekyll/static_file.rb
Constant Summary collapse
- @@mtimes =
The cache of last modification times [path] -> mtime.
Hash.new
Class Method Summary collapse
-
.reset_cache ⇒ Object
Reset the mtimes cache (for testing purposes).
Instance Method Summary collapse
-
#destination(dest) ⇒ Object
Obtain destination path.
- #destination_rel_dir ⇒ Object
- #extname ⇒ Object
-
#initialize(site, base, dir, name, collection = nil) ⇒ StaticFile
constructor
Initialize a new StaticFile.
-
#modified? ⇒ Boolean
Is source path modified?.
-
#mtime ⇒ Object
Returns last modification time for this file.
-
#path ⇒ Object
Returns source file path.
-
#relative_path ⇒ Object
Returns the source file path relative to the site source.
- #to_liquid ⇒ Object
-
#write(dest) ⇒ Object
Write the static file to the destination directory (if modified).
-
#write? ⇒ Boolean
Whether to write the file to the filesystem.
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.
12 13 14 15 16 17 18 |
# File 'lib/jekyll/static_file.rb', line 12 def initialize(site, base, dir, name, collection = nil) @site = site @base = base @dir = dir @name = name @collection = collection end |
Class Method Details
.reset_cache ⇒ Object
Reset the mtimes cache (for testing purposes).
Returns nothing.
91 92 93 94 |
# File 'lib/jekyll/static_file.rb', line 91 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.
39 40 41 |
# File 'lib/jekyll/static_file.rb', line 39 def destination(dest) @site.in_dest_dir(*[dest, destination_rel_dir, @name].compact) end |
#destination_rel_dir ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/jekyll/static_file.rb', line 43 def destination_rel_dir if @collection @dir.gsub(/\A_/, '') else @dir end end |
#extname ⇒ Object
30 31 32 |
# File 'lib/jekyll/static_file.rb', line 30 def extname File.extname(path) end |
#modified? ⇒ Boolean
Is source path modified?
Returns true if modified since last write.
59 60 61 |
# File 'lib/jekyll/static_file.rb', line 59 def modified? @@mtimes[path] != mtime end |
#mtime ⇒ Object
Returns last modification time for this file.
52 53 54 |
# File 'lib/jekyll/static_file.rb', line 52 def mtime File.stat(path).mtime.to_i end |
#path ⇒ Object
Returns source file path.
21 22 23 |
# File 'lib/jekyll/static_file.rb', line 21 def path File.join(*[@base, @dir, @name].compact) end |
#relative_path ⇒ Object
Returns the source file path relative to the site source
26 27 28 |
# File 'lib/jekyll/static_file.rb', line 26 def relative_path @relative_path ||= File.join(*[@dir, @name].compact) end |
#to_liquid ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/jekyll/static_file.rb', line 96 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).
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jekyll/static_file.rb', line 75 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) true end |
#write? ⇒ Boolean
Whether to write the file to the filesystem
Returns true.
66 67 68 |
# File 'lib/jekyll/static_file.rb', line 66 def write? true end |