Class: Bridgetown::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
LayoutPlaceable, LiquidRenderable, Localizable, Publishable, Comparable
Defined in:
lib/bridgetown-core/resource/base.rb

Overview

rubocop:todo Metrics/ClassLength

Constant Summary collapse

DATE_FILENAME_MATCHER =
%r!^(?>.+/)*?(\d{2,4}-\d{1,2}-\d{1,2})-([^/]*)(\.[^.]+)$!.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Localizable

#all_locales

Methods included from LiquidRenderable

#liquid_engine_configured?, #render_with_liquid?, #yaml_file?

Methods included from LayoutPlaceable

#no_layout?, #place_in_layout?

Methods included from Publishable

#publishable?, #published?

Constructor Details

#initialize(model:) ⇒ Base

Returns a new instance of Base.

Parameters:



34
35
36
37
38
39
40
41
# File 'lib/bridgetown-core/resource/base.rb', line 34

def initialize(model:)
  @model = model
  @site = model.site
  @data = collection.data? ? HashWithDotAccess::Hash.new : front_matter_defaults
  @slots = []

  trigger_hooks :post_init
end

Instance Attribute Details

#contentString

Returns:

  • (String)


28
29
30
# File 'lib/bridgetown-core/resource/base.rb', line 28

def content
  @content
end

#dataHashWithDotAccess::Hash



13
14
15
# File 'lib/bridgetown-core/resource/base.rb', line 13

def data
  @data
end

#destinationDestination (readonly)

Returns:



16
17
18
# File 'lib/bridgetown-core/resource/base.rb', line 16

def destination
  @destination
end

#modelBridgetown::Model::Base (readonly)



19
20
21
# File 'lib/bridgetown-core/resource/base.rb', line 19

def model
  @model
end

#outputString

Returns:

  • (String)


28
29
30
# File 'lib/bridgetown-core/resource/base.rb', line 28

def output
  @output
end

#siteBridgetown::Site (readonly)

Returns:



22
23
24
# File 'lib/bridgetown-core/resource/base.rb', line 22

def site
  @site
end

#slotsArray<Bridgetown::Slot> (readonly)

Returns:



25
26
27
# File 'lib/bridgetown-core/resource/base.rb', line 25

def slots
  @slots
end

#untransformed_contentString

Returns:

  • (String)


28
29
30
# File 'lib/bridgetown-core/resource/base.rb', line 28

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.

Returns:

  • (Integer)

    -1, 0, or +1



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/bridgetown-core/resource/base.rb', line 277

def <=>(other) # rubocop:todo Metrics/AbcSize
  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_urlString

Returns:

  • (String)


174
175
176
# File 'lib/bridgetown-core/resource/base.rb', line 174

def absolute_url
  format_url destination&.absolute_url
end

#around_hook(hook_suffix) ⇒ Object



133
134
135
136
137
# File 'lib/bridgetown-core/resource/base.rb', line 133

def around_hook(hook_suffix)
  trigger_hooks :"pre_#{hook_suffix}"
  yield
  trigger_hooks :"post_#{hook_suffix}"
end

#as_jsonObject



261
262
263
# File 'lib/bridgetown-core/resource/base.rb', line 261

def as_json(*)
  to_h
end

#basename_without_extString

Returns:

  • (String)


154
155
156
# File 'lib/bridgetown-core/resource/base.rb', line 154

def basename_without_ext
  relative_path.basename(".*").to_s
end

#collectionBridgetown::Collection

Collection associated with this resource



46
47
48
# File 'lib/bridgetown-core/resource/base.rb', line 46

def collection
  model.collection
end

#dateObject



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

def date
  data["date"] ||= site.time
end

#extnameString

Returns:

  • (String)


159
160
161
# File 'lib/bridgetown-core/resource/base.rb', line 159

def extname
  relative_path.extname
end

#front_matter_defaultsHashWithDotAccess::Hash

Loads in any default front matter associated with the resource.



86
87
88
89
90
91
# File 'lib/bridgetown-core/resource/base.rb', line 86

def front_matter_defaults
  site.frontmatter_defaults.all(
    relative_path.to_s,
    collection.label.to_sym
  ).with_dot_access
end

#idString

Returns:

  • (String)


184
185
186
# File 'lib/bridgetown-core/resource/base.rb', line 184

def id
  model.origin.id
end

#inspectObject



269
270
271
# File 'lib/bridgetown-core/resource/base.rb', line 269

def inspect
  "#<#{self.class} #{id}>"
end

#layoutBridgetown::Layout

Layout associated with this resource This will output a warning if the layout can't be found.

Returns:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bridgetown-core/resource/base.rb', line 54

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

#next_resourceObject Also known as: next_doc, next



289
290
291
292
# File 'lib/bridgetown-core/resource/base.rb', line 289

def next_resource
  pos = collection.resources.index { |item| item.equal?(self) }
  collection.resources[pos + 1] if pos && pos < collection.resources.length - 1
end

#output_extString

Returns:

  • (String)


189
190
191
# File 'lib/bridgetown-core/resource/base.rb', line 189

def output_ext
  destination&.output_ext
end

#pathString

Returns:

  • (String)


169
170
171
# File 'lib/bridgetown-core/resource/base.rb', line 169

def path
  (model.origin.respond_to?(:original_path) ? model.origin.original_path : relative_path).to_s
end

Returns:

  • (String, nil)


164
165
166
# File 'lib/bridgetown-core/resource/base.rb', line 164

def permalink
  data&.permalink
end

#previous_resourceObject Also known as: previous_doc, previous



296
297
298
299
# File 'lib/bridgetown-core/resource/base.rb', line 296

def previous_resource
  pos = collection.resources.index { |item| item.equal?(self) }
  collection.resources[pos - 1] if pos&.positive?
end

#read!Bridgetown::Resource::Base Also known as: read



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bridgetown-core/resource/base.rb', line 101

def read!
  self.data = model.data_attributes
  self.content = model.content # could be nil

  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

#relationsBridgetown::Resource::Relations



79
80
81
# File 'lib/bridgetown-core/resource/base.rb', line 79

def relations
  @relations ||= Bridgetown::Resource::Relations.new(self)
end

#relative_pathPathname

The relative path of source file or file-like origin

Returns:

  • (Pathname)


69
70
71
# File 'lib/bridgetown-core/resource/base.rb', line 69

def relative_path
  model.origin.relative_path
end

#relative_path_basename_without_prefixString

Returns:

  • (String)


140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bridgetown-core/resource/base.rb', line 140

def relative_path_basename_without_prefix
  return_path = Pathname.new("")
  relative_path.each_filename do |filename|
    if matches = DATE_FILENAME_MATCHER.match(filename) # rubocop:disable Lint/AssignmentInCondition
      filename = matches[2] + matches[3]
    end

    return_path += filename unless filename.starts_with?("_")
  end

  (return_path.dirname + return_path.basename(".*")).to_s
end

#relative_urlString

Returns:

  • (String)


179
180
181
# File 'lib/bridgetown-core/resource/base.rb', line 179

def relative_url
  format_url destination&.relative_url
end

#requires_destination?Boolean Also known as: write?

Returns:

  • (Boolean)


220
221
222
# File 'lib/bridgetown-core/resource/base.rb', line 220

def requires_destination?
  collection.write? && data.config&.output != false
end

#summaryString

Ask the configured summary extension to output a summary of the content, otherwise return the first line.

Returns:

  • (String)


201
202
203
204
205
# File 'lib/bridgetown-core/resource/base.rb', line 201

def summary
  return summary_extension_output if respond_to?(:summary_extension_output)

  content.to_s.strip.lines.first.to_s.strip.html_safe
end

#taxonomiesHash<String, Hash<String => Bridgetown::Resource::TaxonomyType, Array<Bridgetown::Resource::TaxonomyTerm>>>

Returns:



209
210
211
212
213
214
215
216
217
218
# File 'lib/bridgetown-core/resource/base.rb', line 209

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_hObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/bridgetown-core/resource/base.rb', line 246

def to_h
  {
    id: id,
    absolute_url: absolute_url,
    relative_path: relative_path,
    relative_url: relative_url,
    date: date,
    data: data,
    taxonomies: taxonomies,
    untransformed_content: untransformed_content,
    content: content,
    output: output,
  }
end

#to_jsonObject



265
266
267
# File 'lib/bridgetown-core/resource/base.rb', line 265

def to_json(...)
  as_json(...).to_json(...)
end

#to_liquidDrops::ResourceDrop

Create a Liquid-understandable version of this resource.

Returns:



242
243
244
# File 'lib/bridgetown-core/resource/base.rb', line 242

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

#to_sObject



235
236
237
# File 'lib/bridgetown-core/resource/base.rb', line 235

def to_s
  output || content || ""
end

#transform!Object



122
123
124
125
126
# File 'lib/bridgetown-core/resource/base.rb', line 122

def transform!
  transformer.process! unless collection.data?

  self
end

#transformerBridgetown::Resource::Transformer



74
75
76
# File 'lib/bridgetown-core/resource/base.rb', line 74

def transformer
  @transformer ||= Bridgetown::Resource::Transformer.new(self)
end

#trigger_hooks(hook_name, *args) ⇒ Object



128
129
130
131
# File 'lib/bridgetown-core/resource/base.rb', line 128

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

#write(_dest = nil) ⇒ Object

Write the generated Document file to the destination directory.

dest - The String path to the destination dir.

Returns nothing.



230
231
232
233
# File 'lib/bridgetown-core/resource/base.rb', line 230

def write(_dest = nil)
  destination.write(output)
  trigger_hooks(:post_write)
end