Class: Bridgetown::StaticFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
bridgetown-core/lib/bridgetown-core/static_file.rb

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.

Parameters:

  • site (Bridgetown::Site)
  • base (String)

    path to the .

  • dir (String)

    path between and the file.

  • name (String)

    filename of the file.

  • collection (Bridgetown::Collection) (defaults to: nil)

    optional collection the file is attached to



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 30

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).as_dots
  data.permalink ||= if collection && !collection.builtin?
                       "#{collection.default_permalink.chomp("/").chomp(".*")}.*"
                     else
                       "/:path.*"
                     end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def collection
  @collection
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def data
  @data
end

#extnameObject (readonly)

Returns the value of attribute extname.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def extname
  @extname
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def name
  @name
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def relative_path
  @relative_path
end

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 8

def site
  @site
end

Class Method Details

.mtimesObject

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



14
15
16
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 14

def mtimes
  @mtimes ||= {}
end

.reset_cacheObject



18
19
20
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 18

def reset_cache
  @mtimes = nil
end

Instance Method Details

#basenameObject Also known as: basename_without_ext



125
126
127
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 125

def basename
  @basename ||= File.basename(name, ".*")
end

#cleaned_relative_pathObject

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 'bridgetown-core/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

#defaultsObject

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



193
194
195
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 193

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.



56
57
58
59
60
61
62
63
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 56

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::Utils.unencode_uri(dest_url))
end

#destination_rel_dirObject



65
66
67
68
69
70
71
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 65

def destination_rel_dir
  if @collection
    File.dirname(url)
  else
    @dir
  end
end

#inspectObject

Returns a debug string on inspecting the static file. Includes only the relative path of the object.



199
200
201
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 199

def inspect
  "#<#{self.class} @relative_path=#{relative_path.inspect}>"
end

#modified?Boolean

Is source path modified?

Returns true if modified since last write.

Returns:

  • (Boolean)


87
88
89
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 87

def modified?
  self.class.mtimes[path] != mtime
end

#modified_timeObject Also known as: date



73
74
75
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 73

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

#mtimeObject

Returns last modification time for this file.



80
81
82
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 80

def mtime
  modified_time.to_i
end

#pathObject

Returns source file path.



47
48
49
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 47

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

#placeholdersObject



140
141
142
143
144
145
146
147
148
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 140

def placeholders
  {
    collection: @collection.label,
    path: cleaned_relative_path,
    output_ext: "",
    name: "",
    title: "",
  }
end

#relative_path_basename_without_prefixObject



131
132
133
134
135
136
137
138
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 131

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_liquidObject



121
122
123
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 121

def to_liquid
  @to_liquid ||= Drops::StaticFileDrop.new(self)
end

#typeObject

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



187
188
189
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 187

def type
  @type ||= @collection&.label&.to_sym
end

#urlObject

Applies a similar URL-building technique as resources 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
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 173

def url
  @url ||= begin
    newly_processed = false
    base = if @collection.nil?
             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).



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 107

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.

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'bridgetown-core/lib/bridgetown-core/static_file.rb', line 95

def write?
  publishable = defaults.fetch("published", true)
  return publishable unless @collection

  publishable && @collection.write?
end