User:Plastikspork/citetools.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.
// --------------------------- Cite Template  Functions --------------------------- //
// <nowiki>
// Convert some references to cite web templates
function spork_cite_web(str){
  // MONTH DD, YYYY or MONTH YYYY
  var smdy = "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?:\.|[a-z]*)[,\. ]+(?:[0-9]{1,2}[,\. ]+|)[1-9][0-9]{3}";
  // DD MONTH YYYY
  var sdmy = "[0-9]{1,2}[,\. ]+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?:\.|[a-z]*)[,\. ]+[1-9][0-9]{3}";
  // YYYY-MM-DD
  var symd = "[1-9][0-9]{3}-[0-9]{1,2}-[0-9]{1,2}";
  // Matches any date
  var matchdate = "\\(?(" + smdy + "|" + sdmy + "|" + symd + ")\\.?\\)?";
  // Matchs [URL TITLE]
  var ut = "\\[(https?:[^\\| ]*)(?: | \\|[ ]*)([^<>\\[\\]\\|]*)\\]";
  // Matches whitespace between fields
  var ws = "[:;\.,— ]+";
  // Matches an author's name
  var au = "([A-Z][a-zî ]+(?:[, ]+|[ ]*[A-Z]\.[ ]*)[A-Z][a-z]+(?:[ ]*[A-Z]\.[ ]*))";
  // Matches publisher string
  var pub = "[- ]*(?:from )*[ -]*[\"']*([^<>][^<>]*?)[\"']*";
  // Unified formatting of accessdate string
  var s = new RegExp("(<ref[^<>]*>[^<>]*) (?:URL last accessed|URL accessed|last accessed|last retrieved)[ ]*(?: on|,|)[ ]*" + matchdate, "gi");
  str=str.replace(s, "$1 Retrieved on $2");
  s.compile("(<ref[^<>]*>[^<>]*) (?:accessed|retrieved)[ ]*(?: on|,|)[ ]*" + matchdate, "gi");
  str=str.replace(s, "$1 Retrieved on $2");
  // Match <ref>NAME [URL TITLE] PUBLISHER DATE Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*" + au + "[ ]*[\"']*[ ]*" + ut + "[\"']*" + ws + pub + ws + matchdate + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|author= $2|url= $3|title= $4|publisher= $5|date= $6|accessdate= $7}}$8");
// Match <ref>NAME DATE [URL TITLE] PUBLISHER Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*" + au + ws + matchdate + ws + "[\"']*[ ]*" + ut + "[\"']*" + ws + pub + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|author= $2|date= $3|url= $4|title= $5|publisher= $6|accessdate= $7}}$8");
// Match <ref>[URL TITLE] DATE PUBLISHER Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*[\"']*[ ]*" + ut + "[\"']*" + ws + matchdate + ws + pub + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|date = $4|publisher= $5|accessdate= $6}}$7");
// Match <ref>DATE [URL TITLE] PUBLISHER Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*" + matchdate + ws + "[\"']*[ ]*" + ut + "[\"']*" + ws + pub + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|date= $2|url= $3|title= $4|publisher= $5|accessdate= $6}}$7");
  // Match <ref>[URL TITLE] PUBLISHER DATE Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*[\"']*[ ]*" + ut + "[\"']*" + ws + pub + ws + matchdate + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|publisher= $4|date= $5|accessdate= $6}}$7");
  // Match <ref>NAME [URL TITLE] DATE Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*" + au + "[ ]*[\"']*[ ]*" + ut + "[\"']*" + ws + matchdate + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|author= $2|url= $3|title= $4|date= $5|accessdate= $6}}$7");
  // Match <ref>[URL TITLE] DATE Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*[\"']*[ ]*" + ut + "[\"']*" + ws + matchdate + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|date= $4|accessdate= $5}}$6");
  // Match <ref>[URL TITLE] PUBLISHER Retrieved on ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + ut + "['\"]*" + ws + pub + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|publisher= $4|accessdate= $5}}$6");
  // Match <ref>[URL TITLE] ACCESSDATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + ut + "['\"]*" + ws + "Retrieved on " + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|accessdate= $4}}$5");
  // Match <ref>[URL TITLE] PUBLISHER DATE</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + ut + "['\"]*" + ws + pub + ws + "(?:published)*" + ws + matchdate + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3|publisher= $4|date= $5}}$6");
  // Match <ref>[URL TITLE]</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + ut + "['\"]*" + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $2|title= $3}}$4");
  // Match <ref>DATE [URL TITLE] PUBLISHER</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + matchdate + ut + ws + "['\"]*" + ws + pub + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $3|title= $4|publisher= $5|date= $2}}$6");
  // Match <ref>DATE [URL TITLE]</ref>
  s.compile( "(<ref[^<>]*>)[ ]*['\"]*[ ]*" + matchdate + ut + ws + "['\"]*" + "[:;\., ]*(</ref>)", "gi");
  str=str.replace(s, "$1{{cite web|url= $3|title= $4|date= $2}}$5");

  return str;
}

// Convert some references to cite journal templates
function spork_cite_article(str) {
  // Match <ref>First Last (YYYY) "TITLE" ''JOURNAL'', Vol ##, pages ##-##</ref>
  str=str.replace(/(<ref[^<>]*>)([A-Z][a-zî ]+) ([A-Z][a-z]+) \(([12][0-9][0-9][0-9])\) "([^"<>]*)[, ]*"[, ]* ''([^'<>]*)'', vol.(?:&nbsp;| )([0-9]+), pages(?:&nbsp;| )([0-9]+–?[0-9]*)\.?<\/ref>/g, '$1{{cite journal|first = $2|last = $3|year = $4|title = $5|journal = $6|volume = $7|pages = $8}}</ref>'); //"
  // Match <ref>First Last (YYYY) "TITLE" ''JOURNAL'', Vol ##, no. ##, pages ##-##</ref>
  str=str.replace(/(<ref[^<>]*>)([A-Z][a-zî ]+) ([A-Z][a-z]+) \(([12][0-9][0-9][0-9])\) "([^"<>]*)[, ]*"[, ]* ''([^'<>]*)'', vol.(?:&nbsp;| )([0-9]+), no.(?:&nbsp;| )([0-9]+), pages(?:&nbsp;| )([0-9\-]+)\.?<\/ref>/g, '$1{{cite journal|first = $2|last = $3|year = $4|title = $5|journal = $6|volume = $7|number = $8|pages = $9}}</ref>'); //"

  return str;
}
// </nowiki>