class Bridgetown::Resource::Base
Constants
- DATE_FILENAME_MATCHER
Attributes
@return [String]
@return [HashWithDotAccess::Hash]
@return [Destination]
@return [Bridgetown::Model::Base]
@return [String]
@return [Bridgetown::Site]
@return [String]
Public Class Methods
@param site [Bridgetown::Site] @param origin [Bridgetown::Resource::Origin]
# File lib/bridgetown-core/resource/base.rb, line 30 def initialize(model:) @model = model @site = model.site @data = front_matter_defaults trigger_hooks :post_init end
Public Instance Methods
Compare this document against another document. Comparison is a comparison between the 2 paths of the documents.
Returns -1, 0, +1 or nil depending on whether this doc's path is less than,
equal or greater than the other doc's path. See String#<=> for more details.
# File lib/bridgetown-core/resource/base.rb, line 269 def <=>(other) # rubocop:todo Metrics/AbcSize return nil unless other.respond_to?(:data) if data.date.respond_to?(:to_datetime) && other.data.date.respond_to?(:to_datetime) return data.date.to_datetime <=> other.data.date.to_datetime end cmp = data["date"] <=> other.data["date"] cmp = path <=> other.path if cmp.nil? || cmp.zero? cmp end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 167 def absolute_url format_url destination&.absolute_url end
# File lib/bridgetown-core/resource/base.rb, line 126 def around_hook(hook_suffix) trigger_hooks :"pre_#{hook_suffix}" yield trigger_hooks :"post_#{hook_suffix}" end
# File lib/bridgetown-core/resource/base.rb, line 252 def as_json(*) to_h end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 147 def basename_without_ext relative_path.basename(".*").to_s end
Collection
associated with this resource
@return [Bridgetown::Collection]
# File lib/bridgetown-core/resource/base.rb, line 41 def collection model.collection end
Merges new data into the existing data hash.
@param new_data [HashWithDotAccess::Hash]
# File lib/bridgetown-core/resource/base.rb, line 91 def data=(new_data) @data = @data.merge(new_data) end
# File lib/bridgetown-core/resource/base.rb, line 181 def date data["date"] ||= site.time end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 152 def extname relative_path.extname end
Loads in any default front matter associated with the resource.
@return [HashWithDotAccess::Hash]
# File lib/bridgetown-core/resource/base.rb, line 81 def front_matter_defaults site.frontmatter_defaults.all( relative_path.to_s, collection.label.to_sym ).with_dot_access end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 177 def id model.origin.id end
# File lib/bridgetown-core/resource/base.rb, line 260 def inspect "#<#{self.class} #{id}>" end
Layout
associated with this resource This will output a warning if the layout can't be found.
@return [Bridgetown::Layout]
# File lib/bridgetown-core/resource/base.rb, line 49 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
# File lib/bridgetown-core/resource/base.rb, line 281 def next_resource pos = collection.resources.index { |item| item.equal?(self) } collection.resources[pos + 1] if pos && pos < collection.resources.length - 1 end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 162 def path (model.origin.respond_to?(:original_path) ? model.origin.original_path : relative_path).to_s end
@return [String, nil]
# File lib/bridgetown-core/resource/base.rb, line 157 def permalink data&.permalink end
# File lib/bridgetown-core/resource/base.rb, line 287 def previous_resource pos = collection.resources.index { |item| item.equal?(self) } collection.resources[pos - 1] if pos&.positive? end
@return [Bridgetown::Resource::Base]
# File lib/bridgetown-core/resource/base.rb, line 96 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
@return [Bridgetown::Resource::Relations]
# File lib/bridgetown-core/resource/base.rb, line 74 def relations @relations ||= Bridgetown::Resource::Relations.new(self) end
The relative path of source file or file-like origin
@return [Pathname]
# File lib/bridgetown-core/resource/base.rb, line 64 def relative_path model.origin.relative_path end
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 133 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
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 172 def relative_url format_url destination&.relative_url end
# File lib/bridgetown-core/resource/base.rb, line 208 def requires_destination? collection.write? && data.config&.output != false end
Ask the configured summary extension to output a summary of the content, otherwise return the first line.
@return [String]
# File lib/bridgetown-core/resource/base.rb, line 189 def summary return summary_extension_output if respond_to?(:summary_extension_output) content.to_s.strip.lines.first.to_s.strip.html_safe end
@return [Hash<String, Hash<String => Bridgetown::Resource::TaxonomyType
,
Array<Bridgetown::Resource::TaxonomyTerm>>>]
# File lib/bridgetown-core/resource/base.rb, line 197 def taxonomies @taxonomies ||= site.taxonomy_types.values.each_with_object( HashWithDotAccess::Hash.new ) do |taxonomy, hsh| hsh[taxonomy.label] = { type: taxonomy, terms: [], } end end
# File lib/bridgetown-core/resource/base.rb, line 237 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
# File lib/bridgetown-core/resource/base.rb, line 256 def to_json(*options) as_json(*options).to_json(*options) end
Create a Liquid-understandable version of this resource.
@return [Drops::ResourceDrop] represents this resource's data.
# File lib/bridgetown-core/resource/base.rb, line 233 def to_liquid @to_liquid ||= Drops::ResourceDrop.new(self) end
# File lib/bridgetown-core/resource/base.rb, line 226 def to_s output || content || "" end
# File lib/bridgetown-core/resource/base.rb, line 117 def transform! transformer.process! unless collection.data? end
@return [Bridgetown::Resource::Transformer]
# File lib/bridgetown-core/resource/base.rb, line 69 def transformer @transformer ||= Bridgetown::Resource::Transformer.new(self) end
# File lib/bridgetown-core/resource/base.rb, line 121 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 the generated Document
file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
# File lib/bridgetown-core/resource/base.rb, line 221 def write(_dest = nil) destination.write(output) trigger_hooks(:post_write) end
# File lib/bridgetown-core/resource/base.rb, line 212 def write? requires_destination? && site.publisher.publish?(self) end
Private Instance Methods
# File lib/bridgetown-core/resource/base.rb, line 295 def ensure_default_data slug = if matches = relative_path.to_s.match(DATE_FILENAME_MATCHER) # rubocop:disable Lint/AssignmentInCondition set_date_from_string(matches[1]) unless data.date matches[2] else basename_without_ext end data.slug ||= slug data.title ||= Bridgetown::Utils.titleize_slug(slug) end
# File lib/bridgetown-core/resource/base.rb, line 335 def format_url(url) url.to_s.sub(%r{index\.html?$}, "").sub(%r{\.html?$}, "") end
# File lib/bridgetown-core/resource/base.rb, line 325 def import_taxonomies_from_data taxonomies.each do |_label, metadata| Array(data[metadata.type.key]).each do |term| metadata.terms << TaxonomyTerm.new( resource: self, label: term, type: metadata.type ) end end end
# File lib/bridgetown-core/resource/base.rb, line 307 def set_date_from_string(new_date) # rubocop:disable Naming/AccessorMethodName return unless new_date.is_a?(String) data.date = Bridgetown::Utils.parse_date( new_date, "Document '#{relative_path}' does not have a valid date in the #{model}." ) end