User:Lenore/autolink.js

// Autolink wikilinks, [external links] and {templates} (especially useful for monobook.js

// and similar pages); adapted from Wikipedia:WikiProject User scripts/Scripts/Autolink

/* ---------------------------------------------------------------------------------------------- */

$(function () {

var autolinkCronoSpecial = mw.config.get('wgCanonicalSpecialPageName') == "Recentchanges" || /*

*/ mw.config.get('wgCanonicalSpecialPageName') == "Watchlist" || /*

*/ mw.config.get('wgCanonicalSpecialPageName') == "Recentchangeslinked" || /*

*/ mw.config.get('wgCanonicalSpecialPageName') == "Newpages" || /*

*/ mw.config.get('wgCanonicalSpecialPageName') == "Log" || /*

*/ mw.config.get('wgCanonicalSpecialPageName') == "Contributions";

// Special crono pages where this script is enabled

if ((typeof(autolinkMySpecialPage) != "undefined"))

autolinkCronoSpecial = autolinkCronoSpecial || mw.config.get('wgPageName') == autolinkMySpecialPage;

if (mw.config.get('wgCanonicalNamespace') == "Special" && !autolinkCronoSpecial) {

return; // Disabled in the other special pages

}

if (typeof(autolinkParseLink) == 'undefined') {

autolinkParseLink = true; // Parse links in all cases, even in diffs. To be disabled when wikEd is active

}

/* ---------------------------------------- Variables ------------------------------------------- */

var autolinkDiff = location.href.indexOf('diff=') != -1;

// It says if I'm in a diff page

var autolinkCrono = (mw.config.get('wgAction') == 'history' || autolinkCronoSpecial);

// It says if I'm in a history page

var autolinkEdit = ($.inArray(mw.config.get('wgAction'), ['edit' , 'submit']) !== -1);

// It says if I'm in an edit or submit page

var autolinkColor; // links color

var autolinkTargetDiv = [];

if (autolinkDiff) { // in diff pages

if (autolinkParseLink) { // without wikEd

autolinkTargetDiv[0] = $('.diff');

autolinkTargetDiv[1] = $('.firstrevisionheader');

autolinkColor = 'inherit'; // not coloured links

} else { // with wikEd

autolinkTargetDiv[0] = $('.comment');

autolinkColor = ''; // coloured links

}

} else if (autolinkCrono || autolinkEdit) { // in comments

autolinkTargetDiv[0] = $('.comment');

autolinkColor = ''; // coloured links

} else { // in code sections

autolinkTargetDiv[0] = $('source');

autolinkTargetDiv[1] = $('.javascript');

autolinkTargetDiv[2] = $('.source-javascript');

autolinkTargetDiv[3] = $('.css');

autolinkTargetDiv[4] = $('.source-css');

autolinkColor = ''; // coloured links

}

/* ------------------------------------------ Regex --------------------------------------------- */

var autolinkRegexTl;

var autolinkRegexURL;

var autolinkRegexURLinWcodeWoLabel;

var autolinkRegexSubstinWcodeWoLabel;

var autolinkRegexURLinWcodeWithLabel;

var autolinkRegexSubstinWcodeWithLabel;

var autolinkRegexOtherPages;

var autolinkRegexSubstOtherPages;

var autolinkRegexTemplate;

var autolinkRegexSubstTemplate;

var autolinkRegexWlink1;

var autolinkRegexSubstWlink1;

var autolinkRegexWlink2;

var autolinkRegexSubstWlink2;

var autolinkRegexInternalURL;

var autolinkRegexImportScript;

var autolinkActivateHTML = false;

/* ------------------------------------------------------------------------------------------ */

autolinkRegexTl = /([^{]{{2}\s*[Tt]l\|)([^[\]{}|:<>\n]+)/g;

// For {{tl}}

autolinkRegexURL = /((?:[^"\[]|[^=]")(?:)?)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+?)(?=(?:<\/span>)?[\s|)\]}'"])/g;

// External links (no wikicode)

// Regex for diffs

autolinkRegexURLinWcodeWoLabel = /((?:[^\[]|^)\[\s*(?:<\/span>)?\s*(?:)?\s*)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)((?:<\/span>)?(?:)?)([\w._\/\\~%\-+&#?!=()@:]*)((?:<\/span>)?(?:)?)([\w._\/\\~%\-+&#?!=()@:]*)([^\]\n]*\])/gm;

autolinkRegexSubstinWcodeWoLabel = '$1$2$3$4$5$6$7';

// External links in diff pages, wikicode without label

autolinkRegexURLinWcodeWithLabel = autolinkRegexURLinWcodeWoLabel;

autolinkRegexSubstinWcodeWithLabel = autolinkRegexSubstinWcodeWoLabel;

// External links in diff pages, wikicode with label

autolinkRegexOtherPages = /((?:[^{]|^)\{{2}\s*(?:<\/span>)?\s*(?:)?\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*(?:<\/span>)?\s*(?:)?\s*)((?:[Dd]iscussion[ei] ?)?\s*(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto)?\s*(?: ?[Tt]alk)?\s*:[^[\]{}|:<>\n]+)((?:<\/span>)?(?:)?)([^[\]{}|:<>\n]*)((?:<\/span>)?(?:)?)([^[\]{}|:<>\n]*)(\||}{2})/gm;

autolinkRegexSubstOtherPages = '$1$2$3$4$5$6$7';

// Other pages included in diff pages

autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:<\/span>)?\s*(?:)?\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*(?:<\/span>)?\s*(?:)?)([^[\]{}|:<>\n]+)((?:<\/span>)?(?:)?)([^[\]{}|:<>\n]*)((?:<\/span>)?(?:)?)([^[\]{}|:<>\n]*)(\||}{2}|:)/gm;

autolinkRegexSubstTemplate = '$1$2$3$4$5$6$7';

// Templates in diff pages

autolinkRegexWlink1 = /(\[{2}\s*(?:<\/span>)?\s*(?:)?\s*)([^[\]{}|<>\n]+)((?:<\/span>)?(?:)?)([^[\]{}|<>\n]*)((?:<\/span>)?(?:)?)([^[\]{}|<>\n]*)((?:[^\]\n]|\][^\]])*\]{2})/g;

autolinkRegexSubstWlink1 = '$1$2$3$4$5$6$7';

// Wikilinks in diff pages

autolinkRegexWlink2 = autolinkRegexWlink1;

autolinkRegexSubstWlink2 = autolinkRegexSubstWlink1;

// Regex for comments or code sections

if (!autolinkDiff || (autolinkDiff && !autolinkParseLink)) {

autolinkRegexURLinWcodeWoLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s*\]/gm;

autolinkRegexSubstinWcodeWoLabel = '$1$2';

// External links in comments or code sections, wikicode without label

autolinkRegexURLinWcodeWithLabel = /([^\[]|^)\[\s*((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)\s+([^\n]+?)\s*\]/gm;

autolinkRegexSubstinWcodeWithLabel = '$1$3';

// External links in comments or code sections, wikicode with label (the URL will not be visible)

autolinkRegexOtherPages = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)((?:[Dd]iscussion[ei] ?)?\s*(?:[Ss]peciale?|[Ww](?:ikipedia|P)|[Tt]emplate|[Uu](?:ser|tente)|[Pp]ortale?|[Pp]rogetto)?\s*(?: ?[Tt]alk)?\s*:[^[\]{}|:<>\n]+)(\||}{2})/gm;

autolinkRegexSubstOtherPages = '$1$2$3';

// Other pages included in comments or code sections

autolinkRegexTemplate = /((?:[^{]|^)\{{2}\s*(?:(?:[Ss][Uu][Bb][Ss][Tt]|[Mm][Ss][Gg]|[Mm][Ss][Gg][Nn][Ww])\s*:)?\s*)([^[\]{}|:<>\n]+)(\||}{2}|:)/gm;

autolinkRegexSubstTemplate = '$1$2$3';

// Templates in comments or code sections

autolinkRegexWlink1 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|\s*(.+?)\s*\]{2}/g;

autolinkRegexSubstWlink1 = '$2';

// Wikilinks in code sections, with label

autolinkRegexWlink2 = /\[{2}\s*([^[\]{}|<>\n]+?)\s*\|?\s*\]{2}/g;

autolinkRegexSubstWlink2 = '$1';

// Wikilinks in code sections, without label

autolinkActivateHTML = true;

// Activate some HTML (inline) and wikicode for bold and italic

}

autolinkRegexInternalURL = /([^=])('|"|\s)((?:\/?w\/index\.php\?|\/?wiki\/)[\w._\/\\~%\-+&#?!=()@:]+)\2/g;

// External links (no wikicode, under wgServer)

autolinkRegexImportScript = /([Ii]mport[Ss]cript(?:)?\((?:<\/span>)?)('|")([^[\]{}|<>\n]+?)(\2(?:<\/span>)?\)(?:<\/span>)?)/g;

// ImportScript

/* ---------------------------------------------------------------------------------------------- */

for (i=0; i

for (j=0; j

var autolinkContent = autolinkTargetDiv[i][j].innerHTML;

autolinkContent = autolinkContent.replace(/</g, '­<­');

autolinkContent = autolinkContent.replace(/>/g, '­>­');

// &lt; to &shy;<&shy; and &gt; to &shy;>&shy; (&shy; is a marker)

autolinkContent = autolinkContent.replace(/&/g, '&');

// &amp; to &

/* ----------------------------------------------------------------------------------- */

autolinkContent = autolinkContent.replace(autolinkRegexTl, '$1$2');

// For {{tl}}: make his argument into link

autolinkContent = autolinkContent.replace(autolinkRegexURL, '$1$2');

// Parse inactive external links (no wikicode)

autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWoLabel, autolinkRegexSubstinWcodeWoLabel);

// Make external links in wikicode without label into links

autolinkContent = autolinkContent.replace(autolinkRegexURLinWcodeWithLabel, autolinkRegexSubstinWcodeWithLabel);

// Make external links in wikicode with label into links

autolinkContent = autolinkContent.replace(autolinkRegexOtherPages, autolinkRegexSubstOtherPages);

// Make other pages included code into links

autolinkContent = autolinkContent.replace(autolinkRegexTemplate, autolinkRegexSubstTemplate);

autolinkContent = autolinkContent.replace(/href\="\/wiki\/Template\:#/g, 'href="/wiki/Help:');

// Make template code into links

autolinkContent = autolinkContent.replace(autolinkRegexWlink1, autolinkRegexSubstWlink1);

autolinkContent = autolinkContent.replace(autolinkRegexWlink2, autolinkRegexSubstWlink2);

// Make wikilink code into links

autolinkContent = autolinkContent.replace(autolinkRegexInternalURL, '$1$2$3$2');

// Parse inactive external links (no wikicode, under wgServer)

autolinkContent = autolinkContent.replace(autolinkRegexImportScript, '$1$2$3$4');

// Parse ImportScript

/* ----------------------------------------------------------------------------------- */

autolinkContent = autolinkContent.replace(/­<­/g, '<');

autolinkContent = autolinkContent.replace(/­>­/g, '>');

// &shy;<&shy; to &lt; and &shy;>&shy; to &gt; (revert)

if (autolinkActivateHTML) {

autolinkContent = autolinkContent.replace(/<(span|b|i|strong|small|tt|del|s|u|sub|sup)>(.*?)<\/(\1)>/g, '<$1>$2');

autolinkContent = autolinkContent.replace(/([^']|^)'{3}(.+?)'{3}([^']|$)/gm, '$1$2$3');

autolinkContent = autolinkContent.replace(/([^']|^)'{2}(.+?)'{2}([^']|$)/gm, '$1$2$3');

if (autolinkCrono || autolinkEdit)

autolinkContent = autolinkContent.replace(/(.*?)<\/i>/g, '$1');

// I'm in a comment field (italic)

}

if (autolinkDiff && autolinkParseLink) {

autolinkContent = autolinkContent.replace(/]+><\/a>/g, ''); // Clean

autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(]+>)(?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+(<\/a>)\s+([^\]\n]+)\]/gm, '$1$2$4$3');

autolinkContent = autolinkContent.replace(/([^\[]|^)\[\s*(]+>)((?:https?|ftps?):\/\/[\w._\/\\~%\-+&#?!=()@:]+)(<\/a>)\s*\]/gm, '$1$2$3$4');

}

autolinkTargetDiv[i][j].innerHTML = autolinkContent; // Write it back

}

}

});

// Autolink