class Bridgetown::Resource::PermalinkProcessor
Attributes
resource[RW]
@return [Bridgetown::Resource::Base]
slugify_mode[RW]
Public Class Methods
new(resource)
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 20 def initialize(resource) @resource = resource @slugify_mode = @resource.site.config.slugify_mode end
placeholder_processors()
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 11 def self.placeholder_processors @placeholder_processors || {} end
register_placeholder(key, block)
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 15 def self.register_placeholder(key, block) @placeholder_processors ||= {} @placeholder_processors[key] = block end
Public Instance Methods
add_base_path(permalink)
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 99 def add_base_path(permalink) if resource.site.base_path.present? return "#{resource.site.base_path(strip_slash_only: true)}#{permalink}" end permalink end
final_ext()
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 25 def final_ext resource.method(:destination).arity == 1 ? resource.extname : resource.destination.final_ext end
finalize_permalink(new_url, permalink)
click to toggle source
@param new_url [String] @param permalink [String]
# File lib/bridgetown-core/resource/permalink_processor.rb, line 83 def finalize_permalink(new_url, permalink) # Handle .* style permalinks or files not ending in .html if permalink.ends_with?(".*") || !%r{\.html?$}.match?(final_ext) "/#{new_url}#{final_ext}" # If permalink includes the file extension, add it back in to the URL elsif permalink =~ %r{\.[^/]*$} "/#{new_url}#{Regexp.last_match[0]}" # Ensure index-style URLs get output correctly elsif permalink.ends_with?("/") "/#{new_url}/".sub(%r{^/index/$}, "/") # We good :) else "/#{new_url}" end end
permalink_for_permalink_style(permalink_style)
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 64 def permalink_for_permalink_style(permalink_style) collection_prefix = ("/:collection" unless resource.collection.builtin?) case permalink_style.to_sym when :pretty "#{collection_prefix}/:categories/:year/:month/:day/:slug/" when :pretty_ext, :date "#{collection_prefix}/:categories/:year/:month/:day/:slug.*" when :simple "#{collection_prefix}/:categories/:slug/" when :simple_ext "#{collection_prefix}/:categories/:slug.*" else permalink_style.to_s end end
process_segment(segment)
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 46 def process_segment(segment) segment = segment.to_sym if self.class.placeholder_processors[segment] segment_value = self.class.placeholder_processors[segment].(resource) if segment_value.is_a?(Hash) segment_value[:raw_value] elsif segment_value.is_a?(Array) segment_value.map do |subsegment| Utils.slugify(subsegment, mode: slugify_mode) end.join("/") else Utils.slugify(segment_value, mode: slugify_mode) end else segment end end
transform()
click to toggle source
# File lib/bridgetown-core/resource/permalink_processor.rb, line 29 def transform permalink = resource.data.permalink || permalink_for_permalink_style(resource.collection.default_permalink) # Strip out file extension and process each segment of a URL to swap out # placeholders such as :categories or :title url_segments = Bridgetown::Filters::URLFilters.strip_extname(permalink).split("/") new_url = url_segments.map do |segment| segment.starts_with?(":") ? process_segment(segment.sub(%r{^:}, "")) : segment end.select(&:present?).join("/") # No relative URLs should ever end in /index.html new_url.sub!(%r{/index$}, "") if final_ext == ".html" add_base_path finalize_permalink(new_url, permalink) end