Jump to content

User:Bobblewik/monobook.js/datestest.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bobblewik (talk | contribs) at 18:45, 19 March 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
function formatdates() {
    var txt = document.editform.wpTextbox1;

    // century
    txt.value = txt.value.replace(/\[\[(\d{1,2}(?:st|nd|rd|th))[ \-]century\]\]/gi, '$1 century');
    txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]century\]\]/gi, '$1 century');
    txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]centuries\]\]/gi, '$1 centuries');

    // decades
    txt.value = txt.value.replace(/\[\[(\d{1,4}s)\]\]/gi, '$1');

    // months
    txt.value = txt.value.replace(/\[\[(January|February|March|April|May|June|July|August|September|October|November|December)\]\]/gi, '$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)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d{3,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\]\]');
    //Month+day_number piped into number
    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');
    // 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');

    // days of the week including optional plurals
    txt.value = txt.value.replace(/\[\[(Mondays?|Tuesdays?|Wednesdays?|Thursdays?|Fridays?|Saturdays?|Sundays?)\]\]/gi, '$1');
    // days of the week but leave out 'Sun' as potentially valid link to the Sun
    txt.value = txt.value.replace(/\[\[(Mon|Tue|Tues|Wed|Thu|Thur|Thurs|Fri|Sat)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Mondays?\|(Mondays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Tuesdays?\|(Tuesdays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Wednesdays?\|(Wednesdays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Thursdays?\|(Thursdays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Fridays?\|(Fridays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Saturdays?\|(Saturdays?)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[Sundays?\|(Sundays?)\]\]/gi, '$1');

    //BC etc
    txt.value = txt.value.replace(/\[\[(\d{1,4} (?:AD|BC|CE|BCE))\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,4} (?:AD|BC|CE|BCE)\|(\d{1,4})\]\]/gi, '$1');
    //4 digit years piped into 2
    txt.value = txt.value.replace(/\[\[\d{1,4}\|(\d{1,2})\]\]/gi, '$1');

    //year: last character in link on left is not month or digit, first character in link on right is not month or digit
    txt.value = txt.value.replace(/([^yhletr\d]\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^jfmasond\d])/gi, '$1$2$3');
    //year pair: last character in link on left is not month or digit, first character in link on right is not month or digit
    txt.value = txt.value.replace(/([^yhletr\d]\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^jfmasond\d])/gi, '$1$2$3$4$5');

    //year: last character in link on left is not month or digit, avoid links on right
    txt.value = txt.value.replace(/([^yhletr\d]\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3');
    //year pair: last character in link on left is not month or digit, avoid links on right
    txt.value = txt.value.replace(/([^yhletr\d]\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3$4$5');

    //year: check for line-ends, text on left, avoid links on right. Run twice to deal better with lists.
    txt.value = txt.value.replace(/([\w\(\);:.\*\|\&]\s?,?\-?\s?|\n)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3');
    txt.value = txt.value.replace(/([\w\(\);:.\*\|\&]\s?,?\-?\s?|\n)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3');
    //year pair: check for line-ends, text on left, avoid links on right
    txt.value = txt.value.replace(/([\w\(\);:.\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3$4$5');

    //year: avoid links on left, first character in link on right is not month or digit
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^jfmasond\d])/gi, '$1$2$3');
    //year pair: avoid links on left, first character in link on right is not month or digit
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^jfmasond\d])/gi, '$1$2$3$4$5');

    //year:avoid links on left, text on right
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);:.\*\|\&])/gi, '$1$2$3');
    //year pair: avoid links on left, text on right
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[=\w\(\);:.\*\|\&])/gi, '$1$2$3$4$5');

    //year:avoid links on both sides
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3');
    //year pair: avoid links on both sides
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3$4$5');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "Removing unnecessary date links per \[\[WP:DATE\]\]";
	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()
}

addOnloadHook(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:formatdates()', 'datestest', 'ca-datefixer', 'Fixes some date formatting', '', '');
    }
});