Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixing transliteration in jquery.slug.js

modified patch from @pierremartin that uses map from cakephp's inflector code.
closes #180
  • Loading branch information...
commit f8452d84533d152cf700f72229572dcef0c146fa 1 parent 480426a
Rachman Chavik rchavik authored
Showing with 82 additions and 21 deletions.
  1. +82 −21 webroot/js/jquery/jquery.slug.js
103 webroot/js/jquery/jquery.slug.js
View
@@ -1,36 +1,97 @@
//
-// jQuery Slug Generation Plugin by Perry Trinier (perrytrinier@gmail.com)
-// Licensed under the GPL: http://www.gnu.org/copyleft/gpl.html
+// jQuery Slug Plugin by Perry Trinier (perrytrinier@gmail.com)
+// MIT License: http://www.opensource.org/licenses/mit-license.php
jQuery.fn.slug = function(options) {
var settings = {
- slug: 'slug', // Class used for slug destination input and span. The span is created on $(document).ready()
- hide: true // Boolean - By default the slug input field is hidden, set to false to show the input field and hide the span.
+ slug: 'slug', // Class used for slug destination input and span. The span is created on $(document).ready()
+ hide: true // Boolean - By default the slug input field is hidden, set to false to show the input field and hide the span.
};
-
+
if(options) {
jQuery.extend(settings, options);
}
-
- $this = jQuery(this);
- jQuery(document).ready( function() {
+ $this = $(this);
+
+ $(document).ready( function() {
if (settings.hide) {
- jQuery('input.' + settings.slug).after("<span class="+settings.slug+"></span>");
- jQuery('input.' + settings.slug).hide();
+ $('input.' + settings.slug).after("<span class="+settings.slug+"></span>");
+ $('input.' + settings.slug).hide();
}
});
-
- makeSlug = function() {
- var slugcontent = $this.val();
- var slugcontent_hyphens = slugcontent.replace(/\s/g,'-');
- var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');
- jQuery('input.' + settings.slug).val(finishedslug.toLowerCase());
- jQuery('span.' + settings.slug).text(finishedslug.toLowerCase());
+ var transliterate = function(str) {
+ var rExps=[
+ {re: /ä|æ|ǽ/g, ch: 'ae'},
+ {re: /ö|œ/g, ch: 'oe'},
+ {re: /ü/g, ch: 'ue'},
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ř/g, ch: 'R'},
+ {re: /ŕ|ŗ|ř/g, ch: 'r'},
+ {re: /Ś|Ŝ|Ş|Š/g, ch: 'S'},
+ {re: /ś|ŝ|ş|š|ſ/g, ch: 's'},
+ {re: /Ţ|Ť|Ŧ/g, ch: 'T'},
+ {re: /ţ|ť|ŧ/g, ch: 't'},
+ {re: /Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/g, ch: 'U'},
+ {re: /ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/g, ch: 'u'},
+ {re: /Ý|Ÿ|Ŷ/g, ch: 'Y'},
+ {re: /ý|ÿ|ŷ/g, ch: 'y'},
+ {re: /Ŵ/g, ch: 'W'},
+ {re: /ŵ/g, ch: 'w'},
+ {re: /Ź|Ż|Ž/g, ch: 'Z'},
+ {re: /ź|ż|ž/g, ch: 'z'},
+ {re: /Æ|Ǽ/g, ch: 'AE'},
+ {re: /ß/g, ch: 'ss'},
+ {re: /IJ/g, ch: 'IJ'},
+ {re: /ij/g, ch: 'ij'},
+ {re: /Œ/g, ch: 'OE'},
+ {re: /ƒ/g, ch: 'f'}
+ ];
+ for (var i=0, len=rExps.length; i<len; i++) {
+ str = str.replace(rExps[i].re, rExps[i].ch);
}
-
- jQuery(this).keyup(makeSlug);
-
+ return str;
+ }
+
+ makeSlug = function() {
+ var slug = transliterate(jQuery.trim($this.val()))
+ .replace(/\s+/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase()
+ .replace(/\-{2,}/g,'-')
+ .replace(/\-$/, '')
+ .replace(/^\-/, '')
+ ;
+ $('input.' + settings.slug).val(slug);
+ $('span.' + settings.slug).text(slug);
+ }
+
+ $(this).keyup(makeSlug);
+
return $this;
-};
+};
Please sign in to comment.
Something went wrong with that request. Please try again.