adding an underscore version of slugify, for snippet slugs
Joey Butler
committed Mar 03, 2011
commit 5a80a6e31e5ea131cac3420a4143503775295f8a
Showing 2
changed files with
13 additions
and 7 deletions
app/views/cms_admin/snippets/_form.html.erb
+1
-1
| @@ | @@ -3,5 +3,5 @@ |
| <% end %> | |
| <%= form.text_field :label, :id => (@cms_snippet.new_record?? 'slugify' : nil) %> | |
| - | <%= form.text_field :slug, :id => 'slug' %> |
| + | <%= form.text_field :slug, :id => 'slug', :class => 'delimiter-underscore' %> |
| <%= form.text_area :content, :class => 'code' %> | |
| \ No newline at end of file | |
public/javascripts/comfortable_mexican_sofa/cms.js
+12
-6
| @@ | @@ -31,17 +31,23 @@ $.CMS = function(){ |
| slugify: function(){ | |
| $('input#slugify').bind('keyup.cms', function() { | |
| - | $('input#slug').val( slugify( $(this).val() ) ); |
| + | var slug_input = $('input#slug'); |
| + | var delimiter = slug_input.hasClass('delimiter-underscore') ? '_' : '-'; |
| + | slug_input.val( slugify( $(this).val(), delimiter ) ); |
| }); | |
| - | |
| - | function slugify(str){ |
| + | |
| + | function slugify(str, delimiter){ |
| + | var opposite_delimiter = (delimiter == '-') ? '_' : '-'; |
| str = str.replace(/^\s+|\s+$/g, ''); | |
| - | var from = "ÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛàáäâèéëêìíïîòóöôùúüûÑñÇç·/_,:;"; |
| - | var to = "aaaaeeeeiiiioooouuuuaaaaeeeeiiiioooouuuunncc------"; |
| + | var from = "ÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛàáäâèéëêìíïîòóöôùúüûÑñÇç"; |
| + | var to = "aaaaeeeeiiiioooouuuuaaaaeeeeiiiioooouuuunncc"; |
| for (var i=0, l=from.length ; i<l ; i++) { | |
| str = str.replace(new RegExp(from[i], "g"), to[i]); | |
| } | |
| - | str = str.replace(/[^a-zA-Z0-9 -]/g, '').replace(/\s+/g, '-').toLowerCase(); |
| + | var chars_to_replace_with_delimiter = new RegExp('[·/,:;'+ opposite_delimiter +']', 'g'); |
| + | str = str.replace(chars_to_replace_with_delimiter, delimiter); |
| + | var chars_to_remove = new RegExp('[^a-zA-Z0-9 '+ delimiter +']', 'g'); |
| + | str = str.replace(chars_to_remove, '').replace(/\s+/g, delimiter).toLowerCase(); |
| return str; | |
| } | |
| }, | |