Created
October 30, 2012 21:56
-
-
Save hannesl/3983323 to your computer and use it in GitHub Desktop.
A couple of Ruby functions for transliterating file names or URLs.
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
| # 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