class Bridgetown::Resource::Base

Constants

DATE_FILENAME_MATCHER

Attributes

content[RW]

@return [String]

data[R]

@return [HashWithDotAccess::Hash]

destination[R]

@return [Destination]

model[R]

@return [Bridgetown::Model::Base]

output[RW]

@return [String]

site[R]

@return [Bridgetown::Site]

untransformed_content[RW]

@return [String]

Public Class Methods

new(model:) click to toggle source

@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

<=>(other) click to toggle source

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
absolute_url() click to toggle source

@return [String]

# File lib/bridgetown-core/resource/base.rb, line 167
def absolute_url
  format_url destination&.absolute_url
end
around_hook(hook_suffix) { || ... } click to toggle source
# 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
as_json(*) click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 252
def as_json(*)
  to_h
end
basename_without_ext() click to toggle source

@return [String]

# File lib/bridgetown-core/resource/base.rb, line 147
def basename_without_ext
  relative_path.basename(".*").to_s
end
collection() click to toggle source

Collection associated with this resource

@return [Bridgetown::Collection]

# File lib/bridgetown-core/resource/base.rb, line 41
def collection
  model.collection
end
data=(new_data) click to toggle source

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
date() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 181
def date
  data["date"] ||= site.time
end
extname() click to toggle source

@return [String]

# File lib/bridgetown-core/resource/base.rb, line 152
def extname
  relative_path.extname
end
front_matter_defaults() click to toggle source

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
id() click to toggle source

@return [String]

# File lib/bridgetown-core/resource/base.rb, line 177
def id
  model.origin.id
end
inspect() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 260
def inspect
  "#<#{self.class} #{id}>"
end
layout() click to toggle source

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
next_doc()
Alias for: next_resource
next_resource() click to toggle source
# 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
Also aliased as: next_doc
path() click to toggle source

@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
previous_doc()
Alias for: previous_resource
previous_resource() click to toggle source
# 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
Also aliased as: previous_doc
read()
Alias for: read!
read!() click to toggle source

@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
Also aliased as: read
relations() click to toggle source

@return [Bridgetown::Resource::Relations]

# File lib/bridgetown-core/resource/base.rb, line 74
def relations
  @relations ||= Bridgetown::Resource::Relations.new(self)
end
relative_path() click to toggle source

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
relative_path_basename_without_prefix() click to toggle source

@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
relative_url() click to toggle source

@return [String]

# File lib/bridgetown-core/resource/base.rb, line 172
def relative_url
  format_url destination&.relative_url
end
requires_destination?() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 208
def requires_destination?
  collection.write? && data.config&.output != false
end
summary() click to toggle source

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
taxonomies() click to toggle source

@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
to_h() click to toggle source
# 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
to_json(*options) click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 256
               def to_json(*options)
  as_json(*options).to_json(*options)
end
to_liquid() click to toggle source

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
to_s() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 226
def to_s
  output || content || ""
end
transform!() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 117
def transform!
  transformer.process! unless collection.data?
end
transformer() click to toggle source

@return [Bridgetown::Resource::Transformer]

# File lib/bridgetown-core/resource/base.rb, line 69
def transformer
  @transformer ||= Bridgetown::Resource::Transformer.new(self)
end
trigger_hooks(hook_name, *args) click to toggle source
# 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(_dest = nil) click to toggle source

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
write?() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 212
def write?
  requires_destination? && site.publisher.publish?(self)
end

Private Instance Methods

ensure_default_data() click to toggle source
# 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
format_url(url) click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 335
def format_url(url)
  url.to_s.sub(%r{index\.html?$}, "").sub(%r{\.html?$}, "")
end
import_taxonomies_from_data() click to toggle source
# 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
normalize_categories_and_tags() click to toggle source
# File lib/bridgetown-core/resource/base.rb, line 316
def normalize_categories_and_tags
  data.categories = Bridgetown::Utils.pluralized_array_from_hash(
    data, :category, :categories
  )
  data.tags = Bridgetown::Utils.pluralized_array_from_hash(
    data, :tag, :tags
  )
end
set_date_from_string(new_date) click to toggle source
# 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