User:Auto~enwiki/ref.js

// Experimental script for moving reference definitions to the reflist, by User:decltype. // The script borrows some HTML-specific code from DYK Check by User:Shubinator, and uses User:Ale_jrb's waLib to retrieve wikicode importScript("User:Ale_jrb/Scripts/waLib.js");

function createRefFixHeader(output) {  // makes the header above the scan results var header = document.createElement("span"); header.id = "reffix-header"; header.innerHTML = " Ref Fixer 0.1"; output.parentNode.insertBefore(header,output); } function refFix { if (document.getElementById("refFixOut")) {    return; }

var output = document.createElement("ul"); output.id = "refFixOut"; var dummy = document.getElementById("siteSub"); dummy.parentNode.insertBefore(output, dummy.nextSibling); createRefFixHeader(output); currentTitle = 0; var normalizedTitle = wgTitle.replace(/\%/g,"%25").replace(/\+/g, "%2B").replace(/\//g,"%2F"); normalizedTitle = normalizedTitle.replace(/\?/g,"%3F").replace(/\#/g,"%23").replace(/\&/g,"%26"); if (wgNamespaceNumber == 2) {   normalizedTitle = "User:" + normalizedTitle; } refScan(1, output);

} var refScanAPI = null; function refScan(step, output) { switch(step) { case 1: refScanAPI = new wa_mediawikiApi; var pageName = wgPageName.replace(/_/g," "); refScanAPI.onCompleteAction = function { refScan(2, output); } ; refScanAPI.getPage(pageName, 1, "user|content"); break; case 2: var pageWiki = refScanAPI.data["page"]["revisions"][0]["content"]; findRefs(pageWiki, output); default: } } function findRefs(wiki, output) {  var out1= document.createElement("span"); out1.id = "ref-out1"; out1.innerHTML = " ..."; var header = document.getElementById("reffix-header"); output.parentNode.insertBefore(out1, header);

// regexps, anyone? var end = 0; var n = 0;

var newRefList = "|refs=\n"; var refs; var wiki2 = wiki; for {   var begin = wiki.indexOf("", begin); // is this the ref definition? var test = wiki.indexOf("/", begin+1);

if(test != -1 && test < refTagClose) {     //alert("fake ending: " + wiki.substring(begin,end+6)); end = begin + 1; continue; }   end = wiki.indexOf(" ", begin); var refStr = wiki.substring(begin,end+6); refTagClose -= begin; // name unnamed references var nameIndex = refStr.indexOf("name"); var namedRefStr; if(nameIndex == -1 || nameIndex > refTagClose) {     namedRefStr = refStr.replace(">", " name=\"unnamedRef" + (++n) + "\">"); }   else {     namedRefStr = refStr; }    // find closing tag refStrRep = namedRefStr.substring(0,namedRefStr.indexOf(">")) + "/>";

wiki2 = wiki2.replace(refStr, refStrRep); newRefList += namedRefStr + "\n"; refs += " " + refStr.replace(//g,"&gt;") + " "; }

// add modified refsection n = wiki2.indexOf("",n); var reflist = wiki2.substring(n,n2); //alert("oldreflist:" + reflist); newRefList = reflist + newRefList; //alert("oldreflist:" + newRefList); wiki2 = wiki2.replace(reflist, newRefList); out1.innerHTML = " " + wiki2.replace(//g,"&gt;") + "  "; } addOnloadHook( function   {    if(true)     {      mw.util.addPortletLink("p-tb", "javascript:refFix", "Locate references", "ca-reffix");    }  } );