User:Plastikspork/tools.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.
// ---------------------------------------  --------------------------------------- //
// ----------------------------------- Credits ------------------------------------ //
//
// These javascript tools were inspired by
//      [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
// Many of the functions were created by modifying existing Formatter functions
//
// Some functions were created in collaboration with
//      [[User:Drilnoth/codefixer.js]]
//
// This script is intended to be complimentary to these scripts with very little
// to no duplication of function.
 
// ---------------------------------- Disclaimer ---------------------------------- //
//
// Use at your own risk and make sure you check the edit changes before you save
//
// Let me know [[User_Talk:Plastikspork]] if you find bugs!

// ----------------------------- Installing the Script ---------------------------- //
//
// (1) Open/Create your USERNAME/monobook.js page, where USERNAME is your username.
//     A quick way to get there is to go to your user page, then append
//     '/monobook.js' to the end of the URL.
//
// (2) Put the following command on your monobook.js page:
//          importScript('User:Plastikspork/tools.js');
//
// (3) Save the page and reload it by following the instructions at the top of your
//     monobook.js page.  For example, Ctrl+Shift+R in Firefox.

// ------------------------------- Using the Script ------------------------------- //
//
// (1) This is a collection of javascript functions which can be called from other
//     scripts.
//
//  Note: Some functions could run slowly on very large pages, but do 
//        always eventually complete in my experience.
// ---------------------------------------  --------------------------------------- //


// --------------------- spork_extern_wikilinks_to_wikilinks ---------------------- //
// Purpose: Converts External Wikipedia links to internal Wikipedia Links
//
// Examples: [http://en.wikipedia.org/wiki/dog puppy] -> [[dog|puppy]]
//           dog blah [http://en.wikipedia.org/wiki/dog] -> [[dog]] blah
//           [http://en.wikipedia.org/wiki/dog] blah dog -> blah [[dog]]
//           dog <ref>http://en.wikipedia.org</ref> -> [[dog]]
//           [[dog]] <ref>http://en.wikipedia.org</ref> -> [[dog]]
//           dog [http://en.wikipedia.org/dog_(band)] -> [[dog (band)|dog]]
//
// Credits: Inspired by [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
//
function spork_extern_wikilinks_to_wikilinks(str){
  var nr = new RegExp("", "g");
  var s = new RegExp("", "gi");
  var r = "";
  var ptext = "(?:[^\\[\\]<>]|\\[\\[[^\\[\\]]*\\]\\]|\\[[^\\[\\]]*\\]|<ref[^<>]*>[^<>]*<\/ref>)*?";

  // Trailing slash in external wikipedia links
  str=str.replace(/(http:\/\/[a-z]{2,3}\.wikipedia\.org\/wiki\/[^ \[\]\|<>\r\n]*)\/([^\w%&?])/g, '$1$2');
  // Broken wikilinks
  str=str.replace(/(\[http:\/\/[a-z]{2,3}\.wikipedia\.org[^\]\[\| ]*)[ ]*\|[ ]*([^\]\[\|]*\])/g, '$1 $2');
  str=str.replace(/(\[http:\/\/[a-z]{2,3}\.wikipedia\.org[^\]\[\| ]*)[ ]*\[[ ]*([^\]\[\|]*\])\]/g, '$1 $2');
  // Convert "external wikipedia" image links to wikilinks
  str=str.replace(/(\[)http:\/\/en\.wikipedia\.org\/wiki\/Image(:[^ \[\]\|<>]*) ([^\|\[\]]*)(\])/gi, '$1$1:Image$2|$3$4$4');
  str=str.replace(/(\[)http:\/\/en\.wikipedia\.org\/wiki\/File(:[^ \[\]\|<>]*) ([^\|\[\]]*)(\])/gi, '$1$1:File$2|$3$4$4');
  // Convert "external wikipedia" links to wikilinks
  str=str.replace(/(\[)http:\/\/([a-z]{2,3})\.wikipedia\.org\/wiki\/([^ \[\]\|<>]*) ([^\|\[\]]*)(\])/gi, '$1$1:$2:$3|$4$5$5');
  str=str.replace(/(\[)http:\/\/([a-z]{2,3})\.wikipedia\.org\/w\/index\.php\?title=([^ \[\]\|\&<>]*) ([^\|\[\]]*)(\])/gi, '$1$1:$2:$3|$4$5$5');

  // Get the list of all external wikilinks
  var m = str.match(/(?:<ref[^<>]*>|\[)+http:\/\/[a-z]{2,3}\.wikipedia\.org\/wiki\/[^ <>\[\]\|]*(?:\]+|<\/ref>)+/gi);
  if (m) {
    // For each external wikilink in the list
    for (var i = 0; i < m.length; i++) {
      var n_arr = m[i].toString().match(/(?:<ref[^<>]*>|\[)+http:\/\/([a-z]{2,3})\.wikipedia\.org\/wiki\/([^ <>\[\]\|]*)(?:\]+|<\/ref>)+/);
      var n = n_arr[0]; // Contains the entire wiki link
      var a = n_arr[1]; // Contains the language
      var b = n_arr[2]; // Contains the page target
      b = b.replace(/_/g, ' '); // Remove the underscores
      var bm = b.replace(/([\(\)\.])/g, '\\$1'); // Backslash parenthesis and periods
      n = n.replace(/([\[\]\(\)\.])/g, '\\$1'); // Backslash brackets, parentheses, periods
      nr.compile(n, "g");

      // Can we find matching text before link?
      s.compile( "\\[\\[:[a-z]+:(" + bm + ")\\]\\]" + "(" + ptext + ")" + n, "gi");
      r = "[[" + ":" + a + ":" + b + "\|" + "$1" + "]]" + "$2";
      str = str.replace(s, r);
      s.compile( "\\[\\[(" + bm + ")\\]\\]" + "(" + ptext + ")" + n, "gi");
      str = str.replace(s, r);
      s.compile( "(" + bm + ")" + "(" + ptext + ")" + n, "gi");
      str = str.replace(s, r);
      if( -1 == str.search(nr) ) continue;

      // Can we find matching text after link?
      s.compile( n + "\\[\\[:[a-z]+:(" + ptext + ")\\]\\]" + "(" + bm + ")", "gi");
      r = "$1" + "[[" + ":" + a + ":" + b + "\|" + "$2" + "]]";
      str = str.replace(s, r);
      s.compile( n + "\\[\\[(" + ptext + ")\\]\\]" + "(" + bm + ")", "gi");
      str = str.replace(s, r);
      s.compile( n + "(" + ptext + ")" + "(" + bm + ")", "gi");
      str = str.replace(s, r);
      if( -1 == str.search(nr) ) continue;

      // Does the link have a trailing parenthetical string?
      bm = bm.replace( / \\\(.*?\\\)$/, '');

      // Can we find matching text before link?
      s.compile( "(" + bm + ")" + "(" + ptext + ")" + n, "gi");
      r = "[[" + ":" + a + ":" + b + "\|" + "$1" + "]]" + "$2";
      str = str.replace(s, r);
      if( -1 == str.search(nr) ) continue;

      // Can we find matching text after link?
      s.compile( n + "(" + ptext + ")" + "(" + bm + ")", "gi");
      r = "$1" + "[[" + ":" + a + ":" + b + "\|" + "$2" + "]]";
      str = str.replace(s, r);
      if( -1 == str.search(nr) ) continue;
    }
  }

  // Remove redundant lang designation
  str = str.replace(new RegExp('(\\[\\[):' + mw.config.get('wgContentLanguage') + ':', 'g'), '$1');
 
  return str;
}