User:Karl Dickman/dates.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

//Script to add a link button

function addClassicLinkToTopbar() {

      var td = document.getElementById( 'topbar' ).getElementsByTagName( 'td' )[ 1 ];
      var loc = td.getElementsByTagName( 'p' )[ 0 ];
      var link = document.createElement( 'a' );
                link.appendChild( document.createTextNode( 'Dates' ) );
                link.href = 'javascript:formatdates()'
                link.id = 'topbar_dateformatter'
                link.title = 'Standardises formatting of dates in an article'
      td.insertBefore( link , loc.nextSibling );
      td.insertBefore( document.createTextNode( ' | ' ), loc.nextSibling );

} if ( window.addEventListener ) window.addEventListener( 'load', addClassicLinkToTopbar, false ); else if ( window.attachEvent ) window.attachEvent( 'onload', addClassicLinkToTopbar );

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

    // century without AD,BC etc
    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');
    // century with AD,BC etc
    txt.value = txt.value.replace(/\[\[(\d{1,2}(?:st|nd|rd|th))[ \-]century\s(AD|BC|CE|BCE)\]\]/gi, '$1 century $2');
    txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]century\s(AD|BC|CE|BCE)\]\]/gi, '$1 century $2');
    txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]centuries\s(AD|BC|CE|BCE)\]\]/gi, '$1 centuries $2');

    // piped decades and years
    txt.value = txt.value.replace(/\[\[(\d{1,4}\'?s)\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,4}s? (?:AD|BC|CE|BCE)\|(\d{1,4})\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,4}s? (?:AD|BC|CE|BCE)\|(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,4}s?\|(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
    txt.value = txt.value.replace(/\[\[\d{1,4}s?\|(\d{1,2}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');

    //month+year
    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\]\]');
    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) \d{1,2}\|((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\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 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?)\]\]/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');

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

    //year: examine characters in link on left for date, examine characters in link on right for date
    txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^yhletr\d])\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/gi, '$1$2$3');
    //year pair: examine characters in link on left for date, examine characters in link on right for date
    txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^yhletr\d])\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc\s\-]))/gi, '$1$2$3$4$5');

    //year: examine characters in link on left for date, avoid links on right
    txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^yhletr\d])\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3');
    //year pair: examine characters in link on left for date, avoid links on right
    txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^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, examine characters in link on right for date
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/gi, '$1$2$3');
    //year pair: avoid links on left, examine characters in link on right for date
    txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/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:text on left, text on right
    txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3');
    //year pair: avoid links on left, text on right
    txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3$4$5');

    //year:avoid links on left, hyphen but no digits (to avoid ISO date) in link on right. Currently suspended because it isn't fully tested.
    //txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^\d])/gi, '$1$2$3');

    //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');

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


    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    //Made summary more accurate: not all links are removed. Keep summary brief
    var summary = "Date links";
	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()
}