Skip to content

Commit

Permalink
Merge pull request jekyll#4959 from derekgottlieb/rubocop-theme-and-url
Browse files Browse the repository at this point in the history
Merge pull request 4959
  • Loading branch information
jekyllbot committed Jun 3, 2016
2 parents 52fcd69 + db60507 commit 986eda3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ AllCops:
- lib/jekyll/renderer.rb
- lib/jekyll/site.rb
- lib/jekyll/static_file.rb
- lib/jekyll/theme.rb
- lib/jekyll/url.rb
- lib/jekyll/utils.rb
- lib/jekyll.rb
- bin/**/*
Expand Down
7 changes: 4 additions & 3 deletions lib/jekyll/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def sass_path

def configure_sass
return unless sass_path
require 'sass'
require "sass"
Sass.load_paths << sass_path
end

Expand All @@ -38,7 +38,7 @@ def path_for(folder)
return unless resolved_dir

path = Jekyll.sanitized_path(root, resolved_dir)
path if Dir.exists?(path)
path if File.directory?(path)
end

def realpath_for(folder)
Expand All @@ -50,7 +50,8 @@ def realpath_for(folder)
def gemspec
@gemspec ||= Gem::Specification.find_by_name(name)
rescue Gem::LoadError
raise Jekyll::Errors::MissingDependencyException, "The #{name} theme could not be found."
raise Jekyll::Errors::MissingDependencyException,
"The #{name} theme could not be found."
end
end
end
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 986eda3

Please sign in to comment.