User:Saintrain/bracketmatch.js

// var Bug='f-size: B:';

// modification of // User:ais523/bracketmatch.js - Colour matching brackets in a copy of the edit box. // By User:ais523, on a suggestion by User:Absidy.

addOnloadHook(function{ var p=document.getElementById('wikiPreview');  if(p==null) p=document.getElementById('viewsourcetext');  if(p) {    p.innerHTML+="ParseBrackets:1 ";    p.innerHTML+="(1:Bug="+Bug+") ";  }  try  {    var edh=document.getElementsByClassName('editHelp')[0];    if(bm_useparsebutton)      edh.innerHTML='  '+edh.innerHTML;  } catch(x) {} });

function bm_parsebrackets { var t=document.getElementById('wpTextbox1').value; var a=sajax_init_object; var p='action=expandtemplates&generatexml=1&format=json&callback=bm_callback&text='+encodeURIComponent(t); a.open('POST', mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/api.php'); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); a.setRequestHeader("Content-length", p.length); a.setRequestHeader("Connection", "close"); a.onreadystatechange=function{bm_apirespond(a)}; a.send(p); }

function bm_showparsed { bm_parsebrackets; return 0; }

function bm_apirespond(a) { try {   if(a.readyState==4) eval(a.responseText); } catch(e) {   document.getElementById('bm_parseres').innerHTML= "Could not parse due to a server error.ParseBrackets:2"; document.getElementById('bm_Bug').innerHTML="(2:Bug="+Bug+")"; } }

var colang;

function bm_hexdigit(n) { var num2hex= ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']; var i=Math.floor(n); Bug += ' num2hex:' + i; if(i<10) return i+''; if(i==10) return 'A'; if(i==11) return 'B'; if(i==12) return 'C'; if(i==13) return 'D'; if(i==14) return 'E'; if(i==15) return 'F'; /* var rtn = num2hex[i]; Bug += '->' + rtn; return rtn; /* */ }

function bm_tohex(n) { var i=Math.floor(n); var rtn = bm_hexdigit(i/16)+bm_hexdigit(i%16); Bug += ' tohex:' + rtn; return rtn; }

function bm_gencol(ang) { var r=Math.sin(ang)+1; var g=Math.sin(ang+3.14159*2/3)+1; var b=Math.sin(ang-3.14159*2/3)+1; var rtn = bm_tohex(r*127.5)+bm_tohex(g*127.5)+bm_tohex(b*127.5); Bug += ' gencol:' + rtn; return }

function bm_callback(o) { var subs={"tplarg":"", "template":"", "part":"|", "ext":"&"+"lt;", "/attr":"&"+"gt;", "attr/":"&"+"gt;"}; var h=o.parsetree["*"].split("<"); var i=h.length; var r; var n;  var sp=0; var st=new Array; var col; colang=0; while(--i) {   col=""; n=1; h[i]=h[i].split(">"); h[i][0]=h[i][0].split(" "); r=subs[h[i][0][0]]; if(r==undefined) r=""; if(r=='}}}' || r=='}}') {     st[sp]=bm_gencol(colang); col=st[sp]; sp++; colang+=2.4; //golden angle in radians, approx }   else if(r=='{{{'||r=='{{') {     sp--; col=st[sp]; n=2; }   if(col!="") r=""+r+" "; h[i][0]=r; h[i]=h[i].join(""); } document.getElementById('bm_Bug').innerHTML="(3:Bug="+Bug+")"; document.getElementById('bm_parseres').innerHTML= " "+h.join("").split("\n").join(" ")+ " ParseBrackets:3 "; }

function bm_highlight(x,c) { document.getElementById('bm_f'+x+'1').style.backgroundColor=c; document.getElementById('bm_f'+x+'2').style.backgroundColor=c; // document.getElementById('bm_f'+x+'1').style.fontWeight="bold"; // document.getElementById('bm_f'+x+'2').style.fontWeight="bold"; if(c=="black") window.setTimeout("bm_highlight('"+x+"','transparent')",3000); }

//test: {{{a|b}}} {{a|b}} {{c|{{e|a=b|3=c}}|f}} //test: f g h //test: i {{{{{{{{j}}}}}}}} /* test: {{startofline}}

//