Skip to content

Commit

Permalink
lib/jekyll/url.rb passing rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
derekgottlieb committed May 28, 2016
1 parent b5cec7d commit 03f7bc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ AllCops:
- lib/jekyll/renderer.rb
- lib/jekyll/site.rb
- lib/jekyll/static_file.rb
- lib/jekyll/url.rb
- lib/jekyll/utils.rb
- lib/jekyll.rb
- features/step_definitions.rb
Expand Down
24 changes: 12 additions & 12 deletions lib/jekyll/url.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'uri'
require "uri"

# Public: Methods that generate a URL for a resource such as a Post or a Page.
#
Expand Down Expand Up @@ -67,32 +67,32 @@ def generate_url(template)

def generate_url_from_hash(template)
@placeholders.inject(template) do |result, token|
break result if result.index(':').nil?
break result if result.index(":").nil?
if token.last.nil?
# Remove leading '/' to avoid generating urls with `//`
result.gsub(/\/:#{token.first}/, '')
# Remove leading "/" to avoid generating urls with `//`
result.gsub(%r!/:#{token.first}!, "")
else
result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
end
end
end

def generate_url_from_drop(template)
template.gsub(/:([a-z_]+)/.freeze) do |match|
replacement = @placeholders.public_send(match.sub(':'.freeze, ''.freeze))
template.gsub(/:([a-z_]+)/) do |match|
replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
if replacement.nil?
''.freeze
"".freeze
else
self.class.escape_path(replacement)
end
end.gsub(/\/\//.freeze, '/'.freeze)
end.gsub(%r!//!, "/".freeze)
end

# Returns a sanitized String URL, stripping "../../" and multiples of "/",
# as well as the beginning "/" so we can enforce and ensure it.

def sanitize_url(str)
"/" + str.gsub(/\/{2,}/, "/").gsub(/\.+\/|\A\/+/, "")
"/" + str.gsub(%r!/{2,}!, "/").gsub(%r!\.+\/|\A\/+!, "")
end

# Escapes a path to be a valid URL path segment
Expand All @@ -106,7 +106,7 @@ def sanitize_url(str)
#
# Returns the escaped path.
def self.escape_path(path)
# Because URI.escape doesn't escape '?', '[' and ']' by default,
# 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:
Expand All @@ -116,7 +116,7 @@ def self.escape_path(path)
# pct-encoded = "%" HEXDIG HEXDIG
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
# / "*" / "+" / "," / ";" / "="
URI.escape(path, /[^a-zA-Z\d\-._~!$&'()*+,;=:@\/]/).encode('utf-8')
URI.escape(path, %r{[^a-zA-Z\d\-._~!$&'()*+,;=:@\/]}).encode("utf-8")
end

# Unescapes a URL path segment
Expand All @@ -130,7 +130,7 @@ def self.escape_path(path)
#
# Returns the unescaped path.
def self.unescape_path(path)
URI.unescape(path.encode('utf-8'))
URI.unescape(path.encode("utf-8"))
end
end
end

0 comments on commit 03f7bc1

Please sign in to comment.