Wikipedia:AutoEd/wikilinks.js

//

//

// 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 = wgPageName;                  // PAGENAME including underscores
 var p2 = wgPageName.replace('_',' '); // 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| ]]
 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 "";
 }
}

//

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.