class Bridgetown::PrototypeGenerator

Attributes

site[R]

@return [Bridgetown::Site]

Public Class Methods

add_matching_template(template) click to toggle source
# File lib/bridgetown-core/generators/prototype_generator.rb, line 34
def self.add_matching_template(template)
  matching_templates << template
end
matching_templates() click to toggle source

@return [Set<Bridgetown::Page, Bridgetown::Resource::Base>]

# File lib/bridgetown-core/generators/prototype_generator.rb, line 30
def self.matching_templates
  @matching_templates ||= Set.new
end

Public Instance Methods

ensure_pagination_enabled() click to toggle source
# File lib/bridgetown-core/generators/prototype_generator.rb, line 66
def ensure_pagination_enabled
  unless @site.config.dig(:pagination, :enabled)
    Bridgetown.logger.warn(
      "Pagination:",
      "Must be enabled for prototype pages to contain matches"
    )
  end
end
generate(site) click to toggle source

@param site [Bridgetown::Site]

# File lib/bridgetown-core/generators/prototype_generator.rb, line 39
def generate(site)
  @site = site
  @configured_collection = "posts" unless site.uses_resource?
  page_list = site.uses_resource? ? site.collections.pages.resources : site.pages

  prototype_pages = self.class.matching_templates.select do |page|
    page_list.include?(page)
  end

  if prototype_pages.length.positive?
    ensure_pagination_enabled

    page_list.reject! do |page|
      prototype_pages.include? page
    end

    prototype_pages.each do |prototype_page|
      search_term = validate_search_term(prototype_page)
      next if search_term.nil?

      terms_matching_pages(search_term).each do |term|
        generate_new_page_from_prototype(prototype_page, search_term, term)
      end
    end
  end
end
generate_new_page_from_prototype(prototype_page, search_term, term) click to toggle source
# File lib/bridgetown-core/generators/prototype_generator.rb, line 101
def generate_new_page_from_prototype(prototype_page, search_term, term)
  new_page = PrototypePage.new(prototype_page, @configured_collection, search_term, term)
  site.add_generated_page new_page
  new_page
end
terms_matching_pages(search_term) click to toggle source

Provide a list of all relevent indexed values for the given term.

@param search_term [String]

@return [Array<String>]

# File lib/bridgetown-core/generators/prototype_generator.rb, line 112
def terms_matching_pages(search_term)
  pages_list = if site.uses_resource?
                 site.collections[@configured_collection].resources
               else
                 site.collections[@configured_collection].docs
               end

  Bridgetown::Paginate::PaginationIndexer.index_documents_by(
    pages_list, search_term
  ).keys
end
validate_search_term(prototype_page) click to toggle source

Check incoming prototype configuration and normalize options.

@param prototype_page [Bridgetown::Page, Bridgetown::Resource::Base]

@return [String, nil]

# File lib/bridgetown-core/generators/prototype_generator.rb, line 80
def validate_search_term(prototype_page)
  # @type [String]
  search_term = prototype_page.data["prototype"]["term"].to_s
  return nil unless search_term.present?

  if prototype_page.data["prototype"]["collection"]
    @configured_collection = prototype_page.data["prototype"]["collection"].to_s
  end

  unless site.collections[@configured_collection]
    Bridgetown.logger.warn(
      "No collection specified for prototype page #{prototype_page.relative_path}"
    )
    return nil
  end

  # Categories and Tags are unique in that singular and plural front matter
  # can be present for each
  search_term.sub(%r!^category$!, "categories").sub(%r!^tag$!, "tags")
end