Jump to content

User:Plastikspork/reftools.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Plastikspork (talk | contribs) at 19:31, 12 May 2009 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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(/([^>\[]|<\/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/gi ) >= 0 && search.str( /(?:{{reflist|<\/?references}})/gi ) < 0 ) {
//     if ( str.search( /=[ ]*(?:References|Notes|Footnotes)?[ ]*=[=]*/gi ) >= 0 ) {
//         str.replace( /(=[ ]*(?:References|Notes|Footnotes)?[ ]*=[=]*/gi, '$1\n{{reflist}}' );
//     } else if ( str.search( /=[ ]*Further[ ]+Readings?[ ]*=[=]*/gi ) >= 0 ) {
//         str.replace( /(=[ ]*Further Readings?[ ]*=[=]*)/i, '==References==\n{{reflist}}\n\n$1');
//     } else if ( str.search( /=[ ]*External[ ]+links?[ ]*=[=]*/gi ) >= 0 ) {
//         str.replace( /(=[ ]*External links?[ ]*=[=]*)/i, '==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;
}