Yesterday I slept super happy, he had managed to write a role for sanitizer strings , or as they say over there: to create a SLUG (which I have not yet found the exact definition). < >
If you do not know that I speak it comes to making a “
Hello world!, So what? ” In a “ hello-world-as-it ” to the URLs beautiful all that.
He had not been easy because Ruby is wrong with the characters
non-ASCII ‘to’ and the Castilian has many, he had to do a little juggling with the gem Unicode .
In the end my role was as follows:
'unicode' def to_slug ( sentence, length = 64 ) Return if sentence. Blank ? =',' wrong [' á é',' í',' or',' ú',' ',' ä ë',' ï',' ö',' ü',' à è',' ' ,' ì',' ò',' ù',' ñ',' ç','º' ,'ª','_'] = [' right to',' and',' i',' or',' or',' ',' to e',' i',' or',' or',' to',' e ' ,' i',' or',' or',' n',' s',' or ' , 'to','-'] [ sentence sentence = 0 .. length - 1 ] sentence = Unicode. downcase ( sentence ) for i in 0 .. wrong . size - 1 sentence. gsub !( wrong [ i ], right [ i ] ) End sentence. gsub !( /[^ to - z0 -9 -]/ , '-' ) # Numbers, not letters of sentence. gsub !( /-{ 2 ,}/, '-' ) # 2 or more '-' becoming together 1'-' sentence. gsub !( /^-|-$/, '' ) unless sentence . size == 1 # '-' at begging or end at sentence end
I was super proud that I wake up in the morning and by changing the question to Google I find a … ‘inombrable’ I do this:
'unicode' def to_slug str = Unicode. normalize_KD ( self ). gsub (/[^ \ x00 - \ x7F ]/ n ,'') str = str. gsub (/ \ W / , '-'). gsub ( /^- /,''). Gsub (/- $/, ''). downcase end
Exactly (almost) what I had but 2 line.
That’s
¡ Ruby !
In the end I made some changes and got the good from one to another and I’ve added support for stopwords:
'of',' to',' that',' not',' has',' in', 'to' , 'by',' him',' the',' what',' the',' the', 'the' , 'a',' a' ] def to_slug ( length = 64 , drop_stopwords = false ) return “” if self . length == 0 str = Unicode. normalize_KD ( self ). gsub (/[^ \ x00 - \ x7F ]/ n ,”). downcase # Stopwords if drop_stopwords Stopwords. Each of | stopword | str. gsub !( / \ s # stopword) (\ s | ^ # stopword) (\ s /, ”) end end str = str. gsub (/[^ A - Za - z0 -9 ]< >/, ‘-’). Gsub (/^- /,”) . gsub (/- $/,”). downcase str = str [ 0 .. length - 1 ] end
On his post to be recommended to complete the list of stopwords with whom you want.