Class: Bridgetown::PrototypeGenerator

Inherits:
Generator show all
Defined in:
lib/bridgetown-core/generators/prototype_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize

Methods included from Prioritizable

#<=>, included

Constructor Details

This class inherits a constructor from Bridgetown::Plugin

Instance Attribute Details

#siteBridgetown::Site (readonly)

Returns:



27
28
29
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 27

def site
  @site
end

Class Method Details

.add_matching_template(template) ⇒ Object



34
35
36
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 34

def self.add_matching_template(template)
  matching_templates << template
end

.matching_templatesSet<Bridgetown::Page, Bridgetown::Resource::Base>

Returns:



30
31
32
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 30

def self.matching_templates
  @matching_templates ||= Set.new
end

Instance Method Details

#ensure_pagination_enabledObject



65
66
67
68
69
70
71
72
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 65

def ensure_pagination_enabled
  return if @site.config.dig(:pagination, :enabled)

  Bridgetown.logger.warn(
    "Pagination:",
    "Must be enabled for prototype pages to contain matches"
  )
end

#generate(site) ⇒ Object

Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 39

def generate(site)
  @site = site
  page_list = site.collections.pages.resources

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

  return unless 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

#generate_new_page_from_prototype(prototype_page, search_term, term) ⇒ Object



100
101
102
103
104
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 100

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) ⇒ Array<String>

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

Parameters:

  • search_term (String)

Returns:

  • (Array<String>)


111
112
113
114
115
116
117
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 111

def terms_matching_pages(search_term)
  pages_list = site.collections[@configured_collection].resources

  Bridgetown::Paginate::PaginationIndexer.index_documents_by(
    pages_list, search_term
  ).keys
end

#validate_search_term(prototype_page) ⇒ String?

Check incoming prototype configuration and normalize options.

Parameters:

Returns:

  • (String, nil)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bridgetown-core/generators/prototype_generator.rb', line 79

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