Jump to content

User:Plastikspork/reftools.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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(/<sup[^<>]*>[;,\. ]*\[(https?:\/\/[^\[\] ]*)\][;,\. ]*<\/sup>/gi, '<ref>$1</ref>');
  str=str.replace(/(<sup[^<>]*>)[;,\. ]*\[(https?:\/\/[^\[\] ]*)\][;,\. ]*(\[)/gi, '<ref>$2</ref>$1$3');
  str=str.replace(/([^>\[]|<\/ref>)\[(https?:\/\/[^\[\] ]*)\]([^<\]]|<ref)/gi, '$1<ref>$2</ref>$3');
  str=str.replace(/([^>\[]|<\/ref>)\[(https?:\/\/[^\[\] ]*)\]([^<\]]|<ref)/gi, '$1<ref>$2</ref>$3');
  str=str.replace(/(<ref[^<>]*>)([\[]+)(https?:\/\/[^\[\] ]*)\2(<\/ref>)/gi, '$1$3$4');

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

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

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

  return str;
}