Skip to content

Commit

Permalink
Use index instead of while and array
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Jul 19, 2017
1 parent 20a333f commit 8f52910
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/jekyll/commands/serve/servlet.rb
Expand Up @@ -25,7 +25,6 @@ def search_file(req, res, basename)
super || super(req, res, "#{basename}.html")
end

# rubocop:disable Lint/AssignmentInCondition
def search_index_file(req, res)
# /file/index.html -> /file.html

Expand All @@ -44,15 +43,13 @@ def search_index_file(req, res)
# so we'll turn res.filename into an array of path elements then pop off the
# basename.
#
# We use a while loop just in case res.filename has trailing slashes.
#
# Once we have popped off the basename, we can join up what's left and use it
# as the new res.filename.
path_arr = res.filename.scan(%r!/[^/]*!)
while basename = path_arr.pop
break unless basename == "/"
end
res.filename = path_arr.join
#
# Index of final / that isn't followed by another / or EOL
index = res.filename.rindex %r!/(?!/|$)!
basename = res.filename[index..-1]
res.filename = res.filename[0, index]

# Try and find a file named dirname.html in the parent directory.
file = search_file(req, res, basename + ".html")
Expand All @@ -63,7 +60,6 @@ def search_index_file(req, res)

return file
end
# rubocop:enable Lint/AssignmentInCondition

# rubocop:disable Style/MethodName
def do_GET(req, res)
Expand Down

0 comments on commit 8f52910

Please sign in to comment.