Class: Bridgetown::StaticFile
- Inherits:
-
Object
- Object
- Bridgetown::StaticFile
- Extended by:
- Forwardable
- Defined in:
- lib/bridgetown-core/static_file.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#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.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Class Method Summary collapse
-
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
- .reset_cache ⇒ Object
Instance Method Summary collapse
- #basename ⇒ Object (also: #basename_without_ext)
-
#cleaned_relative_path ⇒ Object
Similar to Bridgetown::Document#cleaned_relative_path.
-
#defaults ⇒ Object
Returns the front matter defaults defined for the file's URL and/or type as defined in bridgetown.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 (also: #date)
-
#mtime ⇒ Object
Returns last modification time for this file.
-
#path ⇒ Object
Returns source file path.
- #placeholders ⇒ Object
- #relative_path_basename_without_prefix ⇒ Object
- #to_liquid ⇒ Object
-
#type ⇒ Object
Returns the type of the collection if present, nil otherwise.
-
#url ⇒ Object
Applies a similar URL-building technique as Bridgetown::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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bridgetown-core/static_file.rb', line 29 def initialize(site, base, dir, name, collection = nil) # rubocop:disable Metrics/ParameterLists @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).with_dot_access data.permalink ||= if collection && !collection.builtin? "#{collection.default_permalink.chomp("/").chomp(".*")}.*" else "/:path.*" end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
7 8 9 |
# File 'lib/bridgetown-core/static_file.rb', line 7 def collection @collection end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/bridgetown-core/static_file.rb', line 7 def data @data end |
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
7 8 9 |
# File 'lib/bridgetown-core/static_file.rb', line 7 def extname @extname end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/bridgetown-core/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/bridgetown-core/static_file.rb', line 7 def relative_path @relative_path end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
7 8 9 |
# File 'lib/bridgetown-core/static_file.rb', line 7 def site @site end |
Class Method Details
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
13 14 15 |
# File 'lib/bridgetown-core/static_file.rb', line 13 def mtimes @mtimes ||= {} end |
.reset_cache ⇒ Object
17 18 19 |
# File 'lib/bridgetown-core/static_file.rb', line 17 def reset_cache @mtimes = nil end |
Instance Method Details
#basename ⇒ Object Also known as: basename_without_ext
124 125 126 |
# File 'lib/bridgetown-core/static_file.rb', line 124 def basename @basename ||= File.basename(name, ".*") end |
#cleaned_relative_path ⇒ Object
Similar to Bridgetown::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.
161 162 163 164 165 166 167 168 |
# File 'lib/bridgetown-core/static_file.rb', line 161 def cleaned_relative_path @cleaned_relative_path ||= begin cleaned = relative_path[0..-extname.length - 1] cleaned.gsub!(%r!\.*\z!, "") cleaned.sub!(@collection.relative_path, "") if @collection cleaned end end |
#defaults ⇒ Object
Returns the front matter defaults defined for the file's URL and/or type as defined in bridgetown.config.yml.
195 196 197 |
# File 'lib/bridgetown-core/static_file.rb', line 195 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.
55 56 57 58 59 60 61 62 |
# File 'lib/bridgetown-core/static_file.rb', line 55 def destination(dest) dest = site.in_dest_dir(dest) dest_url = url if site.base_path.present? && collection dest_url = dest_url.delete_prefix site.base_path(strip_slash_only: true) end site.in_dest_dir(dest, Bridgetown::URL.unescape_path(dest_url)) end |
#destination_rel_dir ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/bridgetown-core/static_file.rb', line 64 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.
201 202 203 |
# File 'lib/bridgetown-core/static_file.rb', line 201 def inspect "#<#{self.class} @relative_path=#{relative_path.inspect}>" end |
#modified? ⇒ Boolean
Is source path modified?
Returns true if modified since last write.
86 87 88 |
# File 'lib/bridgetown-core/static_file.rb', line 86 def modified? self.class.mtimes[path] != mtime end |
#modified_time ⇒ Object Also known as: date
72 73 74 |
# File 'lib/bridgetown-core/static_file.rb', line 72 def modified_time @modified_time ||= File.stat(path).mtime end |
#mtime ⇒ Object
Returns last modification time for this file.
79 80 81 |
# File 'lib/bridgetown-core/static_file.rb', line 79 def mtime modified_time.to_i end |
#path ⇒ Object
Returns source file path.
46 47 48 |
# File 'lib/bridgetown-core/static_file.rb', line 46 def path @path ||= File.join(*[@base, @dir, @name].compact) end |
#placeholders ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/bridgetown-core/static_file.rb', line 139 def placeholders { collection: @collection.label, path: cleaned_relative_path, output_ext: "", name: "", title: "", } end |
#relative_path_basename_without_prefix ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/bridgetown-core/static_file.rb', line 130 def relative_path_basename_without_prefix return_path = Pathname.new("") Pathname.new(cleaned_relative_path).each_filename do |filename| return_path += filename unless filename.starts_with?("_") end (return_path.dirname + return_path.basename(".*")).to_s end |
#to_liquid ⇒ Object
120 121 122 |
# File 'lib/bridgetown-core/static_file.rb', line 120 def to_liquid @to_liquid ||= Drops::StaticFileDrop.new(self) end |
#type ⇒ Object
Returns the type of the collection if present, nil otherwise.
189 190 191 |
# File 'lib/bridgetown-core/static_file.rb', line 189 def type @type ||= @collection&.label&.to_sym end |
#url ⇒ Object
Applies a similar URL-building technique as Bridgetown::Document that takes the collection's URL template into account. The default URL template can be overriden in the collection's configuration in bridgetown.config.yml.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/bridgetown-core/static_file.rb', line 173 def url @url ||= begin newly_processed = false special_posts_case = @collection&.label == "posts" && site.config.content_engine != "resource" base = if @collection.nil? || special_posts_case cleaned_relative_path else newly_processed = true Bridgetown::Resource::PermalinkProcessor.new(self).transform end.to_s.chomp("/") newly_processed ? base : "#{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).
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bridgetown-core/static_file.rb', line 106 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_rf(dest_path) Bridgetown.logger.debug "Saving file:", 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
bridgetown.config.yml contain published: false
.
94 95 96 97 98 99 |
# File 'lib/bridgetown-core/static_file.rb', line 94 def write? publishable = defaults.fetch("published", true) return publishable unless @collection publishable && @collection.write? end |