class Jekyll::URL
Public Class Methods
escape_path(path)
click to toggle source
Escapes a path to be a valid URL path segment
path - The path to be escaped.
Examples:
URL.escape_path("/a b") # => "/a%20b"
Returns the escaped path.
# File lib/jekyll/url.rb, line 108 def self.escape_path(path) # Because URI.escape doesn't escape '?', '[' and ']' by default, # specify unsafe string (except unreserved, sub-delims, ":", "@" and "/"). # # URI path segment is defined in RFC 3986 as follows: # segment = *pchar # pchar = unreserved / pct-encoded / sub-delims / ":" / "@" # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" # pct-encoded = "%" HEXDIG HEXDIG # sub-delims = "!" / "$" / "&" / "'" / "(" / ")" # / "*" / "+" / "," / ";" / "=" URI.escape(path, /[^a-zA-Z\d\-._~!$&'()*+,;=:@\/]/).encode('utf-8') end
new(options)
click to toggle source
options - One of :permalink or :template must be supplied.
:template - The String used as template for URL generation, for example "/:path/:basename:output_ext", where a placeholder is prefixed with a colon. :placeholders - A hash containing the placeholders which will be replaced when used inside the template. E.g. { "year" => Time.now.strftime("%Y") } would replace the placeholder ":year" with the current year. :permalink - If supplied, no URL will be generated from the template. Instead, the given permalink will be used as URL.
# File lib/jekyll/url.rb, line 25 def initialize(options) @template = options[:template] @placeholders = options[:placeholders] || {} @permalink = options[:permalink] if (@template || @permalink).nil? raise ArgumentError, "One of :template or :permalink must be supplied." end end
unescape_path(path)
click to toggle source
Unescapes a URL path segment
path - The path to be unescaped.
Examples:
URL.unescape_path("/a%20b") # => "/a b"
Returns the unescaped path.
# File lib/jekyll/url.rb, line 132 def self.unescape_path(path) URI.unescape(path.encode('utf-8')) end
Public Instance Methods
generate_url(template)
click to toggle source
Internal: Generate the URL by replacing all placeholders with their respective values in the given template
Returns the unsanitized String URL
# File lib/jekyll/url.rb, line 60 def generate_url(template) if @placeholders.is_a? Drops::UrlDrop generate_url_from_drop(template) else generate_url_from_hash(template) end end
generate_url_from_drop(template)
click to toggle source
# File lib/jekyll/url.rb, line 80 def generate_url_from_drop(template) template.gsub(/:([a-z_]+)/.freeze) do |match| replacement = @placeholders.public_send(match.sub(':'.freeze, ''.freeze)) if replacement.nil? ''.freeze else self.class.escape_path(replacement) end end.gsub(/\/\//.freeze, '/'.freeze) end
generate_url_from_hash(template)
click to toggle source
# File lib/jekyll/url.rb, line 68 def generate_url_from_hash(template) @placeholders.inject(template) do |result, token| break result if result.index(':').nil? if token.last.nil? # Remove leading '/' to avoid generating urls with `//` result.gsub(/\/:#{token.first}/, '') else result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) end end end
generated_permalink()
click to toggle source
generated_url()
click to toggle source
sanitize_url(str)
click to toggle source
Returns a sanitized String URL, stripping “../../” and multiples of “/”, as well as the beginning “/” so we can enforce and ensure it.
# File lib/jekyll/url.rb, line 94 def sanitize_url(str) "/" + str.gsub(/\/{2,}/, "/").gsub(/\.+\/|\A\/+/, "") end