Class: Bridgetown::PrototypePage

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

Constant Summary

Constants inherited from GeneratedPage

GeneratedPage::HTML_EXTENSIONS

Instance Attribute Summary

Attributes inherited from GeneratedPage

#basename, #content, #data, #dir, #ext, #fast_refresh_order, #name, #original_resource, #output, #paginator, #site

Instance Method Summary collapse

Methods inherited from GeneratedPage

#[], #add_permalink_suffix, #converter_output_ext, #converters, #destination, #html?, #index?, #inspect, #layout, #mark_for_fast_refresh!, #output_ext, #output_exts, #path, #permalink, #permalink_ext, #place_into_layouts, #process, #relative_path, #slots, #template, #to_liquid, #to_s, #transform!, #trigger_hooks, #type, #unmark_for_fast_refresh!, #url, #write, #write?

Methods included from Transformable

#transform_content, #transform_with_layout

Methods included from Publishable

#publishable?, #published?

Methods included from Localizable

#all_locales, #localeless_path, #matches_resource?

Methods included from LayoutPlaceable

#no_layout?, #place_in_layout?

Constructor Details

#initialize(prototyped_page, collection, search_term, term) ⇒ PrototypePage

Returns a new instance of PrototypePage.

Parameters:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 127

def initialize(prototyped_page, collection, search_term, term) # rubocop:disable Lint/MissingSuper
  self.original_resource = prototyped_page
  @site = prototyped_page.site
  @url = ""
  @name = "index.html"
  @ext = ".html"
  @basename = "index"
  @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s.sub(%r{^_pages}, "")
  @path = site.in_source_dir(@dir, @name)
  @collection = collection
  @search_term = search_term
  @term = term

  fast_refresh!

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

Instance Method Details

#fast_refresh!Object



145
146
147
148
149
150
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 145

def fast_refresh!
  self.data = Bridgetown::Utils.deep_merge_hashes prototyped_page.data, {}
  self.content = prototyped_page.content
  @original_title = data["title"]
  process_prototype_page_data(@collection, @search_term, @term)
end

#process_prototype_page_data(collection, search_term, term) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 152

def process_prototype_page_data(collection, search_term, term)
  pagination_key = prototyped_page.data.key?("paginate") ? "paginate" : "pagination"
  # Fill in pagination details to be handled later by Bridgetown::Paginate
  data["pagination"] = Bridgetown::Utils.deep_merge_hashes(
    prototyped_page.data[pagination_key].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) ⇒ Object

rubocop:todo Metrics/AbcSize



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 170

def process_title_data_placeholder(search_term, term) # rubocop:todo Metrics/AbcSize
  unless prototyped_page.data["prototype"]["data"] &&
      @original_title&.include?(":prototype-data-label")
    return
  end

  related_data = site.data[prototyped_page.data["prototype"]["data"]][term]
  return unless related_data

  data["data_signal"] = site.signals.send(
    :"#{prototyped_page.data["prototype"]["data"]}_signal"
  )
  data["#{search_term}_term"] = term
  data["#{search_term}_data"] = related_data
  data_label = related_data[prototyped_page.data["prototype"]["data_label"]]
  data["title"] = @original_title.gsub(
    ":prototype-data-label", data_label
  )
end

#process_title_simple_placeholders(term) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 190

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

  return unless data["title"]&.include?(":prototype-term")

  data["title"] = data["title"].gsub(
    ":prototype-term", term
  )
end

#slugify_term(term) ⇒ Object



204
205
206
207
208
209
210
211
# File 'bridgetown-core/lib/bridgetown-core/generators/prototype_generator.rb', line 204

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