// Credits: A modification of [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
function autoEdWikilinks(str) { //MAIN FUNCTION describes list of fixes

 //Get the list of all wikilinks with underscores
 var m = str.match(/\[\[[^\[\]]*_[^\[\]]*\]\]/g);
 if (m) {
  //For each wikilink in the list
  for (var i = 0; i < m.length; i++) {
   var x = m[i].toString() // Contains the entire wikilink
   // Exclude URLs and leading underscores
   if( !x.match(/^\[\[[\t ]*(?:http|ftp|https):/i) && !x.match(/^\[\[_[^\[\]]*\]\]/) ) {
    var x_arr = x.match(/^(\[\[[^\[\]\|]*)(\|?[^\[\]]*?\]\])$/);
    var a = x_arr[1]; // Everything before the pipe (or everything minus ]])
    var b = x_arr[2]; // Everything after the pipe (or ]])

    // Replace underscores with spaces
    a = a.replace(/_/g, ' ');
    // Do the replacement
    str = str.replace(x, a+b);
   }
  }
 }

 //Fix links which have no target
 str = str.replace(/\[\[[\t ]*\|/gi, '[[');
 
 //Leading and trailing space before the pipe inside wikilinks
 str=str.replace(/(\[\[)[\t ]+([^\[\]\|]*?)(\|)/g, '$1$2$3');
 str=str.replace(/(\[\[)([^\[\]\|]*?)[\t ]+(\|)/g, '$1$2$3');
 //Leading space after the pipe (or in an unpiped) wikilink
 str=str.replace(/^(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, '$1$2$3');
 str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, ' $1$2$3');
 //Trailing space after the pipe (or in an unpiped) wikilink
 str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|\t ][^\[\]\|]*?)[\t ]+(\]\])([^A-Za-z])/gi, '$1$2$3 $4');
 str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|]*?)[\t ]+(\]\])$/gi, '$1$2$3');

 //Get the list of all piped wikilinks
 var m = str.match(/\[\[[^\[]*?\|[^\[]*?\]\]/g);
 if (m) {
  //For each piped wikilink in the list
  for (var i = 0; i < m.length; i++) {
   var n_arr = m[i].toString().match(/\[\[[ ]*([^\[]*?)\|[ ]*([^\[]*?)\]\]/);
   var n = n_arr[0]; // Contains the entire piped link
   var a = n_arr[1]; // Contains everything before pipe
   var b = n_arr[2]; // Contains everything after pipe
   var c = b.replace(/[\.,:; ]*$/); // Same as b, but without trailing punctuation
   //Is the display name a leading substring of the wikilink?
   if (b.indexOf(a) == 0 || b.indexOf(autoEd_first2lower(a)) == 0) {
    //Create a simplified replacement string
    var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w]*?)\]\]/i, "[[$2]]$3");
    //And do the replacement
    str = str.replace(n, k);
   }
   if (c.indexOf(a) == 0 || c.indexOf(autoEd_first2lower(a)) == 0) {
    // Create a simplified replacement string
    var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w\.,:;]*?)\]\]/i, "[[$2]]$3");
    // And do the replacement
    str = str.replace(n, k);
   }
  }
 }

 //Push trailing characters into display string of piped wikilinks
 //str = str.replace(/\[\[([^\[\]\|]+)\|([^\[\]\|]+)\]\]([a-z]+)/g, "[[$1|$2$3]]");

 //Removes links to current article
 var p1 = mw.config.get('wgPageName');                    // PAGENAME including underscores
 var p2 = mw.config.get('wgPageName').replace(/_/g, ' '); // PAGENAME without underscores
 var p3 = autoEd_first2lower(p1);      // First character lowercase PAGENAME including underscores
 var p4 = autoEd_first2lower(p2);      // First character lowercase PAGENAME without underscores
 // Standard wikilinks
 str = str.replace(new RegExp('\\[\\[(' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\]\\]', 'g'), '$1');
 // Piped wikilinks
 str = str.replace(new RegExp('\\[\\[(?:' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\|([^\\]\\|]*)\\]\\]', 'g'), '$1');

 //Shorten interwiki links
 str = str.replace(/\[\[WIKTIONARY:/gi, '[[wikt:');
 str = str.replace(/\[\[WIKINEWS:/gi, '[[n:');
 str = str.replace(/\[\[WIKIBOOKS:/gi, '[[b:');
 str = str.replace(/\[\[WIKIQUOTE:/gi, '[[q:');
 str = str.replace(/\[\[WIKISOURCE:/gi, '[[s:');
 str = str.replace(/\[\[WIKISPECIES:/gi, '[[species:');
 str = str.replace(/\[\[WIKIVERSITY:/gi, '[[v:');
 str = str.replace(/\[\[(?:WIKIMEDIA|FOUNDATION):/gi, '[[wmf:');
 str = str.replace(/\[\[METAWIKIPEDIA:/gi, '[[m:');

 //Replace [[Foo #bar]] -> [[Foo#bar]]
 str = str.replace(/\[\[([^\]]*?)( |_)+#([^\]]*?)\]\]/g, '[[$1#$3]]');

 //Replace [[Foo|Foo]] -> [[Foo| ]]
 str = str.replace(/\|\]\]/g, '| ]]');

 return str;
}

// Converts the first character in a string to lower case
// Notes: Used by autoEdWikilinks
function autoEd_first2lower(str) {
 if (str != "") {
  var letter = str.substr(0, 1);
  return letter.toLowerCase() + str.substr(1, str.length);
 } else {
  return "";
 }
}