Skip to content

Commit

Permalink
Use frozen regular expressions for Utils#slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Jan 18, 2015
1 parent ef2d558 commit aaf0ba1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/jekyll/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module Jekyll
module Utils
extend self

# Constants for use in #slugify
SLUGIFY_MODES = %w{raw default pretty}
SLUGIFY_RAW_REGEXP = Regexp.new('\\s+').freeze
SLUGIFY_DEFAULT_REGEXP = Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze

# Merges a master hash with another hash, recursively.
#
# master_hash - the "parent" hash whose values will be overridden
Expand Down Expand Up @@ -129,19 +135,18 @@ def has_yaml_header?(file)
def slugify(string, mode=nil)
mode ||= 'default'
return nil if string.nil?
return string.downcase unless SLUGIFY_MODES.include?(mode)

# Replace each character sequence with a hyphen
re = case mode
when 'raw'
Regexp.new('\\s+')
SLUGIFY_RAW_REGEXP
when 'default'
Regexp.new('[^[:alnum:]]+')
SLUGIFY_DEFAULT_REGEXP
when 'pretty'
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
# and is allowed in both extN and NTFS.
Regexp.new("[^a-zA-Z0-9._~!$&'()+,;=@]+")
else
return string.downcase
SLUGIFY_PRETTY_REGEXP
end

string.
Expand Down

0 comments on commit aaf0ba1

Please sign in to comment.