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})-([^/]*)(\.[^.]+)$!.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
#all_locales, #localeless_path, #matches_resource?
#liquid_engine_configured?, #render_with_liquid?, #yaml_file?
#no_layout?, #place_in_layout?
#publishable?, #published?
Constructor Details
#initialize(model:) ⇒ Base
Returns a new instance of Base.
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
#content ⇒ String
28
29
30
|
# File 'lib/bridgetown-core/resource/base.rb', line 28
def content
@content
end
|
13
14
15
|
# File 'lib/bridgetown-core/resource/base.rb', line 13
def data
@data
end
|
16
17
18
|
# File 'lib/bridgetown-core/resource/base.rb', line 16
def destination
@destination
end
|
19
20
21
|
# File 'lib/bridgetown-core/resource/base.rb', line 19
def model
@model
end
|
#output ⇒ String
28
29
30
|
# File 'lib/bridgetown-core/resource/base.rb', line 28
def output
@output
end
|
22
23
24
|
# File 'lib/bridgetown-core/resource/base.rb', line 22
def site
@site
end
|
25
26
27
|
# File 'lib/bridgetown-core/resource/base.rb', line 25
def slots
@slots
end
|
#untransformed_content ⇒ 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.
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/bridgetown-core/resource/base.rb', line 277
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
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_json ⇒ Object
261
262
263
|
# File 'lib/bridgetown-core/resource/base.rb', line 261
def as_json(*)
to_h
end
|
#basename_without_ext ⇒ String
154
155
156
|
# File 'lib/bridgetown-core/resource/base.rb', line 154
def basename_without_ext
relative_path.basename(".*").to_s
end
|
Collection associated with this resource
46
47
48
|
# File 'lib/bridgetown-core/resource/base.rb', line 46
def collection
model.collection
end
|
#date ⇒ Object
193
194
195
|
# File 'lib/bridgetown-core/resource/base.rb', line 193
def date
data["date"] ||= site.time
end
|
#extname ⇒ String
159
160
161
|
# File 'lib/bridgetown-core/resource/base.rb', line 159
def extname
relative_path.extname
end
|
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
|
#id ⇒ String
184
185
186
|
# File 'lib/bridgetown-core/resource/base.rb', line 184
def id
model.origin.id
end
|
#inspect ⇒ Object
269
270
271
|
# File 'lib/bridgetown-core/resource/base.rb', line 269
def inspect
"#<#{self.class} #{id}>"
end
|
Layout associated with this resource
This will output a warning if the layout can't be found.
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_resource ⇒ Object
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_ext ⇒ String
189
190
191
|
# File 'lib/bridgetown-core/resource/base.rb', line 189
def output_ext
destination&.output_ext
end
|
#path ⇒ 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
|
#permalink ⇒ String?
164
165
166
|
# File 'lib/bridgetown-core/resource/base.rb', line 164
def permalink
data&.permalink
end
|
#previous_resource ⇒ Object
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
|
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
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
69
70
71
|
# File 'lib/bridgetown-core/resource/base.rb', line 69
def relative_path
model.origin.relative_path
end
|
#relative_path_basename_without_prefix ⇒ 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) 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
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?
220
221
222
|
# File 'lib/bridgetown-core/resource/base.rb', line 220
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.
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
|
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_h ⇒ Object
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_json ⇒ Object
265
266
267
|
# File 'lib/bridgetown-core/resource/base.rb', line 265
def to_json(...)
as_json(...).to_json(...)
end
|
Create a Liquid-understandable version of this resource.
242
243
244
|
# File 'lib/bridgetown-core/resource/base.rb', line 242
def to_liquid
@to_liquid ||= Drops::ResourceDrop.new(self)
end
|
#to_s ⇒ Object
235
236
237
|
# File 'lib/bridgetown-core/resource/base.rb', line 235
def to_s
output || content || ""
end
|
122
123
124
125
126
|
# File 'lib/bridgetown-core/resource/base.rb', line 122
def transform!
transformer.process! unless collection.data?
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
|