Class: Jekyll::StaticFile
- Inherits:
-
Object
- Object
- Jekyll::StaticFile
- Extended by:
- Forwardable
- Defined in:
- lib/jekyll/static_file.rb
Instance Attribute Summary collapse
-
#extname ⇒ Object
readonly
Returns the value of attribute extname.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#relative_path ⇒ Object
readonly
Returns the value of attribute relative_path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
- .reset_cache ⇒ Object
Instance Method Summary collapse
-
#basename ⇒ Object
Generate “basename without extension” and strip away any trailing periods.
-
#cleaned_relative_path ⇒ Object
Similar to Jekyll::Document#cleaned_relative_path.
- #data ⇒ Object
-
#defaults ⇒ Object
Returns the front matter defaults defined for the file’s URL and/or type as defined in _config.yml.
-
#destination(dest) ⇒ Object
Obtain destination path.
- #destination_rel_dir ⇒ Object
-
#initialize(site, base, dir, name, collection = nil) ⇒ StaticFile
constructor
Initialize a new StaticFile.
-
#inspect ⇒ Object
Returns a debug string on inspecting the static file.
-
#modified? ⇒ Boolean
Is source path modified?.
- #modified_time ⇒ Object
-
#mtime ⇒ Object
Returns last modification time for this file.
-
#path ⇒ Object
Returns source file path.
- #placeholders ⇒ Object
- #to_liquid ⇒ Object
-
#url ⇒ Object
Applies a similar URL-building technique as Jekyll::Document that takes the collection’s URL template into account.
-
#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. rubocop: disable Metrics/ParameterLists
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jekyll/static_file.rb', line 31 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 |
Instance Attribute Details
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
7 8 9 |
# File 'lib/jekyll/static_file.rb', line 7 def extname @extname end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/jekyll/static_file.rb', line 7 def name @name end |
#relative_path ⇒ Object (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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/jekyll/static_file.rb', line 7 def type @type end |
Class Method Details
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
15 16 17 |
# File 'lib/jekyll/static_file.rb', line 15 def mtimes @mtimes ||= {} end |
.reset_cache ⇒ Object
19 20 21 |
# File 'lib/jekyll/static_file.rb', line 19 def reset_cache @mtimes = nil end |
Instance Method Details
#basename ⇒ Object
Generate “basename without extension” and strip away any trailing periods. NOTE: ‘String#gsub` removes all trailing periods (in comparison to `String#chomp`)
125 126 127 |
# File 'lib/jekyll/static_file.rb', line 125 def basename @basename ||= File.basename(name, extname).gsub(%r!\.*\z!, "") end |
#cleaned_relative_path ⇒ Object
Similar to Jekyll::Document#cleaned_relative_path. Generates a relative path with the collection’s directory removed when applicable
and additionally removes any multiple periods in the string.
NOTE: ‘String#gsub!` removes all trailing periods (in comparison to `String#chomp!`)
Examples:
When `relative_path` is "_methods/site/my-cool-avatar...png":
cleaned_relative_path
# => "/site/my-cool-avatar"
Returns the cleaned relative path of the static file.
151 152 153 154 155 156 157 158 |
# File 'lib/jekyll/static_file.rb', line 151 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 |
#data ⇒ Object
115 116 117 |
# File 'lib/jekyll/static_file.rb', line 115 def data @data ||= @site.frontmatter_defaults.all(relative_path, type) end |
#defaults ⇒ Object
Returns the front matter defaults defined for the file’s URL and/or type as defined in _config.yml.
179 180 181 |
# File 'lib/jekyll/static_file.rb', line 179 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.
57 58 59 60 |
# File 'lib/jekyll/static_file.rb', line 57 def destination(dest) @destination ||= {} @destination[dest] ||= @site.in_dest_dir(dest, Jekyll::URL.unescape_path(url)) end |
#destination_rel_dir ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/jekyll/static_file.rb', line 62 def destination_rel_dir if @collection File.dirname(url) else @dir end end |
#inspect ⇒ Object
Returns a debug string on inspecting the static file. Includes only the relative path of the object.
185 186 187 |
# File 'lib/jekyll/static_file.rb', line 185 def inspect "#<#{self.class} @relative_path=#{relative_path.inspect}>" end |
#modified? ⇒ Boolean
Is source path modified?
Returns true if modified since last write.
82 83 84 |
# File 'lib/jekyll/static_file.rb', line 82 def modified? self.class.mtimes[path] != mtime end |
#modified_time ⇒ Object
70 71 72 |
# File 'lib/jekyll/static_file.rb', line 70 def modified_time @modified_time ||= File.stat(path).mtime end |
#mtime ⇒ Object
Returns last modification time for this file.
75 76 77 |
# File 'lib/jekyll/static_file.rb', line 75 def mtime modified_time.to_i end |
#path ⇒ Object
Returns source file path.
44 45 46 47 48 49 50 |
# File 'lib/jekyll/static_file.rb', line 44 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 |
#placeholders ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/jekyll/static_file.rb', line 129 def placeholders { :collection => @collection.label, :path => cleaned_relative_path, :output_ext => "", :name => basename, :title => "", } end |
#to_liquid ⇒ Object
119 120 121 |
# File 'lib/jekyll/static_file.rb', line 119 def to_liquid @to_liquid ||= Drops::StaticFileDrop.new(self) end |
#url ⇒ Object
Applies a similar URL-building technique as Jekyll::Document that takes the collection’s URL template into account. The default URL template can be overridden in the collection’s configuration in _config.yml.
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/jekyll/static_file.rb', line 163 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 |
#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).
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/jekyll/static_file.rb', line 102 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`.
90 91 92 93 94 95 |
# File 'lib/jekyll/static_file.rb', line 90 def write? publishable = defaults.fetch("published", true) return publishable unless @collection publishable && @collection.write? end |