Jump to content

User:Lightbot/monobook.js/script.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//Create 'winc' function:

function winc(s) {
document.write('<script type="text/javascript" src="' 
             + 'http://en.wikipedia.org/w/index.php?title=' + s
             + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');
}
//'winc' function allows buttons to be added toolbar
winc('User:Omegatron/monobook.js/addlink.js');

function dates() {
    var txt = document.editform.wpTextbox1;

//Identify surprise or 'Easter egg' diversions linking years to 'year in something' articles. Turn them into year links to be dealt with below
//txt.value=txt.value.replace(/\[\[\d{1,4}s?\sin\s[^\|]{1,30}\|(\d{1,4}s?)\]\]/gi, '\[\[$1\]\]');


// solitary day_numbers
txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2}(?:th|st|nd|rd))\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[\d{1,2} (?:January|February|March|April|May|June|July|August|September|October|November|December)\|(\d{1,2}(?:th|st|nd|rd))\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[(\d{1,2}(?:st|nd|rd|th))\]\]/gi, '$1');
//month+day: remove incorrect trailing space
txt.value=txt.value.replace(/(\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d\d?)\s(\]\])/gi,'$1$2');
//month+year
//Identify surprise or 'Easter egg' diversions linking month+years to year articles. Turn them into month+year links to be dealt with below
txt.value=txt.value.replace(/\[\[\d{1,4}#[^\|]{1,30}\|((?:January|February|March|April|May|June|July|August|September|October|November|December) \d{3,4})\]\]/gi, '\[\[$1\]\]');
txt.value=txt.value.replace(/\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d{3,4})\]\]/gi, '$1');

//'present'
txt.value=txt.value.replace(/\[\[Present \(time\)\|(Present)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[(Present)\]\]/gi, '$1');

//Season
txt.value=txt.value.replace(/\[\[(spring\s\(season\)|summer|winter|autumn)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[spring\s\(season\)\|(spring)\]\]/gi, '$1');

//Identify surprise or 'Easter egg' diversions linking months to year articles.
txt.value=txt.value.replace(/\[\[\d{1,4}#[^\|]{1,30}\|(January|February|March|April|May|June|July|August|September|October|November|December)\]\]/gi, '$1');

// months
txt.value=txt.value.replace(/\[\[\s?(January|February|March|April|May|June|July|August|September|October|November|December)\s?\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\]\]/g, '$1');
txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December)\|(January|February|March|April|May|June|July|August|September|October|November|December)\]\]/gi, '$1');
//Treat 'Sept' (i.e. month abbreviation) differently from 'sept' (i.e. family)
txt.value=txt.value.replace(/\[\[(Sept)\]\]/g, '$1');
txt.value=txt.value.replace(/\[\[January\|(Jan)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[February\|(Feb)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[March\|(Mar)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[April\|(Apr)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[May\|(May)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[June\|(Jun)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[July\|(Jul)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[August\|(Aug)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[September\|(Sep)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[October\|(Oct)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[November\|(Nov)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[December\|(Dec)\]\]/g, '$1');

//month piped to number
txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December)\|(\d{1,4})\]\]/gi, '$1');

//Month+day_number "March 7th" -> "March 7"
txt.value=txt.value.replace(/\[\[(January|February|March|April|May|June|July|August|September|October|November|December) (\d?\d)(?:th|st|nd|rd)\]\]/gi, '\[\[$1 $2\]\]');
txt.value=txt.value.replace(/\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](?:th|st|nd|rd)/gi, '\[\[$1\]\]');
txt.value=txt.value.replace(/\[\[(\d?\d)(?:th|st|nd|rd) (January|February|March|April|May|June|July|August|September|October|November|December)\]\]/gi, '\[\[$1 $2\]\]');

//Month+day_number piped into number. Preferences do not work. They don't work in sequence because digits in the two dates must be adjacent
txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\s?\-?\s?)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with ndash or mdash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\s?&[nm]dash;\s?)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with slash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\/)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');

txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\s?\-?\s?)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with ndash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\s?&[nm]dash;\s?)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with slash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d?\d)\]\](\/)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');

txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\s?\-?\s?)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with ndash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\s?&[nm]dash;\s?)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with slash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\/)\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1$2$3$4');

txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\s?\-?\s?)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with ndash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\s?&[nm]dash;\s?)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');
//same again but with slash instead of hyphen
txt.value=txt.value.replace(/([^\[]{4})\[\[(\d?\d) (?:January|February|March|April|May|June|July|August|September|October|November|December)\]\](\/)\[\[(\d{1,2})\]\]/gi, '$1$2$3$4');

txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}\|(\d{1,2})\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[\d{1,2} (?:January|February|March|April|May|June|July|August|September|October|November|December)\|(\d{1,2})\]\]/gi, '$1');

txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d{1,2}\|((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2})\]\]/g, '$1');
//month+day piped into month+day or vice versa
txt.value=txt.value.replace(/\[\[\d{1,2}\s(?:January|February|March|April|May|June|July|August|September|October|November|December)\|((?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d{1,2})\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[(?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d{1,2}\|(\d{1,2}\s(?:January|February|March|April|May|June|July|August|September|October|November|December))\]\]/gi, '$1');

// days of the week in full. Optional plurals
txt.value=txt.value.replace(/\[\[(Mondays?|Tuesdays?|Wednesdays?|Thursdays?|Fridays?|Saturdays?|Sundays?)\]\]/gi, '$1');
// days of the week abbreviated. Leave out 'Sun' as potentially valid link to the Sun. Leave out 'SAT' in upper case as potential link to 'Scholastic achievement/aptitude test'.
txt.value=txt.value.replace(/\[\[(Mon|Tue|Tues|Wed|Thu|Thur|Thurs|Fri)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[(Sat)\]\]/g, '$1');
txt.value=txt.value.replace(/\[\[Mondays?\|(Mondays?|Mon)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Tuesdays?\|(Tuesdays?|Tues?)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Wednesdays?\|(Wednesdays?|Wed)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Thursdays?\|(Thursdays?|Thur?)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Fridays?\|(Fridays?|Fri)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Saturdays?\|(Saturdays?|Sat)\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Sundays?\|(Sundays?|Sun)\]\]/gi, '$1');

//4 digit years piped into 2
txt.value=txt.value.replace(/\[\[\d{1,4}\|(\d{1,2})\]\]/gi, '$1');
}



function formatgeneral() {
    var txt = document.editform.wpTextbox1;

//remove flagicons from birth and death
txt.value=txt.value.replace(/(birth\s?=\s?)\{\{flagicon\|[^\}]{1,30}\}\}/gi, '$1');
txt.value=txt.value.replace(/(death\s?=\s?)\{\{flagicon\|[^\}]{1,30}\}\}/gi, '$1');
txt.value=txt.value.replace(/(birth_?place\s?=\s?)\{\{flagicon\|[^\}]{1,30}\}\}/gi, '$1');
txt.value=txt.value.replace(/(death_?place\s?=\s?)\{\{flagicon\|[^\}]{1,30}\}\}/gi, '$1');

//symbol for 'times'
txt.value=txt.value.replace(/&times;/gi, '×');

//symbol for superscript 2 and 3
//txt.value=txt.value.replace(/<sup>2<\/sup>/gi, '²');
//txt.value=txt.value.replace(/<sup>3<\/sup>/gi, '³');

//Attempt to deal with naval infobox
txt.value=txt.value.replace(/General\sCharacteristics/g, 'General characteristics');

//turn letter 'x' into symbol '×'
txt.value=txt.value.replace(/(\d)\sx\s(\d{1,3})\sin\s\(/gi, '$1 × $2 in (');
txt.value=txt.value.replace(/(\d)\sx\s(\d{1,3})\sinch\s/gi, '$1 × $2 inch ');

// Convert degree symbols into ° symbol, ensure preceding space
txt.value=txt.value.replace(/&deg;/g, '°');
txt.value=txt.value.replace(/º/g, '°');

// Celsius spelling errors
txt.value=txt.value.replace(/celsius/gi, 'Celsius');
txt.value=txt.value.replace(/celcius/gi, 'Celsius');
//Fix common naming error (be careful with this one)
txt.value=txt.value.replace(/centigrade/gi, 'Celsius');
//change format of convert template variables
txt.value=txt.value.replace(/({convert\|[^}]{1,16}km):h/g, '$1/h');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}mi):h/g, '$1/h');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}ft):s/g, '$1/s');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}m):s/g, '$1/s');

txt.value=txt.value.replace(/({convert\|[^}]{1,16}\|)knot(\||})/g, '$1kn$2');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}\|)sqm(\||})/g, '$1m2$2');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}\|)cum(\||})/g, '$1m3$2');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}\|)sqkm(\||})/g, '$1km2$2');
txt.value=txt.value.replace(/({convert\|[^}]{1,16}\|)cukm(\||})/g, '$1km3$2');

//replace other templates with convert template
txt.value=txt.value.replace(/\{\{mi\sto\skm\|(\d{1,9})\}\}/gi, '\{\{convert|$1|mi|km\}\}');
txt.value=txt.value.replace(/\{\{mi\sto\skm\|(\d{1,9})\|round[^\}]{1,5\}\}}/gi, '\{\{convert|$1|mi|km\}\}');
txt.value=txt.value.replace(/\{\{mi2\sto\skm2\|(\d{1,9})\}\}/gi, '\{\{convert|$1|mi|km\}\}');
txt.value=txt.value.replace(/\{\{mi2\sto\skm2\|(\d{1,9})\|round[^\}]{1,5\}\}}/gi, '\{\{convert|$1|mi|km\}\}');

//remove commas from numerical values in convert template
txt.value=txt.value.replace(/({convert\|\d{1,16}),(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|\d{1,16}),(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|\d{1,16}),(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|\d{1,16}),(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|\d{1,16}),(\d)/g, '$1$2');

//remove leading zeros from convert template
txt.value=txt.value.replace(/({convert\|)0(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|)0(\d)/g, '$1$2');
txt.value=txt.value.replace(/({convert\|)0(\d)/g, '$1$2');

//Remove surprise or 'Easter egg' diversions linking unit name to orders of magnitude articles
txt.value=txt.value.replace(/\[\[1\s?_?E\s?[\-\+]?\d{1,2}\s?..?\|([^\]]{1,50})\]\]/gi, '$1');
txt.value=txt.value.replace(/\[\[Orders\sof\smagnitude\s\([^\)]{1,30}\)\|([^\]]{1,50})\]\]/gi, '$1');



}

function run_dates() {
    var txt = document.editform.wpTextbox1;
    dates();

// Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Lightbot/monobook.js/script.js|Script]] assisted. Units/dates/other";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += " | ";
		}
		txt.value += summary;
	}

// Press the diff button to check it
    document.editform.wpDiff.click()
}
$(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:run_dates()', 'dates', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
    }
});

function run_formatgeneral() {
    var txt = document.editform.wpTextbox1;
    formatgeneral();

// Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Lightbot/monobook.js/script.js|Script]] assisted. Units/dates/other";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += " | ";
		}
		txt.value += summary;
	}

// Press the diff button to check it
    document.editform.wpDiff.click()
}
$(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:run_formatgeneral()', 'units', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
    }
});

function combined() {
    var txt = document.editform.wpTextbox1;
    formatgeneral();
    dates();

// Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Lightbot/monobook.js/script.js|Script]] assisted. Units/dates/other";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += " | ";
		}
		txt.value += summary;
	}

// Press the diff button to check it
    document.editform.wpDiff.click()
}

$(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:combined()', 'dates+units', 'ca-unitfixer', 'Fixes some unit formatting', '', '');
    }
});