Skip to content

Commit

Permalink
Replace simple regex with equivalent Ruby methods (jekyll#6736)
Browse files Browse the repository at this point in the history
Merge pull request 6736
  • Loading branch information
ashmaroli authored and jekyllbot committed Feb 20, 2018
1 parent 3a0d9bb commit a5c25ad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
26 changes: 26 additions & 0 deletions benchmark/sanitize-url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

require "benchmark/ips"

PATH = "/../../..../...//.....//lorem/ipsum//dolor///sit.xyz"

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

def sanitize_with_builtin
"/#{PATH}".gsub("..", "/").gsub("./", "").squeeze("/")
end

if sanitize_with_regex == sanitize_with_builtin
Benchmark.ips do |x|
x.report("sanitize w/ regexes") { sanitize_with_regex }
x.report("sanitize w/ builtin") { sanitize_with_builtin }
x.compare!
end
else
puts "w/ regexes: #{sanitize_with_regex}"
puts "w/ builtin: #{sanitize_with_builtin}"
puts ""
puts "Thank you. Do try again :("
end
2 changes: 1 addition & 1 deletion lib/jekyll/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def populate_tags
def merge_categories!(other)
if other.key?("categories") && !other["categories"].nil?
if other["categories"].is_a?(String)
other["categories"] = other["categories"].split(%r!\s+!).map(&:strip)
other["categories"] = other["categories"].split
end
other["categories"] = (data["categories"] || []) | other["categories"]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/static_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def url
:template => @collection.url_template,
:placeholders => placeholders,
})
end.to_s.gsub(%r!/$!, "")
end.to_s.chomp("/")
end

# Returns the type of the collection if present, nil otherwise.
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/theme_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Jekyll::ThemeBuilder
attr_reader :name, :path, :code_of_conduct

def initialize(theme_name, opts)
@name = theme_name.to_s.tr(" ", "_").gsub(%r!_+!, "_")
@name = theme_name.to_s.tr(" ", "_").squeeze("_")
@path = Pathname.new(File.expand_path(name, Dir.pwd))
@code_of_conduct = !!opts["code_of_conduct"]
end
Expand Down
8 changes: 4 additions & 4 deletions lib/jekyll/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def generate_url_from_hash(template)
break result if result.index(":").nil?
if token.last.nil?
# Remove leading "/" to avoid generating urls with `//`
result.gsub(%r!/:#{token.first}!, "")
result.gsub("/:#{token.first}", "")
else
result.gsub(%r!:#{token.first}!, self.class.escape_path(token.last))
result.gsub(":#{token.first}", self.class.escape_path(token.last))
end
end
end
Expand Down Expand Up @@ -109,14 +109,14 @@ def generate_url_from_drop(template)
replacement = self.class.escape_path(value)

match.sub(":#{winner}", replacement)
end.gsub(%r!//!, "/".freeze)
end.squeeze("/")
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(%r!/{2,}!, "/").gsub(%r!\.+/|\A/+!, "")
"/#{str}".gsub("..", "/").gsub("./", "").squeeze("/")
end

# Escapes a path to be a valid URL path segment
Expand Down

0 comments on commit a5c25ad

Please sign in to comment.