User:Plastikspork/tools.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Plastikspork/tools. |
// --------------------------------------- --------------------------------------- //
// ----------------------------------- 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;
}