Class: Bridgetown::Resource::PermalinkProcessor
- Inherits:
-
Object
- Object
- Bridgetown::Resource::PermalinkProcessor
- Defined in:
- bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb
Instance Attribute Summary collapse
-
#resource ⇒ Bridgetown::Resource::Base
-
#slugify_mode ⇒ Object
Returns the value of attribute slugify_mode.
Class Method Summary collapse
Instance Method Summary collapse
-
#ensure_base_path(permalink) ⇒ Object
-
#final_ext ⇒ Object
-
#finalize_permalink(new_url, permalink) ⇒ Object
-
#initialize(resource) ⇒ PermalinkProcessor
constructor
A new instance of PermalinkProcessor.
-
#permalink_for_permalink_style(permalink_style) ⇒ Object
-
#process_segment(segment) ⇒ Object
-
#transform ⇒ Object
Constructor Details
#initialize(resource) ⇒ PermalinkProcessor
Returns a new instance of PermalinkProcessor.
20 21 22 23 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 20 def initialize(resource) @resource = resource @slugify_mode = @resource.site.config.slugify_mode end |
Instance Attribute Details
#resource ⇒ Bridgetown::Resource::Base
7 8 9 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 7 def resource @resource end |
#slugify_mode ⇒ Object
Returns the value of attribute slugify_mode.
9 10 11 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 9 def slugify_mode @slugify_mode end |
Class Method Details
.placeholder_processors ⇒ Object
11 12 13 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 11 def self.placeholder_processors @placeholder_processors || {} end |
.register_placeholder(key, block) ⇒ Object
15 16 17 18 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 15 def self.register_placeholder(key, block) @placeholder_processors ||= {} @placeholder_processors[key] = block end |
Instance Method Details
#ensure_base_path(permalink) ⇒ Object
99 100 101 102 103 104 105 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 99 def ensure_base_path(permalink) if resource.site.base_path.present? return "#{resource.site.base_path(strip_slash_only: true)}#{permalink}" end permalink end |
#final_ext ⇒ Object
25 26 27 |
# File 'bridgetown-core/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) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'bridgetown-core/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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'bridgetown-core/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 "/:locale/#{collection_prefix}/:categories/:year/:month/:day/:slug/" when :pretty_ext, :date "/:locale/#{collection_prefix}/:categories/:year/:month/:day/:slug.*" when :simple "/:locale/#{collection_prefix}/:categories/:slug/" when :simple_ext "/:locale/#{collection_prefix}/:categories/:slug.*" else permalink_style.to_s end end |
#process_segment(segment) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb', line 45 def process_segment(segment) segment = segment.to_sym if self.class.placeholder_processors[segment] segment_value = self.class.placeholder_processors[segment].(resource) case segment_value when Hash segment_value[:raw_value] when 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 ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'bridgetown-core/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" ensure_base_path finalize_permalink(new_url, permalink) end |