Replace simple regex with equivalent Ruby methods (#6736)
Merge pull request 6736
This commit is contained in:
parent
3a0d9bbc57
commit
a5c25ada1e
|
@ -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
|
|
@ -417,7 +417,7 @@ module Jekyll
|
|||
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
|
||||
|
|
|
@ -137,7 +137,7 @@ module Jekyll
|
|||
: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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -72,9 +72,9 @@ module Jekyll
|
|||
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
|
||||
|
@ -109,14 +109,14 @@ module Jekyll
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue