Class: Jekyll::URL
- Inherits:
-
Object
- Object
- Jekyll::URL
- Defined in:
- lib/jekyll/url.rb
Class Method Summary collapse
-
.escape_path(path) ⇒ Object
Escapes a path to be a valid URL path segment.
-
.unescape_path(path) ⇒ Object
Unescapes a URL path segment.
Instance Method Summary collapse
-
#generate_url(template) ⇒ Object
Internal: Generate the URL by replacing all placeholders with their respective values in the given template.
-
#generated_permalink ⇒ Object
Generates a URL from the permalink.
-
#generated_url ⇒ Object
Generates a URL from the template.
-
#initialize(options) ⇒ URL
constructor
options - One of :permalink or :template must be supplied.
-
#sanitize_url(in_url) ⇒ Object
Returns a sanitized String URL.
-
#to_s ⇒ Object
The generated relative URL of the resource.
Constructor Details
#initialize(options) ⇒ URL
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.
26 27 28 29 30 31 32 33 34 |
# File 'lib/jekyll/url.rb', line 26 def initialize() @template = [:template] @placeholders = [:placeholders] || {} @permalink = [:permalink] if (@template || @permalink).nil? raise ArgumentError, "One of :template or :permalink must be supplied." end end |
Class Method Details
.escape_path(path) ⇒ Object
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.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jekyll/url.rb', line 94 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 |
.unescape_path(path) ⇒ Object
Unescapes a URL path segment
path - The path to be unescaped.
Examples:
URL.unescape_path("/a%20b")
# => "/a b"
Returns the unescaped path.
118 119 120 |
# File 'lib/jekyll/url.rb', line 118 def self.unescape_path(path) URI.unescape(path.encode('utf-8')) end |
Instance Method Details
#generate_url(template) ⇒ Object
Internal: Generate the URL by replacing all placeholders with their respective values in the given template
Returns the unsanitizied String URL
61 62 63 64 65 66 |
# File 'lib/jekyll/url.rb', line 61 def generate_url(template) @placeholders.inject(template) do |result, token| break result if result.index(':').nil? result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) end end |
#generated_permalink ⇒ Object
Generates a URL from the permalink
Returns the _unsanitized String URL
46 47 48 |
# File 'lib/jekyll/url.rb', line 46 def generated_permalink (@generated_permlink ||= generate_url(@permalink)) if @permalink end |
#generated_url ⇒ Object
Generates a URL from the template
Returns the _unsanitized String URL
53 54 55 |
# File 'lib/jekyll/url.rb', line 53 def generated_url @generated_url ||= generate_url(@template) end |
#sanitize_url(in_url) ⇒ Object
Returns a sanitized String URL
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jekyll/url.rb', line 69 def sanitize_url(in_url) url = in_url \ # Remove all double slashes .gsub(/\/\//, '/') \ # Remove every URL segment that consists solely of dots .split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \ # Always add a leading slash .gsub(/\A([^\/])/, '/\1') # Append a trailing slash to the URL if the unsanitized URL had one url << "/" if in_url[-1].eql?('/') url end |
#to_s ⇒ Object
The generated relative URL of the resource
Returns the String URL
39 40 41 |
# File 'lib/jekyll/url.rb', line 39 def to_s sanitize_url(generated_permalink || generated_url) end |