UNPKG

hypher-for-jquery

Version:

A fast and small JavaScript hyphenation engine for jQuery

7 lines 6.96 kB
/*! * hypher-for-jquery * Version: 1.0.3 * A fast and small JavaScript hyphenation engine for jQuery * Author: Bram Stein & Marc Wieland */ !function(a){function b(c,d,e,f){var g={lang:f,leftMin:!1,rightMin:!1,minLength:4,path:"../patterns/",autoload:!0,hypenChar:!1,exceptions:null,devMode:!1};this.options=a.extend({},g,d),this.options.lang=this.options.lang.toLowerCase(),this.langCode={be:["be","be_by","be-by","bel"],bn:["bn","bn_bd","bn-bd"],ca:["ca"],cs:["cs","cs_cz","cs-cz"],da:["da","da_dk","da-dk"],de:["de","de_de","de-de","de_ch","de-ch","de_at","de-at"],"el-monoton":["el","el-monoton"],"el-polyton":["el-polyton"],"en-gb":["en_gb","en-gb"],"en-us":["en","en_us","en-us"],eo:["eo"],es:["es","es_es","es-es"],et:["et","et_ee","et-ee"],fi:["fi","fi_fi","fi-fi"],fr:["fr","fr_fr","fr-fr","fr_be","fr-be"],grc:["grc"],gu:["gu","gu_in","gu-in"],hi:["hi","hi_in","hi-in"],hu:["hu","hu_hu","hu-hu"],hy:["hy"],it:["it","it_it","it-it"],kn:["kn"],la:["la"],lt:["lt","lt_lt","lt-lt"],lv:["lv"],ml:["ml","ml_in","ml-in"],"nb-no":["nb","no","nb_no","nb-no","nn_no","nn-no"],nl:["nl","nl_nl","nl-nl","nl_be","nl-be"],or:["or","ory"],pa:["pa","pa_in","pa-in"],pl:["pl","pl_pl","pl-pl"],pt:["pt","pt_pt","pt-pt","pt_br","pt-br"],ro:["ro","ro_ro","ro-ro"],ru:["ru","ru_ru","ru-ru"],sk:["sk","sk_sk","sk-sk"],sl:["sl","sl_si","sl-si"],"sr-cyrl":["sr-cyrl"],"sr-latn":["sr-latn","sr","sr_rs","sr-rs"],sv:["sv","sv_se","sv-se"],ta:["ta","ta_in","ta-in","ta_lk","ta-lk"],te:["te"],tr:["tr","tr_tr","tr-tr"],uk:["uk","uk_uk","uk-uk"]},b.prototype.include=function(a,b){return-1!=a.indexOf(b)};for(var h in this.langCode)if(this.langCode.hasOwnProperty(h)&&this.include(this.langCode[h],this.options.lang)){this.options.lang=h;break}if("/"===this.options.path.substr(-1)&&(this.options.path=this.options.path.substr(0,this.options.path.length-1)),""!==this.options.path&&(this.options.path=this.options.path+"/"),this.options.autoload?a.ajax({url:this.options.path+this.options.lang+".js",async:!1,dataType:"script",cache:!0,error:function(a,b,c){console.log("Pattern file can't be loaded - check path ("+this.options.path+")")}}):"undefined"==typeof Hyphenator&&console.log("Pattern isn't included - autoload is turned off"),"undefined"==typeof Hyphenator&&console.log("Pattern isn't included - maybe pattern file doesn't exists or path linked to the wrong directory - check path ("+this.options.path+")"),i=0,this.options.leftMin||(this.options.leftMin=Hyphenator.languages[this.options.lang].leftmin),this.options.rightMin||(this.options.rightMin=Hyphenator.languages[this.options.lang].rightmin),this.options.exceptions||(this.options.exceptions=Hyphenator.languages[this.options.lang].exceptions),this.exceptionsObject={},this.options.exceptions)for(exceptions=this.options.exceptions.split(/,\s?/g);i<exceptions.length;i+=1)this.exceptionsObject[exceptions[i].replace(/\|/g,"").toLowerCase()]=new RegExp("("+exceptions[i].split("|").join(")(")+")","i");this.trie=this.createTrie(Hyphenator.languages[this.options.lang].patterns),this.hypens=this.hyphenateText(e),this.globalSettings=this.options}var c="Hypher for jQuery",d="1.0.3";b.prototype.createTrie=function(a){var b,c=0,d=0,e=0,f=0,g=null,h=null,i=null,j=null,k={_points:[]};for(c in a)if(a.hasOwnProperty(c))for(b=a[c].match(new RegExp(".{1,"+ +c+"}","g")),d=0;d<b.length;d+=1){for(g=b[d].replace(/[0-9]/g,"").split(""),h=b[d].split(/\D/),j=k,e=0;e<g.length;e+=1)i=g[e].charCodeAt(0),j[i]||(j[i]={}),j=j[i];for(j._points=[],f=0;f<h.length;f+=1)j._points[f]=h[f]||0}return k},b.prototype.hyphenateText=function(a){for(var b=a.split(/([a-zA-Z0-9_\u0027\u00AD\u00DF-\u00EA\u00EB\u00EC-\u00EF\u00F1-\u00F6\u00F8-\u00FD\u0101\u0103\u0105\u0107\u0109\u010D\u010F\u0111\u0113\u0117\u0119\u011B\u011D\u011F\u0123\u0125\u012B\u012F\u0131\u0135\u0137\u013C\u013E\u0142\u0144\u0146\u0148\u0151\u0153\u0155\u0159\u015B\u015D\u015F\u0161\u0165\u016B\u016D\u016F\u0171\u0173\u017A\u017C\u017E\u017F\u0219\u021B\u02BC\u0390\u03AC-\u03CE\u03F2\u0401\u0410-\u044F\u0451\u0454\u0456\u0457\u045E\u0491\u0531-\u0556\u0561-\u0587\u0902\u0903\u0905-\u090B\u090E-\u0910\u0912\u0914-\u0928\u092A-\u0939\u093E-\u0943\u0946-\u0948\u094A-\u094D\u0982\u0983\u0985-\u098B\u098F\u0990\u0994-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BE-\u09C3\u09C7\u09C8\u09CB-\u09CD\u09D7\u0A02\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A14-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A82\u0A83\u0A85-\u0A8B\u0A8F\u0A90\u0A94-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABE-\u0AC3\u0AC7\u0AC8\u0ACB-\u0ACD\u0B02\u0B03\u0B05-\u0B0B\u0B0F\u0B10\u0B14-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3E-\u0B43\u0B47\u0B48\u0B4B-\u0B4D\u0B57\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB5\u0BB7-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C02\u0C03\u0C05-\u0C0B\u0C0E-\u0C10\u0C12\u0C14-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3E-\u0C43\u0C46-\u0C48\u0C4A-\u0C4D\u0C82\u0C83\u0C85-\u0C8B\u0C8E-\u0C90\u0C92\u0C94-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBE-\u0CC3\u0CC6-\u0CC8\u0CCA-\u0CCD\u0D02\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D3E-\u0D43\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D60\u0D61\u0D7A-\u0D7F\u1F00-\u1F07\u1F10-\u1F15\u1F20-\u1F27\u1F30-\u1F37\u1F40-\u1F45\u1F50-\u1F57\u1F60-\u1F67\u1F70-\u1F7D\u1F80-\u1F87\u1F90-\u1F97\u1FA0-\u1FA7\u1FB2-\u1FB4\u1FB6\u1FB7\u1FBD\u1FBF\u1FC2-\u1FC4\u1FC6\u1FC7\u1FD2\u1FD3\u1FD6\u1FD7\u1FE2-\u1FE7\u1FF2-\u1FF4\u1FF6\u1FF7\u200D\u2019]+)/gi),c=0;c<b.length;c+=1)-1!==b[c].indexOf("/")?0===c||c===b.length-1||/\s+\/|\/\s+/.test(b[c])||(b[c]+="​"):b[c].length>this.options.minLength&&(this.options.hypenChar?"string"==typeof this.options.hypenChar?b[c]=this.hyphenate(b[c]).join(this.options.hypenChar):b[c]=this.hyphenate(b[c]).join("|"):b[c]=this.hyphenate(b[c]).join("­"));return b.join("")},b.prototype.hyphenate=function(a){var b,c,d,e,f,g,h,i,j,k=[],l=[],m=a.toLowerCase(),n=Math.max,o=this.trie,p=[""];if(this.exceptionsObject.hasOwnProperty(m))return a.match(this.exceptionsObject[m]).slice(1);if(-1!==a.indexOf("­"))return[a];for(a="_"+a+"_",b=a.toLowerCase().split(""),c=a.split(""),h=b.length,d=0;h>d;d+=1)l[d]=0,k[d]=b[d].charCodeAt(0);for(d=0;h>d;d+=1)for(g=o,e=d;h>e&&(g=g[k[e]],g);e+=1)if(i=g._points)for(f=0,j=i.length;j>f;f+=1)l[d+f]=n(l[d+f],i[f]);for(d=1;h-1>d;d+=1)d>this.options.leftMin&&d<h-this.options.rightMin&&l[d]%2?p.push(c[d]):p[p.length-1]+=c[d];return p},a.fn.hyphenate=function(e){void 0===e&&(e=""),void 0!==e.lang?language=e.lang:language=document.getElementsByTagName("html")[0].getAttribute("lang");var f=this.length;return this.each(function(g){for(var h=0,i=this.childNodes.length;i>h;h+=1)if(3===this.childNodes[h].nodeType){var j=new b(a(this),e,this.childNodes[h].nodeValue,language);this.childNodes[h].nodeValue=j.hypens,g==f-1&&j.globalSettings.devMode&&(console.log(c+" - "+d),console.log(j.globalSettings))}})}}($);