to_slug icon indicating copy to clipboard operation
to_slug copied to clipboard

unicode support

Open thesmart opened this issue 10 years ago • 0 comments

I made a version of to_slug that allows for unicode glyphs and url-safe symbols in the slug:

  def to_slug
    # become case-insensitive
    value = self.mb_chars.downcase

    # replace any spaces, separators, or connector punctuation with spaces
    value.gsub!(/[\p{Separator}\p{Dash_Punctuation}]+/, ' ')

    # replace connectors with underscore
    value.gsub!(/[\p{Connector_Punctuation}]+/, '_')

    # remove any url-unsafe character with a space
    value.gsub!(/[:?#\/\\+@=]+/, ' ')

    # ampersand == and
    value.gsub!(/(?: ?&+ ?)+/, ' and ')

    # remove any whitespace before and after the string
    value.strip!

    # replace spaces with dashes
    value.gsub!(/\s+/, '-')

    # replace unsupported characters
    value.gsub!(/[\p{Other}]/, '')
    value.gsub!(/[\p{Open_Punctuation}\p{Close_Punctuation}\p{Initial_Punctuation}\p{Final_Punctuation}\p{Other_Punctuation}]/, '')

    # done, here's the slug!
    value.to_s
  end

thesmart avatar Jul 12 '14 01:07 thesmart