Class: Bridgetown::Resource::Base
  
  
  
Overview
  
    rubocop:todo Metrics/ClassLength
   
 
  
  
    
      Constant Summary
      collapse
    
    
      
        - DATE_FILENAME_MATCHER =
          
        
 
        %r!^(?>.+/)*?(\d{2,4}-\d{1,2}-\d{1,2})-([^/]*)(\.[^.]+)$! 
      
    
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #all_locales, #localeless_path, #matches_resource?
  
  
  
  
  
  
  
  
  
  #no_layout?, #place_in_layout?
  
  
  
  
  
  
  
  
  
  #publishable?, #published?
  
  
  
  
  
  
  
  
  
  ===
  
  Constructor Details
  
    
  
  
    #initialize(model:)  ⇒ Base 
  
  
  
  
    Returns a new instance of Base.
   
 
  
    
      
40
41
42
43
44
45
46
47
48
49
50
51 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 40
def initialize(model:)
  @model = model
  @site = model.site
  @data = Signalize.signal(
    collection.data? ? HashWithDotAccess::Hash.new : front_matter_defaults
  )
    @content_signal = Signalize.signal(0)
  @slots = []
  trigger_hooks :post_init
end
     | 
  
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #content  ⇒ String 
  
  
  
  
    Returns the resource content minus its layout.
   
 
  
    
      
119
120
121
122 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 119
def content
  @content_signal.value   @content
end 
     | 
  
 
    
      
      
      
  
  
    
      
14
15
16 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 14
def destination
  @destination
end 
     | 
  
 
    
      
      
      
  
  
    #fast_refresh_order  ⇒ Boolean  
  
  
  
  
    
      
26
27
28 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 26
def fast_refresh_order
  @fast_refresh_order
end 
     | 
  
 
    
      
      
      
  
  
    
      
17
18
19 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 17
def model
  @model
end 
     | 
  
 
    
      
      
      
  
  
    #output  ⇒ String 
  
  
  
  
    
      
34
35
36 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 34
def output
  @output
end 
     | 
  
 
    
      
      
      
  
  
    
      
20
21
22 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 20
def site
  @site
end 
     | 
  
 
    
      
      
      
  
  
    
      
23
24
25 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 23
def slots
  @slots
end 
     | 
  
 
    
      
      
      
  
  
    #untransformed_content  ⇒ String 
  
  
  
  
    
      
29
30
31 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 29
def untransformed_content
  @untransformed_content
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #<=>(other)  ⇒ Integer 
  
  
  
  
    Compare this resource against another resource.
Comparison is a comparison between the 2 dates or paths of the resources.
   
 
  
    
      
319
320
321
322
323
324
325
326
327
328
329 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 319
def <=>(other)   return nil unless other.respond_to?(:data)
  cmp = if data.date.respond_to?(:to_datetime) && other.data.date.respond_to?(:to_datetime)
          data.date.to_datetime <=> other.data.date.to_datetime
        end
  cmp = data["date"] <=> other.data["date"] if cmp.nil?
  cmp = path <=> other.path if cmp.nil? || cmp.zero?
  cmp
end
     | 
  
 
    
      
  
  
    #absolute_url  ⇒ String 
  
  
  
  
    
      
219
220
221 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 219
def absolute_url
  @absolute_url ||= format_url(destination&.absolute_url)
end 
     | 
  
 
    
      
  
  
    #around_hook(hook_suffix)  ⇒ Object 
  
  
  
  
    
      
178
179
180
181
182 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 178
def around_hook(hook_suffix)
  trigger_hooks :"pre_#{hook_suffix}"
  yield
  trigger_hooks :"post_#{hook_suffix}"
end
     | 
  
 
    
      
  
  
    #as_json  ⇒ Object 
  
  
  
  
    
      
303
304
305 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 303
def as_json(*)
  to_h
end 
     | 
  
 
    
      
  
  
    #basename_without_ext  ⇒ String 
  
  
  
  
    
      
199
200
201 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 199
def basename_without_ext
  relative_path.basename(".*").to_s
end
     | 
  
 
    
      
  
  
    #call  ⇒ String 
  
  
  
  
    Transforms the resource and returns the full output
   
 
  
    
      
171 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 171
def call(*) = transform!.output 
     | 
  
 
    
      
  
  
    Collection associated with this resource
   
 
  
    
      
56
57
58 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 56
def collection
  model.collection
end 
     | 
  
 
    
      
  
  
    
      
101
102
103 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 101
def data
  @data.value
end 
     | 
  
 
    
      
  
  
    #data=(new_data)  ⇒ Object 
  
  
  
  
    Merges new data into the existing data hash.
   
 
  
    
      
108
109
110
111
112
113
114
115
116 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 108
def data=(new_data)
  mark_for_fast_refresh! if site.config.fast_refresh && write?
  Signalize.batch do
    @content_signal.value += 1
    @data.value = @data.value.merge(new_data)
  end
  @data.peek
end
     | 
  
 
    
      
  
  
    #date  ⇒ Object 
  
  
  
  
    
      
238
239
240 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 238
def date
  data["date"] ||= site.time
end 
     | 
  
 
    
      
  
  
    #deconstruct_keys  ⇒ Object 
  
  
  
  
    
      
345
346
347 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 345
def deconstruct_keys(...)
  @data.value.deconstruct_keys(...)
end 
     | 
  
 
    
      
  
  
    
      
381
382
383 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 381
def dispose_of_transform_effect
  @transform_effect_disposal&.()
end 
     | 
  
 
    
      
  
  
    #extname  ⇒ String 
  
  
  
  
    
      
204
205
206 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 204
def extname
  relative_path.extname
end 
     | 
  
 
    
      
  
  
    Loads in any default front matter associated with the resource.
   
 
  
    
      
96
97
98 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 96
def front_matter_defaults
  site.frontmatter_defaults.all(relative_path.to_s, collection.label.to_sym).as_dots
end 
     | 
  
 
    
      
  
  
    #id  ⇒ String 
  
  
  
  
    
      
229
230
231 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 229
def id
  model.origin.id
end 
     | 
  
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
  
  
    
      
311
312
313 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 311
def inspect
  "#<#{self.class} #{id}>"
end
     | 
  
 
    
      
  
  
    Layout associated with this resource
This will output a warning if the layout can't be found.
   
 
  
    
      
64
65
66
67
68
69
70
71
72
73
74 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 64
def layout
  return @layout if @layout
  return if no_layout?
  @layout = site.layouts[data.layout].tap do |layout|
    unless layout
      Bridgetown.logger.warn "Resource:", "Layout '#{data.layout}' " \
                                          "requested via #{relative_path} does not exist."
    end
  end
end
     | 
  
 
    
      
  
  
    #mark_for_fast_refresh!  ⇒ Object 
  
  
  
  
    
      
349
350
351
352 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 349
def mark_for_fast_refresh!
  @fast_refresh_order = site.fast_refresh_ordering
  site.fast_refresh_ordering += 1
end 
     | 
  
 
    
      
  
  
    #next_resource  ⇒ Object 
  
  
    Also known as:
    next_doc, next
    
  
  
  
    
      
331
332
333
334 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 331
def next_resource
  pos = collection.resources.index { |item| item.equal?(self) }
  collection.resources[pos + 1] if pos && pos < collection.resources.length - 1
end
     | 
  
 
    
      
  
  
    #output_ext  ⇒ String 
  
  
  
  
    
      
234
235
236 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 234
def output_ext
  destination&.output_ext
end 
     | 
  
 
    
      
  
  
    #path  ⇒ String 
  
  
  
  
    
      
214
215
216 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 214
def path
  (model.origin.respond_to?(:original_path) ? model.origin.original_path : relative_path).to_s
end 
     | 
  
 
    
      
  
  
    #permalink  ⇒ String? 
  
  
  
  
    
      
209
210
211 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 209
def permalink
  data&.permalink
end 
     | 
  
 
    
      
  
  
    #prepare_for_fast_refresh!  ⇒ Object 
  
  
  
  
  
    
      
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 358
def prepare_for_fast_refresh!   dispose_of_transform_effect
  FileUtils.rm(destination.output_path, force: true) if requires_destination?
  past_values = @data.peek.select do |key|
    key == "categories" || key == "tags" || site.taxonomy_types.keys.any?(key)
  end
  origin_data = model.origin.read
  correct_locale = origin_data["locale"] || origin_data[:locale] || data.locale
  model.attributes = origin_data
  model.attributes.locale = correct_locale
  @relative_url = @absolute_url = nil   read!
  tax_diff = past_values.any? { |k, v| @data.peek[k] != v }
  if tax_diff && !collection.data?
        unmark_for_fast_refresh!
    false
  else
    true
  end
end
     | 
  
 
    
      
  
  
    #previous_resource  ⇒ Object 
  
  
    Also known as:
    previous_doc, previous
    
  
  
  
    
      
338
339
340
341 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 338
def previous_resource
  pos = collection.resources.index { |item| item.equal?(self) }
  collection.resources[pos - 1] if pos&.positive?
end
     | 
  
 
    
      
  
  
    
      
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 125
def read!
  self.data = model.data_attributes
  self.content = model.content 
  unless collection.data?
    self.untransformed_content = content
    normalize_categories_and_tags
    import_taxonomies_from_data
    ensure_default_data
    transformer.execute_inline_ruby!
    set_date_from_string(data.date)
  end
  @destination = Destination.new(self) if requires_destination?
  trigger_hooks :post_read
  self
end
     | 
  
 
    
      
    
      
  
  
    #relative_path  ⇒ Pathname 
  
  
  
  
    The relative path of source file or file-like origin
   
 
  
    
      
79
80
81 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 79
def relative_path
  model.origin.relative_path
end 
     | 
  
 
    
      
  
  
    #relative_path_basename_without_prefix  ⇒ String 
  
  
  
  
    
      
185
186
187
188
189
190
191
192
193
194
195
196 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 185
def relative_path_basename_without_prefix
  return_path = Pathname.new("")
  relative_path.each_filename do |filename|
    if matches = DATE_FILENAME_MATCHER.match(filename)       filename = matches[2] + matches[3]
    end
    return_path += filename unless filename.starts_with?("_")
  end
  (return_path.dirname + return_path.basename(".*")).to_s
end
     | 
  
 
    
      
  
  
    #relative_url  ⇒ String 
  
  
  
  
    
      
224
225
226 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 224
def relative_url
  @relative_url ||= format_url(destination&.relative_url)
end 
     | 
  
 
    
      
  
  
    #requires_destination?  ⇒ Boolean 
  
  
    Also known as:
    write?
    
  
  
  
    
      
265
266
267 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 265
def requires_destination?
  collection.write? && data.config&.output != false
end 
     | 
  
 
    
      
  
  
    #summary  ⇒ String 
  
  
  
  
    Ask the configured summary extension to output a summary of the content,
otherwise return the first line.
   
 
  
    
      
246
247
248
249
250 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 246
def summary
  return summary_extension_output if respond_to?(:summary_extension_output)
  content.to_s.strip.lines.first.to_s.strip.html_safe
end 
     | 
  
 
    
      
  
  
    
      
254
255
256
257
258
259
260
261
262
263 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 254
def taxonomies
  @taxonomies ||= site.taxonomy_types.values.each_with_object(
    HashWithDotAccess::Hash.new
  ) do |taxonomy, hsh|
    hsh[taxonomy.label] = {
      type: taxonomy,
      terms: [],
    }
  end
end
     | 
  
 
    
      
  
  
    #to_h  ⇒ Object 
  
  
  
  
    
      
288
289
290
291
292
293
294
295
296
297
298
299
300
301 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 288
def to_h
  {
    id:,
    absolute_url:,
    relative_path:,
    relative_url:,
    date:,
    data:,
    taxonomies:,
    untransformed_content:,
    content:,
    output:,
  }
end
     | 
  
 
    
      
  
  
    #to_json  ⇒ Object 
  
  
  
  
    
      
307
308
309 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 307
def to_json(...)
  as_json(...).to_json(...)
end 
     | 
  
 
    
      
  
  
    Create a Liquid-understandable version of this resource.
   
 
  
    
      
284
285
286 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 284
def to_liquid
  @to_liquid ||= Drops::ResourceDrop.new(self)
end 
     | 
  
 
    
      
  
  
    #to_s  ⇒ Object 
  
  
  
  
    
      
277
278
279 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 277
def to_s
  output || content || ""
end 
     | 
  
 
    
      
  
  
    rubocop:todo Metrics/CyclomaticComplexity
   
 
  
  
    
      
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 146
def transform!   internal_error = nil
  @transform_effect_disposal = Signalize.effect do
    if !@fast_refresh_order && @previously_transformed
      self.content = untransformed_content
      @transformer = nil
      mark_for_fast_refresh! if site.config.fast_refresh && write?
      next
    end
    transformer.process! unless collection.data?
    slots.clear
    @previously_transformed = true
  rescue StandardError, SyntaxError => e
    internal_error = e
  end
  raise internal_error if internal_error
  self
end
     | 
  
 
    
      
    
      
  
  
    #trigger_hooks(hook_name, *args)  ⇒ Object 
  
  
  
  
    
      
173
174
175
176 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 173
def trigger_hooks(hook_name, *args)
  Bridgetown::Hooks.trigger collection.label.to_sym, hook_name, self, *args if collection
  Bridgetown::Hooks.trigger :resources, hook_name, self, *args
end 
     | 
  
 
    
      
  
  
    #unmark_for_fast_refresh!  ⇒ Object 
  
  
  
  
    
      
354
355
356 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 354
def unmark_for_fast_refresh!
  @fast_refresh_order = nil
end 
     | 
  
 
    
      
  
  
    #write(_dest = nil)  ⇒ Object 
  
  
  
  
    Write the generated resource file to the destination directory.
   
 
  
  
    
      
271
272
273
274
275 
     | 
    
      # File 'lib/bridgetown-core/resource/base.rb', line 271
def write(_dest = nil)
  destination.write(output)
  unmark_for_fast_refresh!
  trigger_hooks(:post_write)
end 
     |