User:Plastikspork/reftools.js

function spork_bracket_url_to_ref(str) {

var loopcount = 0;

// while ( str.search( /(==[\t ]*External[\t ]+Links[\t ]*=[=]+(?:[\r\n]|[^=])*[\r\n][\*\t ]*)\[(https?:\/\/[^\[\]\n ]*)\]/gim ) >= 0 && loopcount <= 10 ) { //   str=str.replace(/(==[\t ]*External[\t ]+Links[\t ]*=[=]+(?:[\r\n]|[^=])*[\r\n][\*\t ]*)\[(https?:\/\/[^\[\]\n ]*)\]/gim, '$1$2'); //   loopcount++; // }

// loopcount = 0; // while ( str.search( /(==[\t ]*External[\t ]+Links[\t ]*=[=]+(?:[\r\n]|[^=])*[\r\n][\*\t ]*)([\w][^\r\n=]*?)[\t ]*\[(https?:\/\/[^\[\]\n ]*)\]/gim ) >= 0 && loopcount <= 10 ) { //   str=str.replace(/(==[\t ]*External[\t ]+Links[\t ]*=[=]+(?:[\r\n]|[^=])*[\r\n][\*\t ]*)([\w][^\r\n=]*?)[\t ]*\[(https?:\/\/[^\[\]\n ]*)\]/gim, '$1[$3 $2]'); //   loopcount++; // }  str=str.replace(/]*>[;,\. ]*\[(https?:\/\/[^\[\] ]*)\][;,\. ]*<\/sup>/gi, ' '); str=str.replace(/(]*>)[;,\. ]*\[(https?:\/\/[^\[\] ]*)\][;,\. ]*(\[)/gi, ' $1$3'); str=str.replace(/([^>\[]|<\/ref>)\[(https?:\/\/[^\[\] ]*)\]([^<\]]|\[]|<\/ref>)\[(https?:\/\/[^\[\] ]*)\]([^<\]]|]*>)([\[]+)(https?:\/\/[^\[\] ]*)\2(<\/ref>)/gi, '$1$3$4');

// if ( str.search( /= 0 && str.search( /(?:\{\{reflist|<\/?references)/im ) < 0 ) { //    if ( str.search( /=[ ]*(?:Reference|Note|Footnote)s?[ ]*=[=]*/im ) >= 0 ) { //        str.replace( /(=[ ]*(?:Reference|Note|Footnote)s?[ ]*=[=]*)/im, '$1\n{'+'{reflist}}' ); //    }  else if ( str.search( /=[ ]*Further[ ]+Readings?[ ]*=[=]*/im ) >= 0 ) { //        str.replace( /(=[ ]*Further Readings?[ ]*=[=]*)/im, '==References==\n{'+'{reflist}}\n\n$1'); //    } else if ( str.search( /=[ ]*External[ ]+links?[ ]*=[=]*/im ) >= 0 ) { //        str.replace( /(=[ ]*External links?[ ]*=[=]*)/im, '==References==\n{'+'{reflist}}\n\n$1'); //    } else if ( str.search( /(?:\{\{DefaultSort|\[\[Category)/im ) >= 0 ) { //        str.replace( /(\{\{DefaultSort|\[\[Category)/im, '==References==\n{'+'{reflist}}\n\n$1' ); //    } else { //        str = str + '\n\n==References==\n{'+'{reflist}}'; //    } //  }

return str; }

function spork_ref_clean(str) { var loopcount = 0;

// Clean citation templates (multiple times) loopcount = 0; while( str.search( /\{\{(?:cite|citation) (?:[\r\n]|[^}])*\|[\r\n ]*[a-z][a-z0-9_]*[ ]*=[ \r\n]*(?:}}|\|)/gim ) >= 0 && loopcount <= 10 ) { str=str.replace(/(\{\{(?:cite|citation) (?:[\r\n]|[^}])*)\|[\r\n ]*[a-z][a-z0-9_]*[ ]*=[ \r\n]*(}}|\|)/gim, '$1$2'); loopcount++; }

// Change blah // to    blah (multiple times) loopcount = 0; while( str.search( /( blah  // to     blah (multiple times)  loopcount = 0;  while( str.search( /   // to     blah  (multiple times)  loopcount = 0;  while( str.search( /([^<>]*<\/ref>)(?:.|[\r\n])*\/]*>[ ]*\1/gim ) >= 0 && loopcount <= 10 ) {    str=str.replace(/[^<>]*<\/ref>)((?:.|[\r\n])*)(\/\[\]\|]*)\1/gim, '$3 />$2$3$1');  loopcount++;  }

return str; }