lib/jekyll/url.rb passing rubocop

This commit is contained in:
Derek Gottlieb 2016-05-28 17:15:33 -05:00
parent b5cec7d335
commit 03f7bc1a8c
2 changed files with 12 additions and 13 deletions

View File

@ -16,7 +16,6 @@ AllCops:
- lib/jekyll/renderer.rb - lib/jekyll/renderer.rb
- lib/jekyll/site.rb - lib/jekyll/site.rb
- lib/jekyll/static_file.rb - lib/jekyll/static_file.rb
- lib/jekyll/url.rb
- lib/jekyll/utils.rb - lib/jekyll/utils.rb
- lib/jekyll.rb - lib/jekyll.rb
- features/step_definitions.rb - features/step_definitions.rb

View File

@ -1,4 +1,4 @@
require 'uri' require "uri"
# Public: Methods that generate a URL for a resource such as a Post or a Page. # Public: Methods that generate a URL for a resource such as a Post or a Page.
# #
@ -67,10 +67,10 @@ module Jekyll
def generate_url_from_hash(template) def generate_url_from_hash(template)
@placeholders.inject(template) do |result, token| @placeholders.inject(template) do |result, token|
break result if result.index(':').nil? break result if result.index(":").nil?
if token.last.nil? if token.last.nil?
# Remove leading '/' to avoid generating urls with `//` # Remove leading "/" to avoid generating urls with `//`
result.gsub(/\/:#{token.first}/, '') result.gsub(%r!/:#{token.first}!, "")
else else
result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
end end
@ -78,21 +78,21 @@ module Jekyll
end end
def generate_url_from_drop(template) def generate_url_from_drop(template)
template.gsub(/:([a-z_]+)/.freeze) do |match| template.gsub(/:([a-z_]+)/) do |match|
replacement = @placeholders.public_send(match.sub(':'.freeze, ''.freeze)) replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
if replacement.nil? if replacement.nil?
''.freeze "".freeze
else else
self.class.escape_path(replacement) self.class.escape_path(replacement)
end end
end.gsub(/\/\//.freeze, '/'.freeze) end.gsub(%r!//!, "/".freeze)
end end
# Returns a sanitized String URL, stripping "../../" and multiples of "/", # Returns a sanitized String URL, stripping "../../" and multiples of "/",
# as well as the beginning "/" so we can enforce and ensure it. # as well as the beginning "/" so we can enforce and ensure it.
def sanitize_url(str) def sanitize_url(str)
"/" + str.gsub(/\/{2,}/, "/").gsub(/\.+\/|\A\/+/, "") "/" + str.gsub(%r!/{2,}!, "/").gsub(%r!\.+\/|\A\/+!, "")
end end
# Escapes a path to be a valid URL path segment # Escapes a path to be a valid URL path segment
@ -106,7 +106,7 @@ module Jekyll
# #
# Returns the escaped path. # Returns the escaped path.
def self.escape_path(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 "/"). # specify unsafe string (except unreserved, sub-delims, ":", "@" and "/").
# #
# URI path segment is defined in RFC 3986 as follows: # URI path segment is defined in RFC 3986 as follows:
@ -116,7 +116,7 @@ module Jekyll
# pct-encoded = "%" HEXDIG HEXDIG # pct-encoded = "%" HEXDIG HEXDIG
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
# / "*" / "+" / "," / ";" / "=" # / "*" / "+" / "," / ";" / "="
URI.escape(path, /[^a-zA-Z\d\-._~!$&'()*+,;=:@\/]/).encode('utf-8') URI.escape(path, %r{[^a-zA-Z\d\-._~!$&'()*+,;=:@\/]}).encode("utf-8")
end end
# Unescapes a URL path segment # Unescapes a URL path segment
@ -130,7 +130,7 @@ module Jekyll
# #
# Returns the unescaped path. # Returns the unescaped path.
def self.unescape_path(path) def self.unescape_path(path)
URI.unescape(path.encode('utf-8')) URI.unescape(path.encode("utf-8"))
end end
end end
end end