Skip to content

Instantly share code, notes, and snippets.

@janv
Forked from clemens/gist:2843
Created September 10, 2009 14:44
Show Gist options
  • Save janv/184586 to your computer and use it in GitHub Desktop.
Save janv/184586 to your computer and use it in GitHub Desktop.
Permalinks mit Umlauten
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