Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/string_extensions.rb
Overview
RemoveAccents version 1.0.3 © 2008-2009 Solutions Informatiques Techniconseils inc.
This module adds 2 methods to the string class. Up-to-date version and documentation available at:
www.techniconseils.ca/en/scripts-remove-accents-ruby.php
This script is available under the following license : Creative Commons Attribution-Share Alike 2.5.
See full license and details at : creativecommons.org/licenses/by-sa/2.5/ca/
Version history:
* 1.0.3 : July 23 2009
Corrected some incorrect character codes. Source is now wikipedia at:
http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Related_character_maps
Thanks to Raimon Fernandez for pointing out the incorrect codes.
* 1.0.2 : October 29 2008
Slightly optimized version of urlize - Jonathan Grenier ([email protected])
* 1.0.1 : October 29 2008
First public revision - Jonathan Grenier ([email protected])
Constant Summary collapse
- ACCENTS_MAPPING =
The extended characters map used by removeaccents. The accented characters are coded here using their numerical equivalent to sidestep encoding issues. These correspond to ISO-8859-1 encoding.
{ 'E' => [200,201,202,203], 'e' => [232,233,234,235], 'A' => [192,193,194,195,196,197], 'a' => [224,225,226,227,228,229,230], 'C' => [199], 'c' => [231], 'O' => [210,211,212,213,214,216], 'o' => [242,243,244,245,246,248], 'I' => [204,205,206,207], 'i' => [236,237,238,239], 'U' => [217,218,219,220], 'u' => [249,250,251,252], 'N' => [209], 'n' => [241], 'Y' => [221], 'y' => [253,255], 'AE' => [306], 'ae' => [346], 'OE' => [188], 'oe' => [189] }
Instance Method Summary collapse
-
#removeaccents ⇒ Object
Remove the accents from the string.
-
#urlize(options = {}) ⇒ Object
Convert a string to a format suitable for a URL without ever using escaped characters.
Instance Method Details
#removeaccents ⇒ Object
Remove the accents from the string. Uses String::ACCENTS_MAPPING as the source map.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/string_extensions.rb', line 149 def removeaccents str = String.new(self) String::ACCENTS_MAPPING.each {|letter,accents| packed = accents.pack('U*') rxp = Regexp.new("[#{packed}]", nil, 'U') str.gsub!(rxp, letter) } str end |
#urlize(options = {}) ⇒ Object
Convert a string to a format suitable for a URL without ever using escaped characters. It calls strip, removeaccents, downcase (optional) then removes the spaces (optional) and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
Options
-
:downcase => call downcase on the string (defaults to true)
-
:convert_spaces => Convert space to underscore (defaults to false)
-
:regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
-
:removeaccents => If want to remove accents from String, defaults to true
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/string_extensions.rb', line 171 def urlize( = {}) [:downcase] ||= true [:convert_spaces] ||= true [:removeaccents] ||= true [:regexp] ||= /[^-_A-Za-z0-9]/ str = [:removeaccents].present? ? self.removeaccents.strip.removeaccents : self.strip str.downcase! if [:downcase] str.gsub!(/\ /,'_') if [:convert_spaces] str.gsub([:regexp], '') end |