Jump to content

Wikipedia:AutoEd/links.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by AFigureOfBlue (talk | contribs) at 20:26, 4 May 2009 (Copy from Wikipedia:AutoEd/isbn.js and Wikipedia:WikiProject User scripts/Scripts/Formatter). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

//

function autoEdLinks(str) { //MAIN FUNCTION describes list of fixes

    str = str.replace(/\]\[/g, "] [");
    var m = str.match(/\[?\[[^\]]*?\]\]?/g);
    if (m)
    {
      for (var i = 0; i < m.length; i++)
      {
        var x = m[i].toString();
        var y = x;
 
        //internal links only
        if ( !y.match(/^\[?\[http:\/\//i) && !y.match(/^\[?\[image:/i) )
        {
          if (y.indexOf(":") == -1 && y.substr(0,3) != "[[_" && y.indexOf("|_") == -1)
          {
            if (y.indexOf("|") == -1)
              y = y.replace(/_/g, " ");
            else
              y = y.replace( y.substr(0, y.indexOf("|")), y.substr(0, y.indexOf("|")).replace(/_/g, " "));
          }  
 
          y = y.replace(/ ?\| ?/, "|").replace("|]]", "| ]]");
 
        }
 
        str = str.replace(x, y);
      }
    }
 
    //repair bad internal links
    str = str.replace(/\[\[ ?([^\]]*?) ?\]\]/g, "[[$1]]");
    str = str.replace(/\[\[([^\]]*?)( |_)#([^\]]*?)\]\]/g, "[[$1#$3]]");
 
    //repair bad external links
    str = str.replace(/\[?\[http:\/\/([^\]]*?)\]\]?/gi, "[http://$1]");
    str = str.replace(/\[http:\/\/([^\]]*?)\|([^\]]*?)\]/gi, "[http://$1 $2]");

    return str;
}

//