class Bridgetown::PrototypePage

Attributes

prototyped_page[R]

@return [Bridgetown::Page, Bridgetown::Resource::Base]

Public Class Methods

new(prototyped_page, collection, search_term, term) click to toggle source

@param prototyped_page [Bridgetown::Page, Bridgetown::Resource::Base] @param collection [Bridgetown::Collection] @param search_term [String] @param term [String]

# File lib/bridgetown-core/generators/prototype_generator.rb, line 133
def initialize(prototyped_page, collection, search_term, term)
  @prototyped_page = prototyped_page
  @site = prototyped_page.site
  @url = ""
  @name = "index.html"
  @path = prototyped_page.path

  process(@name)

  self.data = Bridgetown::Utils.deep_merge_hashes prototyped_page.data, {}
  self.content = prototyped_page.content

  # Perform some validation that is also performed in Bridgetown::Page
  validate_data! prototyped_page.path
  validate_permalink! prototyped_page.path

  @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s.sub(%r{^_pages}, "")
  @path = site.in_source_dir(@dir, @name)

  process_prototype_page_data(collection, search_term, term)

  Bridgetown::Hooks.trigger :pages, :post_init, self
end

Public Instance Methods

process_prototype_page_data(collection, search_term, term) click to toggle source
# File lib/bridgetown-core/generators/prototype_generator.rb, line 157
def process_prototype_page_data(collection, search_term, term)
  # Fill in pagination details to be handled later by Bridgetown::Paginate
  data["pagination"] = Bridgetown::Utils.deep_merge_hashes(
    prototyped_page.data["pagination"].to_h, {
      "enabled"     => true,
      "collection"  => collection,
      "where_query" => [search_term, term],
    }
  )
  # Use the original prototype page term so we get "tag" back, not "tags":
  data[prototyped_page.data["prototype"]["term"]] = term
  # Process title and slugs/URLs:
  process_title_data_placeholder(search_term, term)
  process_title_simple_placeholders(term)
  slugify_term(term)
end
process_title_data_placeholder(search_term, term) click to toggle source

rubocop:todo Metrics/AbcSize

# File lib/bridgetown-core/generators/prototype_generator.rb, line 175
def process_title_data_placeholder(search_term, term)
  if prototyped_page.data["prototype"]["data"]
    if data["title"]&.include?(":prototype-data-label")
      related_data = site.data[prototyped_page.data["prototype"]["data"]].dig(term)
      if related_data
        data["#{search_term}_data"] = related_data
        data_label = related_data[prototyped_page.data["prototype"]["data_label"]]
        data["title"] = data["title"].gsub(
          ":prototype-data-label", data_label
        )
      end
    end
  end
end
process_title_simple_placeholders(term) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/bridgetown-core/generators/prototype_generator.rb, line 191
def process_title_simple_placeholders(term)
  if data["title"]&.include?(":prototype-term-titleize")
    data["title"] = data["title"].gsub(
      ":prototype-term-titleize", Bridgetown::Utils.titleize_slug(term)
    )
  end

  if data["title"]&.include?(":prototype-term")
    data["title"] = data["title"].gsub(
      ":prototype-term", term
    )
  end
end
slugify_term(term) click to toggle source
# File lib/bridgetown-core/generators/prototype_generator.rb, line 205
def slugify_term(term)
  term_slug = Bridgetown::Utils.slugify(term, mode: site.config.slugify_mode)
  @url = if permalink.is_a?(String)
           data["permalink"] = data["permalink"].sub(":term", term_slug)
         else
           "/#{@dir}/#{term_slug}/"
         end
end