User:Conrad.Irwin/format.js

/** * Header flow: * elements->spares *  h2 (spares->header) *   elements->spares *     h3 (spares->h2 section) *       elements->h3 section *  h2 LOOP *     flush (spares->h2) * * Data Storage * dom {} is an object storing DOM nodes, 'Language:Section(:head)' keyed * layout {} is an object storing the wiktionary output layout in dom{} keys *     either layout[language][etymology 0-9][0-9] *     or     layout[language][pos 0-9][0-9] *     or     layout[language][0-9][0-9] ordered by homonym *     etc. /************* DEFINITIONS ***************/ var headers = { 'pos' : ["noun","verb","pronoun","adjective","adverb","preposition","interjection","letter","symbol"], 'lords': ["etymology","pronunciation"] //Things that appear above/before real things };

var page={}; var layout={}; var oldPage;

var divideAndConquer=false;

function parseBody{ var bc = document.getElementById('bodyContent'); oldPage=bc.innerHTML; //So that when we crash we can revert var pos=0; var el; while(el=bc.childNodes[0]){ bc.removeChild(el); if(el.nodeName.toUpperCase.indexOf("H")==0){ handleHead(el); }else if(el.nodeName.toUpperCase=="HR"){ delete el; }else if(el.nodeName.toUpperCase=="#COMMENT"){ delete el; //may as well... }else if( el.nodeName.toUpperCase=="DIV"                 && el.getAttribute("id")=="catlinks"){ handleCategories(el); }else if( el.nodeName.toUpperCase=="TABLE"                && el.getAttribute("id")=="toc"){ delete el;   //and good riddance // }else if( el.nodeName.toUpperCase=="P"){ //  handleParagraph(el); //Possible special cases }else{ includeElement(el); }   } //   handleH2(false); //this is to ensure we don't leave stuff in spares[] displayBody; if(divideAndConquer){unscramblePage}; finish; }

if(wgIsArticle && wgNamespaceNumber==0 && wgAction=="view"){ addOnloadHook(parseBody); };

function displayBody{ var bc = document.getElementById('bodyContent');

var tabs = document.createElement("div"); tabs.style.margin="10px"; tabs.style.lineHeight="31px"; var firstTab=false; for(var lang in layout['']){ if(lang!=""/*&&lang.indexOf(':')==-1*/){ window.scrollTo(0,0); createLanguageTab(tabs,layout[''][lang]); if(lang==window.location.href.replace(/[^#]+#?/,"")){ firstTab=tabs.childNodes[tabs.childNodes.length-2]; }       }    }    page.nowSelected=document.createElement("div"); if(page[':Heading']){ bc.appendChild(page[':Heading']); }   if(tabs.childNodes[2]){ bc.appendChild(tabs); }   bc.appendChild(page.nowSelected); if(layout.catLinks){ bc.appendChild(layout.catLinks); }   if(tabs.childNodes[0]){ if(!firstTab)firstTab=tabs.childNodes[0] //Should try for non-language headings... tabLanguage(firstTab); } }; /********* Parser functions **********/

var page={};   //Stores the DOM Nodes var layout={}; //Stores the Heading heirarchy var pos=[];    //Holds current position var cPos=":Heading"; //Holds current position

var gotoHeader; page[cPos]=document.createElement("div");//Start off somewhere

function handleHead(hd){ var level=hd.tagName.replace(/^H/,"")-2; var span=getChildByClass(hd,"mw-headline"); if(span){ while(pos.length>level&&pos.length>0){ pos.pop; }       var oPos=pos.join(':'); if(!layout[oPos]){ layout[oPos]=[]; }       pos[level]=span.innerHTML.replace(/<[^>]*>/g,""); cPos=pos.join(':'); layout[oPos][layout[oPos].length]=pos[level]; page[cPos]=document.createElement("div"); page[cPos].setAttribute("id",cPos); page[cPos+";head"]=hd; } } function includeElement(el){ page[cPos].appendChild(el); } function finish{ // alert(layout); } //function skipElement(el){delete el};

var categories = {}; function handleCategories(div){ /*   var p = getChildByNode(div,'P'); if(p){ for(var span in p.childNodes){ /* if(p.childNodes[span].nodeName.toUpperCase=="SPAN"){ var l = getChildByNode(p.childNodes[span],'A'); if(l){ var catName = l.innerText; //Urgh! }           }        }    }*/    layout[":Categories"]=div; } /************ LANGUAGE TABS ************/ function createLanguageTab(tabs,lang){ //alert(lang); var tab=document.createElement("span"); try{tab.addEventListener('click',function(e){tabLanguage(e.target)},false)} catch(e){tab.attachEvent('onclick',function{tabLanguage(window.event.srcElement)})}; tab.style.display="inline"; tab.style.backgroundColor="#BBB"; tab.style.border="1px solid #000"; tab.style.padding="5px"; tab.style.whiteSpace="nowrap"; tab.style.marginLeft="-5px"; //tab.setAttribute("style","display:inline; background-color: #BBB;border:1px solid #000000; padding:5px;white-space: nowrap;margin-left:-5px;"); tab.appendChild(document.createTextNode(lang)); tabs.appendChild(tab); tabs.appendChild(document.createTextNode(" "));//to allow linebreaks in FF }

function tabLanguage(tab){

disinheritChildNodes(page.nowSelected); if(page[tab.innerHTML+";head"]){ page.nowSelected.appendChild(page[tab.innerHTML+";head"]); }   page.nowSelected.appendChild(page[tab.innerHTML]); recursiveAppendChild(page.nowSelected, layout[tab.innerHTML],tab.innerHTML+":"); if(ot=getChildByClass(tab.parentNode,"active_tab")){ ot.className=""; ot.style.backgroundColor="#BBB"; }   tab.className="active_tab"; tab.style.backgroundColor="#EEE"; } /************* DOM HELPERS **************/ function getChildByClass(node,clsname){ if(!node) return; for(var i=0;i<node.childNodes.length;i++){ if( node.childNodes[i].className==clsname){ return node.childNodes[i]; }   } }

function disinheritChildNodes(node){ if(!node) return; while(node.childNodes[0]){ node.removeChild(node.childNodes[0]); } }

function recursiveAppendChild(dest,lay,prefix){ for(var i in lay){ if(page[prefix+lay[i]+';head']){ dest.appendChild(page[prefix+lay[i]+';head']); }else{ dest.appendChild(document.createTextNode('Failed:'+prefix+lay[i]+';head')); }       if(page[prefix+lay[i]]){ dest.appendChild(page[prefix+lay[i]]); }else{ dest.appendChild(document.createTextNode('Failed:'+''+lay[i])); }       recursiveAppendChild(dest,layout[prefix+lay[i]],prefix+lay[i]+':'); } }

/******************* Section Handlers :( *******************/ function unscramblePage{ //EEEEKKK   for(var l=0;l]*>/g,).replace(/ +[0-9]*$/,))>=0){                parsePOS(lang+':'+layout[lang][sect]);            }else{            }        }    } } var currentState=[]; var done=false; var sense=[]; function dealWith(lang){

} function parsePOS(sect){ // alert(sect); var el = page[sect]; //Just leave inflection alone for the timebeing for(var i=0;i')>=0){        str=str.substr(0,str.indexOf(''));    }    str=str.replace(/<[^>]*>/g,' ');    str=str.replace(/[^a-z]+/g,' ');    var keys = str.split(' ');    return keys; }