Last active
December 17, 2015 09:19
-
-
Save ppdeassis/5586481 to your computer and use it in GitHub Desktop.
String extensions in ruby (rails initializers...)
This file contains 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
# -*- encoding : utf-8 -*- | |
module StringExtensions | |
def sanitize(options = {}) | |
Sanitize.clean(self, options) | |
end | |
def downcase_with_accent_support | |
self. | |
downcase. | |
tr("À", "à"). | |
tr("Á", "á"). | |
tr("Â", "â"). | |
tr("Ã", "ã"). | |
tr("Ä", "ä"). | |
tr("È", "è"). | |
tr("É", "é"). | |
tr("Ê", "ê"). | |
tr("Ë", "ë"). | |
tr("Ì", "ì"). | |
tr("Í", "í"). | |
tr("Î", "î"). | |
tr("Ï", "ï"). | |
tr("Ò", "ò"). | |
tr("Ó", "ó"). | |
tr("Ô", "ô"). | |
tr("Õ", "õ"). | |
tr("Ö", "ö"). | |
tr("Ù", "ù"). | |
tr("Ú", "ú"). | |
tr("Û", "û"). | |
tr("Ü", "ü"). | |
tr("Ç", "ç") | |
end | |
def titleize_with_accent_support | |
tmp = self. | |
gsub(/-/, '{{dash}}'). | |
downcase_with_accent_support. | |
titleize. | |
gsub('{{Dash}}', '-'). | |
gsub(/(^|\s)à/, "\\1À"). # à => À | |
gsub(/(^|\s)á/, "\\1Á"). # á => Á | |
gsub(/(^|\s)â/, "\\1Â"). # â => Â | |
gsub(/(^|\s)ã/, "\\1Ã"). # ã => Ã | |
gsub(/(^|\s)ä/, "\\1Ä"). # ä => Ä | |
gsub(/(^|\s)è/, "\\1È"). | |
gsub(/(^|\s)é/, "\\1É"). | |
gsub(/(^|\s)ê/, "\\1Ê"). | |
gsub(/(^|\s)ë/, "\\1Ë"). | |
gsub(/(^|\s)ì/, "\\1Ì"). | |
gsub(/(^|\s)í/, "\\1Í"). | |
gsub(/(^|\s)î/, "\\1Î"). | |
gsub(/(^|\s)ï/, "\\1Ï"). | |
gsub(/(^|\s)ò/, "\\1Ò"). | |
gsub(/(^|\s)ó/, "\\1Ó"). | |
gsub(/(^|\s)ô/, "\\1Ô"). | |
gsub(/(^|\s)õ/, "\\1Õ"). | |
gsub(/(^|\s)ö/, "\\1Ö"). | |
gsub(/(^|\s)ù/, "\\1Ù"). | |
gsub(/(^|\s)ú/, "\\1Ú"). | |
gsub(/(^|\s)û/, "\\1Û"). | |
gsub(/(^|\s)ü/, "\\1Ü"). | |
gsub(/(^|\s)ç/, "\\1Ç") | |
# tmp.gsub(/(\xC3[\xA0\xA1\xA2\xA3\xA9\xAA\xAD\xB3\xB4\xB5\xBA\xBC\xA7\x80\x81\x82\x83\x89\x8A\x8D\x93\x94\x95\x9A\x9C\x87])([A-Z])/) do | |
# $1 + $2.downcase | |
# end | |
end | |
def no_accent | |
self. | |
tr("ÀÁÂÃÄ", "A") | |
tr("ÈÉÊË", "E"). | |
tr("ÌÍÎÏ", "I"). | |
tr("ÒÓÔÕÖ", "O"). | |
tr("ÙÚÛÜ", "U"). | |
tr("Ç", "C"). | |
tr("àáâãä", "a"). | |
tr("èéêë", "e"). | |
tr("ìíîï", "i"). | |
tr("òóôõö", "o"). | |
tr("ùúûü", "u"). | |
tr("ç", "c") | |
end | |
alias :unaccent :no_accent | |
end | |
String.send(:include, StringExtensions) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment