Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created October 30, 2012 21:56
Show Gist options
  • Select an option

  • Save hannesl/3983323 to your computer and use it in GitHub Desktop.

Select an option

Save hannesl/3983323 to your computer and use it in GitHub Desktop.
A couple of Ruby functions for transliterating file names or URLs.
# Remove whitespace and special characters and replace with dashes (-).
def self.filename(input)
input.split(%r{ |!|/|:|&|-|$|,|\?|%|\(|\)}).map { |i| i.downcase if i != '' }.compact.join('-')
end
# Convert to ascii and replace non-ascii letters.
def self.transliterate(input)
replacements = {
'å' => 'a',
'ä' => 'a',
'â' => 'a',
'á' => 'a',
'à' => 'a',
'ç' => 'c',
'é' => 'e',
'è' => 'e',
'ê' => 'e',
'ë' => 'e',
'ï' => 'i',
'í' => 'i',
'ì' => 'i',
'ö' => 'o',
'ó' => 'o',
'ò' => 'o',
'ô' => 'o',
'ü' => 'u',
'æ' => 'ae',
'œ' => 'oe',
'ß' => 'ss',
}
input.encode Encoding::ASCII, :fallback => replacements
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment