User:PC-XT/JSubst.js

(function(){var P={list:[]},f=0,

set=function(p,v){if(typeof p=="undefined")return;if(typeof p=="object")for(var i in p)set1(i,p[i]);else set1(p,v);},

set1=function(p,v){if(p=="list"){P.list=[];return addList(v);}if(p=="addList")return addList(v);P[p]=v;},

addList=function(a){var p=typeof a=="object"?a:[a],i;for(i in p)if((typeof p[i]=="object"?p[i]:p[i]=[p[i]]).length>1)P.list[P.list.length]=p[i];/*else if(Template[p[i][0]])addList(Template[p[i][0]]);/*else importScript(p[i][0].match(/^user\:.*\.js$/i)?p[i][0]:"User:PC-XT/JSubst/"+p[i][0]+".js");*/},

subst=function(x){set(x);var box=document.getElementById("wpTextbox1"),text=box.value,T,re,st,nd,np,p,pn,t2,t3,t,ttt,template=P.onlyList?p.onlyList:P.list

,prefix=P.prefix?P.prefix:"([Jj]?[Ss][Uu][Bb][Ss][Tt])"

,pnam=function(){var t,n;

if(p[pn].length&&(t=p[pn].search(/\W*$/))>0)p[pn]=p[pn].substring(0,t);

if((t=p[pn].indexOf("="))>=0){

p[n=p[pn].substring(0,p[pn].search(/\W*=/))]=p[pn].substring(t+1);

if(p[n].length&&(t=p[n].search(/[^\W]/))>0)p[n]=p[n].substring(t);

if(n==pn)pn++;else p[pn]="";

}else p[++pn]="";

};

for(T=0;T

t=template[T][0].substring(0,1);

re=new RegExp("(\\{\\{"+(P.requirePrefix?prefix+"\\:":"("+prefix+"\\:)?")+")["+t.toUpperCase()+t.toLowerCase()+"]"+template[T][0].substring(1)+"\\W*[|}]","g");

while((st=text.search(re))>=0){

np=st+2;p=[""];pn=t2=t3=0;

nd=5000+(new Date());

while(1)if(nd>(new Date())){if(P.alert)alert("User:PC-XT/JSubst Script timeout error");return false;}else if(np>=text.length){if(P.alert)alert("{{JSubst:"+template[T][0]+"|... isn't closed. This template starts at position "+st+".");return false;}

else if((ttt=text.substring(np,np+3))=="{{{"){t3++;np+=3;}

else if((t=ttt.substring(0,2))=="{{"){t2++;np+=2;}

else if(t=="}}"){

np+=2;

if(t3>0&&ttt=="}}}"){t3--;np++;}else if(t2>0)t2--;else break;

}else if(t2<1&&t3<1&&ttt.substring(0,1)=="|"){

pnam();

np++;if(!text.substring(np,np+2).search(/^\W/))if((t=text.substring(np).search(/[^\W]/))>=0)np+=t;else np=text.length;

}else p[pn]+=text.substring(np,++np);

pnam();

text=text.substring(0,st)+template[T][1](function(k,d){return typeof p[k]!="undefined"?p[k]:d?d:"{{{"+k+"}}}";})+text.substring(np);}}box.value=text;},

auto=function(p){set(p);var hook=function(){var ef;if(ef=document.getElementById("editform")){f=ef.onsubmit;ef.onsubmit=function(e1,e2,e3){subst();if(f)f(e1,e2,e3);};}};if(document.getElementById("editform"))hook();else addOnloadHook(hook);};

JSubst={auto:auto,set:set,subst:subst};}(/*linkback: User:PC-XT/JSubst*/))