-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.min.js
1 lines (1 loc) · 10.5 KB
/
highlight.min.js
1
"use strict";Adicat.hl={input:document.createElement("div"),values:document.createElement("div"),kbl:[0,8,13,40,39,38,37],input_history:0,texts:{current:0,comp:null,last:0},options:{values:"count",blacklist:[],live:"on",tabulate:!1,nback:5,show_zeros:"false",space_character:" ",use_dict:"default",sim_cats:["article","prep","adverb","conj","auxverb","ipron","ppron"],sim_metric:"canberra",sim_filter:"false",split:"\\n+",sep:",",meta:!0,input_encoding:"ascii",dict:{default:{colors:{article:"#82c473",prep:"#a378c0",adverb:"#616161",conj:"#9f5c61",auxverb:"#d3d280",ipron:"#6970b2",ppron:"#78c4c2"},composites:{femcomp:{show:!0,formula:"ppron + auxverb + adverb + conj - article - prep"}}}}},output:{},colors:{"#C95B76":["#C95B76","#C9AA73","#B988B5","#CA6535","#6875FF","#A97A75","#8964B5","#A56874","#BB6875","#C95837"],"#C837B4":["#C837B4","#E966A4","#C997C3","#9967D3","#C999E4","#D857B4","#9C67B4","#C93AB3","#9965B3","#8D6794"],"#BB66C0":["#BB66C0","#9765BF","#9E36BE","#9786F0","#BA6ABE","#9A6382","#AA76C2","#9E46BD","#9A36BF","#9B34C0"],"#B46FCC":["#B46FCC","#6470B8","#3340CC","#B370CF","#73409C","#446FCC","#8450CC","#6340CC","#7442FF","#9472C8"],"#47CBC2":["#47CBC2","#85E9C0","#87AAC0","#77E9C2","#77CBFF","#67A5B0","#97C8BD","#67A9F2","#9AC9D0","#59D9C0"],"#69D5A6":["#69D5A6","#AD67FF","#9D66FF","#40E866","#3AE566","#8DE966","#6FE466","#ADE36A","#6DB5A9","#3DE969"],"#DEE176":["#DEE176","#FECF65","#DECEAA","#DECE69","#CF64FF","#DECF3A","#BE9F69","#DED266","#DBEF68","#DED2A7"]},process_span:function(){if("on"===Adicat.hl.options.live){var t,e,i,a,n=window.getSelection(),l=n.getRangeAt(0),c=l.startContainer.parentNode,s=Adicat.hl.options.nback;if("BODY"===c.tagName&&(Adicat.hl.input.lastElementChild?(l.setStartBefore(Adicat.hl.input.lastElementChild),l.setEndAfter(Adicat.hl.input.lastElementChild),l.collapse(!1)):Adicat.hl.input.focus(),l=n.getRangeAt(0),c=l.startContainer.parentNode),"SPAN"===c.tagName){if(0!==c.children.length){if(t=new adicat(c.innerText).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).display(),(e=c.previousElementSibling)&&"blankspace"!==c.className){for(a=t.length;a--;)Adicat.hl.insertSpace(e),e.insertAdjacentElement("afterEnd",t[a]);l.setStart(c,0),c.parentNode.removeChild(c)}}else c.history&&c.history===c.innerText||(t=new adicat(c.innerText).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist),t.captured?(t=t.display()[0],c.className=t.className,c.title=t.title,c.history=t.innerText):(c.className=" none ",c.title="none",c.history=c.innerText));for(;s--&&c.previousElementSibling;)if(c=c.previousElementSibling,"SPAN"===c.tagName){if("blankspace"===c.className)(i=c.previousElementSibling)&&i.innerText===Adicat.hl.options.space_character&&i.parentNode.removeChild(i);else if(!c.history||c.history!==c.innerText)if(c.style&&(c.className=c.title=""),t=new adicat(c.innerText).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).display(),1!==t.length){for(a=t.length;a--;)c.insertAdjacentElement("afterEnd",t[a]),a&&Adicat.hl.insertSpace(c);c.parentNode.removeChild(c)}else c.className=t[0].className,c.title=t[0].title,c.history=t[0].innerText;Adicat.hl.options.tabulate&&Adicat.hl.update_table()}}n.removeAllRanges(),n.addRange(l)}},spanner:function(t){if("on"===Adicat.hl.options.live){var e,i,a,n,l=window.getSelection(),c=l.getRangeAt(0),s=c.startContainer.parentNode;if(c.deleteContents(),-1!==Adicat.hl.kbl.indexOf(t.which)||32===t.which&&32===Adicat.hl.input_history)return null;if(32===t.which&&0!==c.startOffset||"SPAN"!==s.tagName){if(32===t.which)if(c.startOffset!==s.innerText.length){if("SPAN"===s.tagName){for(e=new adicat(s.innerText.substring(0,c.startOffset)).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).display(),n=e[e.length-1],e=e.concat(new adicat(s.innerText.substring(c.startOffset)).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).display()),a=e.length,"blankspace"!==s.className&&Adicat.hl.insertSpace(s);a--;)s.insertAdjacentElement("afterEnd",e[a]),a&&Adicat.hl.insertSpace(s);c.setStart(n.nextElementSibling,1),"blankspace"!==s.className&&s.parentNode.removeChild(s),t.preventDefault()}}else Adicat.hl.insertSpace(s),c.setStartAfter(s.nextSibling),c.setEndAfter(s.nextSibling),c.collapse(!1),t.preventDefault(),s.history&&s.history===s.innerText||(i=new adicat(s.innerText).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist),i.captured?(i=i.display()[0],s.className=i.className,s.title=i.title,s.history=i.innerText):(s.className=" none ",s.title="none",s.history=s.innerText));else e=document.createElement("span"),e.appendChild(i=document.createTextNode(Adicat.hl.options.space_character)),c.insertNode(e),c.setStartBefore(i),c.setEndAfter(i);l.removeAllRanges(),l.addRange(c)}Adicat.hl.input_history=t.which}},display_text:function(t,e){if("string"==typeof(t=t||Adicat.hl.input.innerText)){e||(Adicat.hl.input.innerText=""),t=Adicat.filterOut(t.split(Adicat.patterns.returns),"");for(var i,a,n,l=0,c=t.length;l<c;l++)for(n=new adicat(t[l]).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).display(),a=n.length,l<c-1&&(n[a]=document.createElement("div"),n[a].appendChild(document.createElement("span")),n[a].firstElementChild.className="blankspace",n[a].firstElementChild.appendChild(document.createElement("br")),a++),i=0;i<a;i++)Adicat.hl.input.appendChild(n[i]),"SPAN"===n[i].tagName&&Adicat.hl.append(Adicat.hl.input,"span",{innerHTML:Adicat.hl.options.space_character,className:"blankspace"});!e&&Adicat.hl.options.tabulate&&Adicat.hl.update_table()}},update_table:function(){if(""===Adicat.hl.values.innerHTML){Adicat.hl.values.innerHTML="<table><tr class='highlight'></tr><tr></tr></table>";var t=Adicat.hl.values.children[0].children[0].children;Adicat.hl.append(t[0],"th",{innerText:"WC",title:"word count"}),Adicat.hl.append(t[1],"td",{innerText:0,id:"wc_value"})}var e,i,a,n,l,t=Adicat.hl.values.children[0].children[0].children,c=(t.length,Adicat.hl.input.children),s=c.length,c=Adicat.hl.input.getElementsByTagName("span"),s=c.length,r={wc:0},d=Adicat.hl.options.dict[Adicat.hl.options.use_dict].composites||{},o=document.getElementById("wc_value");o||(Adicat.hl.values.style.display="none",document.body.appendChild(Adicat.hl.values),o=document.getElementById("wc_value"));for(e in Adicat.patterns.dict)Adicat.patterns.dict.hasOwnProperty(e)&&-1===Adicat.hl.options.blacklist.indexOf(e)&&(r[e]=0);for(;s--;)if(Adicat.patterns.char.test(c[s].innerText))for(a=c[s].className.trim().split(" "),e=a.length,r.wc++;e--;)n=a[e],r.hasOwnProperty(n)&&r[n]++;for(e in r)if(r.hasOwnProperty(e)&&"wc"!==e)if("percent"===Adicat.hl.options.values&&0!==r[e]&&(r[e]=Math.round(r[e]/r.wc*1e4)/100),Adicat.hl.output[e]=r[e],i=document.getElementById(e+"_value"))i.innerText=r[e],n=r[e]||"true"===Adicat.hl.options.show_zeros?" "+e+" ":"zero",document.getElementById(e+"_title").className=n,i.className=n;else{if(t[0].children.length||(Adicat.hl.append(t[0],"th",{innerText:"WC",title:"word count"}),Adicat.hl.append(t[1],"td",{innerText:0,id:"wc_value"})),1===t[0].children.length){Adicat.hl.texts.comp>-1&&Adicat.hl.texts.hasOwnProperty(Adicat.hl.texts.comp)&&(Adicat.hl.append(t[0],"th",{innerText:"sim to "+Adicat.hl.texts.comp,title:(/^co/i.test(Adicat.hl.options.sim_metric)?"Cosine similarity":"Inverse Canberra distance")+" to text "+Adicat.hl.texts.comp+"; click to recalculate."}),Adicat.hl.texts.comp_values=new adicat(Adicat.hl.texts[Adicat.hl.texts.comp]).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).toPercent().cats,Adicat.hl.append(t[1],"td",{innerText:0,id:"similarity"}),document.getElementById("similarity").parentElement.parentElement.children[0].children[1].addEventListener("click",Adicat.hl.sim_refresh));for(i in d)d.hasOwnProperty(i)&&d[i].show&&(Adicat.hl.append(t[0],"th",{innerText:i,title:d[i].formula}),Adicat.hl.append(t[1],"td",{innerText:0,id:"comp_"+i}))}Adicat.hl.append(t[0],"th",{innerText:e,id:e+"_title",className:r[e]||"true"===Adicat.hl.options.show_zeros?" "+e+" ":"zero"}),Adicat.hl.append(t[1],"td",{innerText:r[e],id:e+"_value",className:r[e]||"true"===Adicat.hl.options.show_zeros?"":"zero"})}Adicat.hl.texts.comp>-1&&Adicat.hl.texts.comp_values&&(l=document.getElementById("similarity"))&&(l.innerText=Math.round(1e3*new adicat(Adicat.hl.input.innerText).similarity(Adicat.hl.texts.comp_values,Adicat.hl.options.sim_metric,"true"===Adicat.hl.options.sim_filter&&Adicat.hl.options.sim_cats).value)/1e3);for(i in d)d.hasOwnProperty(i)&&d[i].show&&(t=document.getElementById("comp_"+i))&&(t.innerText=Adicat.hl.solve(d[i].formula));o.innerText=r.wc},solve:function(t){function e(t){for(var t=t+"",e=t.match(r),i=e?e.length:0,a="",n=0;n<i;n++)a=e[n].replace(s,""),a&&(a=Adicat.hl.output.hasOwnProperty(a)?Number(Adicat.hl.output[a]):Number(a)),isNaN(a)&&(a=0),t=t.replace(e[n],a);return t}var i=/\s|^Math\./g,a=/[^\.0-9+><*%^()&|\/-]/g,n=/^\(/,l=/^[^(]*/,c=/\([^(]+\)/g,s=/\s+/g,r=/[^+><*^()&%|\/-]+/g,d={},o=[],p="",h=0;if(t=t.replace(Adicat.patterns.returns," "),o=t.match(c)){for(h=o.length;h--;)d[o[h]]=o[h].replace(" ",""),p=d[o[h]].replace(d[o[h]],e(d[o[h]])),t=t.replace(o[h],p);for(o=t.match(/(^|[^(+><*^&|\/-]*)\([^()]*\)/g),h=o.length;h--;)d[o[h]]=o[h].replace(i,""),n.test(d[o[h]])||(p=d[o[h]].match(l),d[o[h]]=d[o[h]].replace(l,""),Math.hasOwnProperty(p)&&(d[o[h]]=Math[p](Number(Function('"use strict"; return '+d[o[h]].replace(a,""))())),isNaN(d[o[h]])&&(d[o[h]]=0))),t=t.replace(o[h],d[o[h]])}return t=e(t),t=Math.round(1e4*Function('"use strict"; return '+t.replace(a,""))())/1e4,isFinite(t)?t:0},sim_refresh:function(){var t=document.getElementById("similarity");t&&(Adicat.hl.texts.comp_values=new adicat(Adicat.hl.texts[Adicat.hl.texts.comp]).categorize(Adicat.patterns.dict,Adicat.hl.options.blacklist).toPercent().cats,t.innerText=Math.round(1e3*new adicat(Adicat.hl.input.innerText).similarity(Adicat.hl.texts.comp_values,Adicat.hl.options.sim_metric,"true"===Adicat.hl.options.sim_filter&&Adicat.hl.options.sim_cats).value)/1e3)},select:function(t){var e,i=t.classList.contains("select"),a=Adicat.hl.input.getElementsByClassName("select");for(e=a.length;e--;)a[e].classList.remove("select");for(a=Adicat.hl.values.getElementsByClassName("select"),e=a.length;e--;)"zero"!==a[e].className&&a[e].classList.remove("select");if(!i)for(t.classList.add("select"),a=Adicat.hl.input.getElementsByClassName(t.innerText),e=a.length;e--;)""!==a[e].innerText&&a[e].classList.add("select")},insertSpace:function(t,e){var i=document.createElement("span");i.className="blankspace",i.innerHTML=Adicat.hl.options.space_character,t.insertAdjacentElement(e||"afterEnd",i)},append:function(t,e,i){var a,n=document.createElement(e);for(a in i)n[a]=i[a];t.appendChild(n)}};