Created
July 28, 2008 05:44
-
-
Save clemens/2843 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActionController::Base.logger.info "[Base] Loading config/initializers/" + File.basename(__FILE__) | |
class String | |
def to_permalink | |
text = self.dup | |
replacements = { | |
# replacement special character(s) to be replaced | |
"A" => [ "À", "Á", "Â", "Ã", "Å"], | |
"Ae" => [ "Ä", "Æ" ], | |
"C" => [ "Ç" ], | |
"D" => [ "Ð" ], | |
"E" => [ "È", "É", "Ê", "Ë" ], | |
"I" => [ "Ì", "Í", "Î", "Ï" ], | |
"N" => [ "Ñ" ], | |
"O" => [ "Ò", "Ó", "Ô", "Õ", "Ø" ], | |
"Oe" => [ "Ö" ], | |
"U" => [ "Ù", "Ú", "Û" ], | |
"Ue" => [ "Ü" ], | |
"Y" => [ "Ý" ], | |
"p" => [ "Þ"], | |
"a" => [ "à", "á", "â", "ã", "å" ], | |
"ae" => [ "ä", "æ" ], | |
"c" => [ "ç" ], | |
"d" => [ "ð" ], | |
"e" => [ "è", "é", "ê", "ë" ], | |
"i" => [ "ì", "í", "î", "ï" ], | |
"n" => [ "ñ" ], | |
"o" => [ "ò", "ó", "ô", "õ", "ø" ], | |
"oe" => [ "ö" ], | |
"ss" => [ "ß" ], | |
"u" => [ "ù", "ú", "û" ], | |
"ue" => [ "ü" ], | |
"y" => [ "ý" ] | |
} | |
replacements.each_pair do |replacement, search_chars| | |
search_chars.each do |ch| | |
text.gsub!(ch, replacement) | |
end | |
end | |
text.gsub!(/([^\-\.A-Za-z0-9_])/, "-") # replace all special chars with hyphens | |
text.gsub!(/([\-.][\-.]+)/, "-") # replace multiple hyphens with single hyphen | |
text.gsub!(/(^[\-.]|[\-.]$)/, "") # remove leading and trailing hyphen | |
text | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment