Jump to content

User:Smalleditor/dates.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.
//<nowiki>
importScript("User:Smalleditor/1exec1engine.js");

function ohc_fix_unambiguous_dates()
{
    // resolvable ambiguous date formats
    regex(/((?:at|from|till):\s*)(\d\d)/gi, '$1♫$2'); //protect for timeline

    // UK style
    ohc_regex(/([^\w\d\/-:♫])@DD[\/\.]@MM[\/\.]@YYNN(?![-–\d\/])/gi, "$1@Day @Month @YYYY", function(d,m,y) {
        if (d == m) return true;
        if (d > 12) return true;
        return false;
        });

    // US style
    ohc_regex(/([^\w\d\/-:♫])@MM[\/\.]@DD[\/\.]@YYNN(?![-–\d\/])/gi, "$1@Month @Day, @YYYY", function(d,m,y) {
        if (d > 12) return true;
        return false;
        });
}

function ohc_US_slash_dates_to_mdy()
{
    // resolvable ambiguous date formats
    ohc_regex(/([^\w\d\/-:♫])@MM[\/\.-]@DD[\/\.-]@YYNN(?![-–\d\/])/gi, '$1@Month @Day, @YYYY');
}

function ohc_UK_slash_dates_to_dmy()
{
    // resolvable ambiguous date formats
    ohc_regex(/([^\w\d\/-:♫])@DD[\/\.-]@MM[\/\.-]@YYNN(?![-–\d\/])/gi, '$1@Day @Month @YYYY');
}

function ohc_remove_leading_zeroes()
{
    ohc_regex(/([^\d][^\w\d\/])@Month\s@ZD@th?,?\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day, @LYear");
    ohc_regex(/([^\d][^\w\d\/])@Month\s@ZD@th?(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day");
    ohc_regex(/([^\d][^\w\d\/])@ZD@th?\s@Month\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@Day @LMonth @LYear");
    ohc_regex(/([^\d][^\w\d\/])@ZD@th?\s@Month(?=[^\w\d][^\d]|\b)/gi, "$1@Day @LMonth");
}

function ohc_delink_dates()
{
    //delink piped md|dm, md|dd and my|m dates	
    ohc_regex(/\[\[@month @dd\|@DD @Month\]\]/gi, "@LDay @LMonth");
    ohc_regex(/\[\[@month @dd\|@DD\]\]/gi, "@LDay");
    ohc_regex(/\[\[@month @yyyy\|@Month\]\]/gi, "@LMonth");

    //delink ISO8601 dates
    ohc_regex(/\[\[@YYYY(?:\]\]-\[\[|-)@MM-@DD\]\]/gi, "@YYYY-@MM-@DD");
    ohc_regex(/@YYYY-\[\[@MM-@DD\]\]/gi, "@YYYY-@MM-@DD");

    //delink full dates
    ohc_regex(/\[\[@Day[\s_](?:of[\s_])?@Month(?:\]\]\s?\[\[\| )@YYYY\]\]/gi, "@Day @LMonth @YYYY");
    ohc_regex(/\[\[@Month (?:the\s)?@Day(?:\]\],? \[\[|, )@YYYY\]\]/gi, "@LMonth @Day, @YYYY");

    //delink single dm or 'dth the m'
    ohc_regex(/(?:the\s)?\[\[@Day[\s_](?:of[\s_])?@Month\]\]/gi, "@Day @LMonth");
    //delink single md or 'm the dth'
    ohc_regex(/\[\[@Month[\s_](?:the[\s_])?@Day\]\](?=[^\w\d])/gi, "@LMonth @Day");

    //Month+day_number "[[March 7]]th" -> "March 7"
    ohc_regex(/\[\[@Month\s@DD\]\]@th/gi, "@LMonth @Day");

    //month+day+year piped pseudo-ISO dates
    ohc_regex(/\[\[@Month @DD\|@dd-@dd\]\]-(?:\[\[)?@YYYY(?:\|@yyyy)?(?:\]\])/gi, "@Day @Month @YYYY");
    ohc_regex(/\[\[@DD\s@Month\|@dd-@dd\]\]-(?:\[\[)?@YYYY(\]\])?/gi, "@Day @Month @YYYY");

    //month+day piped
    ohc_regex(/(?:the\s)?\[\[@day[\s_](?:of[\s_])?@month\|([^\]]{1,30})\]\]/gi, "$1");

    //'[[month day|xxXxx]]Xyyyy ' to 'month day, year'
    ohc_regex(/\[\[@Month\s@Day\|@dd.@dd\]\].@YYYY/gi, "@LMonth @Day, @YYYY");
     //'[[day month|xxXxx]]Xyyyy' to 'day month year'
    ohc_regex(/\[\[@Day\s@Month\|@dd.@dd\]\].@YYYY/gi, "@Day @LMonth @YYYY");

    // century
    regex(/\[\[((?:first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|sixteenth|seventeenth|eighteenth|nineteenth|twentieth|twenty(?:\s|-)first(?:\s|-))(century|centuries)(\sAD|\sBC|\sCE|\sBCE|))\]\]/gi, '$1');
    regex(/\[\[(?:first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|sixteenth|seventeenth|eighteenth|nineteenth|twentieth|twenty(?:\s|-)first)[\s-_](?:century|centuries)(?:\sAD|\sBC|\sCE|\sBCE|)\|([^\]]{1,30})\]\]/gi, '$1');
    regex(/\[\[(\d{1,2}(?:st|nd|rd|th)[\s-_])(century|centuries)(\sAD|\sBC|\sCE|\sBCE|)\]\]/gi, '$1$2$3');
    regex(/\[\[\d{1,2}(?:st|nd|rd|th)[\s-_](?:century|centuries)(?:\sAD|\sBC|\sCE|\sBCE|)\|([^\]]{1,30})\]\]/gi, '$1');

    // months
    ohc_regex(/\[\[@Month\]\]/gi, "@Month");
    ohc_regex(/\[\[@FullMonth\|([^\]]{1,30})\]\]/gi, "$1");

    // decades and years
    regex(/\[\[(\d{1,3}0)\'?s\]\]/g, '$1s');
    regex(/\[\[\d{1,3}0\'?s?\|([^\]]{1,30})\]\]/g, '$1');
    regex(/\[\[(\d{1,3}0)\'?(s)?\s(AD|BC|CE|BCE)\]\]/gi, '$1$2 $3');
    regex(/\[\[(\d{1,4}[\s_]?)(AD|BC|CE|BCE)\]\]/gi, '$1$2');
    regex(/\[\[(AD|BC|CE|BCE)([\s_]?)(\d{1,4})\]\]/gi, '$1$2$3');
    regex(/\[\[([12]\d{3}|\d{1,3})\]\]/gi, '$1');
    regex(/\[\[\d{1,3}0\'?s?\s(?:AD|BC|CE|BCE)\|([^\]]{1,30})\]\]/gi, '$1');
    regex(/\[\[\d{1,3}0\'?s?\s\(decade\)\|([^\]]{1,30})\]\]/gi, '$1');

    //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
    ohc_regex(/\[\[@yyyy#[^\|\]]+\|(@month\s@yyyy)\]\]/gi, "$1");
    ohc_regex(/\[\[(@month\s@yyyy)\]\]/gi, "$1");
    ohc_regex(/\[\[@month\s@yyyy\|([^\]]{1,30})\]\]/gi, "$1");

    //removed piped years when in full date
    ohc_regex(/\[\[@DD\s@Month\]\],?\s?\[\[[^\|\]]{1,32}\|@Year\]\]/gi, '@DD @LMonth @Year');
    ohc_regex(/\[\[@Month\s@DD\]\],?\s?\[\[[^\|\]]{1,32}\|@Year\]\]/gi, '@LMonth @DD, @Year');

    //Identify surprise or 'Easter egg' diversions linking months to year articles.
    ohc_regex(/\[\[\d{1,4}#[^\|\]]+\|@Month\]\]/gi, "@LMonth");

    // month and day piped
    ohc_regex(/@YYYY-\[\[@month[\s_]@day\|@MM-@DD\]\]/gi, "@YYYY-@MM-@DD");
    ohc_regex(/\[\[@month[\s_]@day\|([^\]]{1,30})\]\]/gi, "$1");
    ohc_regex(/\[\[@day[\s_](?:st|nd|rd|th|)[\s_](?:of[\s_])?@month\|([^\]]{1,30})\]\]/gi, "$1");
//    ohc_regex(/\[\[@Day\]\]/gi, "@Day");

    //years piped
    regex(/\[\[\d{1,4}\|([^\]]{1,30})\]\]/gi, '$1');
}

function ohc_fix_common_errors()
{
    //insert comma to separate date from army unit
    ohc_regex(/@FullMonth (the \d\d*@th (?:Armou?red|Artillery|Battalion|Brigade|Company|Group|Infantry|Regiment|Squadron))/gi, "@Month, $1");

    // remove nowrap template from dm and md dates
    ohc_regex(/(date=)\{\{nowrap\|(@dd(?:_|&nbsp;)@month)\}\}/gi, '$1$2');
    ohc_regex(/(date=)\{\{nowrap\|(@month(?:_|&nbsp;)@dd)\}\}/gi, '$1$2');

    //fix bda template redirect
    regex(/{{bda\|([^}]+)}}/gi, '{{birth date and age|$1}}');
    regex(/{{dda\|([^}]+)}}/gi, '{{death date and age|$1}}');

    //zap redundand {{date}}, {{accessdate}} and {{#dateformat}}
    ohc_regex(/\{\{date\|@YYYY-@MM-@DD\}\}/gi, "@Day @Month @YYYY");
    regex(/\{\{date\|([^\}\|\-]+)(?:\|(?:[dmy]{3}|iso|none))?\}\}/gi, '$1');
    ohc_regex(/[ ]*\{\{accessdate\|@YYYY-@MM-@DD[ ]*\}\}/gi, " Retrieved @YYYY-@MM-@DD");
    regex(/\{\{#dateformat:♫?([^\}\|]+)(?:\|dmy|\|mdy)?}}/gi,'$1');

    //underscore and nbsp in linked dates
    ohc_regex(/\[\[@DD(?:_|&nbsp;)@Month\]\]/gi, "@Day @LMonth");
    ohc_regex(/\[\[@Month(?:_|&nbsp;)@DD\]\]/gi, "@LMonth @Day");

    //remove leading zero from linked date
    ohc_regex(/\[\[@Month(?:_|&nbsp;)@ZD@th\]\]/gi, "@LMonth @Day");
    ohc_regex(/\[\[@ZD@th(?:_|&nbsp;)@Month\]\]/gi, "@Day @LMonth");

    //remove unlinked ordinal date formats
    ohc_regex(/([^\w\d])@Day, @Day(\s(?:and|&|to|or)\s)@Day(?:\sof\s?)?(?:_|&nbsp;)@Month(?=[^\w\d])/gi, '$1@SD1, @SD2$2@SD3 @LMonth');
    ohc_regex(/([^\w\d])@Day(\s(?:and|&|to|or)\s)@Day(?:\sof\s?)?(?:_|&nbsp;)@Month(?=[^\w\d])/gi, '$1@SD1$2@SD2 @LMonth');
    ohc_regex(/([^\w\d])@Day(?:\sof\s?)(?:_|&nbsp;)@Month(?=[^\|\]\w\d])/gi, '$1@SD @LMonth');
//    ohc_regex(/([^\w\d])@Day(?:\sof\s?)?(?:_|&nbsp;)@Month(?=[^\w\d])/gi, '$1@SD @LMonth'); //seemingly redundant line after rationalising of magic strings and brackets
    ohc_regex(/([^\w\d])@DD<sup>@th?<\/sup>(?:\sof\s?)?\s(@Month)(?=[^\w\d])/gi, '$1@SD @LMonth');
    ohc_regex(/([^\w\d])@Month\s(?:the\s)?@Day,(?:\sthe\s)?(?:[ _]|&nbsp;)@Day(\s(?:and|&|to|or)(?:[ _]|&nbsp;))(?:the\s)?@Day(?=[^\]\|\w\d][^\d])/gi, '$1@LMonth @Day1, @Day2$2@Day3');
    ohc_regex(/([^\w\d])(@month)\s(@day)((?:,\s@day){0,6}),?(\/|\s?[-–]\s?|\s(?:and|&|to|or)\s+?)@Day(?:,?(?:[ _]|&nbsp;)|\sof\s)?(\d{3,4}[^\w\d][^\d])/gi, '$1$2 $3$4$5@SD, $6');
    ohc_regex(/([^\w\d])@Month(?:_|&nbsp;)(?:the\s)?@Day(?=[^\]\|\w\d][^\d])/gi, '$1@LMonth @DD');
    ohc_regex(/([^\w\d])@Month(?:_|&nbsp;)(?:the\s)?@DD<sup>@th<\/sup>(?=[^\]\|\w\d][^\d])/gi, '$1@LMonth @DD');

    // eliminating dd-mm-yyyy dates
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@DD(?:\]\]-\[\[|-)@MM-@YYYY(?:\]\])?/gi, "$1@Day @LMonth @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@DD(?:\]\]-\[\[|-)@Mon-@YYYY(?:\]\])?/gi, "$1@Day @LMonth @YYYY");

    // spaces and commas between month and year
    ohc_regex(/@Month(?:,\s+|\s+of\s+)@YYYY([\w\d])/gi, "@LMonth @YYYY$1");

    // fix month names (capitalize, remove dots)
    ohc_regex(/\b@DD\s@FullMonth(\s+|\s*,)/gi, "@Day @FullMonth$1");
    ohc_regex(/\b@DD\s@Mon(\s+|\s*,)/gi, "@Day @Mon$1");
    ohc_regex(/\b(\s)@FullMonth\s@DD(\s+|\s*,)/gi, "$1@FullMonth @Day$2");
    ohc_regex(/\b(\s)@Mon\s@DD(\s+|\s*,)/gi, "$1@Mon @Day$2");
    regex(/(\d)[ ]?(?:-|–|&ndash;)[ ]?Present\b/g, '$1 – present');
	regex(/(\d(?:st|nd|rd|th) )C(entur(?:ies|y))(?=[ ]*(=|BC|AD|CE))/g, '$1c$2');
	regex(/(\d(?:st|nd|rd|th) )C(entur(?:ies|y))(?![ -]*(?:Fox|[A-Z]\w*))/g, '$1c$2');


    //spaces and commas - in date formats
    ohc_regex(/([^\d][^\w\d]\[?\[?@dd)[ ]+(@month)(\]\])?[ ]*,[ ]*(\[?\[?@yyyy(\sAD|\sBC|\sCE|\sBCE|)\]?\]?[^\w\d][^\d])/gi, "$1 $2$3 $4");
    ohc_regex(/([^\d][^\w\d]\[?\[?)(@month)[ ]+(@dd)(\]?\]?)(?:[ ]*,[ ]*)(\[?\[?@yyyy(\sAD|\sBC|\sCE|\sBCE|)\]?\]?[^\w\d][^\d])/gi, "$1$2 $3$4, $5");
    ohc_regex(/([^\d][^\w\d]\[?\[?)(@month)[ ]+(@dd)(\]?\]?)[ ]+(\[?\[?@yyyy(\sAD|\sBC|\sCE|\sBCE|)\]?\]?[^\w\d][^\d])/gi, "$1$2 $3$4, $5");

    ohc_regex(/(@month)(?:, | of )(@yyyy[\w\d])/gi, "$1 $2"); //repeat after delinking

    // spaces and commas - md-md and dm-dm date ranges
    ohc_regex(/([^\d][^\w\d])@Month\s@DD(?:\s*[-–]\s*)@Month\s@DD(?=[^\w\d])/gi, "$1@LMonth1 @Day1 – @LMonth2 @Day2");
    ohc_regex(/([^\d][^\w\d])@DD\s@Month(?:\s*[-–]\s*)@DD\s@Month(?=[^\w\d])/gi, "$1@Day1 @LMonth1 – @Day2 @LMonth2");

    // spaces and commas - d-dm and md-d date ranges
    ohc_regex(/([^\d][^\w\d])@Month\s@DD(?:\s*[-–]\s*)@DD(?=[^\w\d])/gi, "$1@LMonth1 @Day1–@Day2");
    ohc_regex(/([^\d][^\w\d])@DD(?:\s*[-–]\s*)@DD\s@Month(?=[^\w\d])/gi, "$1@Day1–@Day2 @LMonth1");

    //reformat dmy-dmy and mdy-mdy (birth-death ranges)
    ohc_regex(/([^\d][^\w\d])@DD\s@Month,? @Year(?:(?:&nbsp;|\s)?(?:[-–—]|&ndash;)(?:&nbsp;|\s)?)@DD\s@Month,? @Year(?=[^\d][^\w\d])/gi, "$1@Day1 @LMonth1 @Year1 – @Day2 @LMonth2 @Year2");
    ohc_regex(/([^\d][^\w\d])@Month\s@DD,? @Year(?:(?:&nbsp;|\s)?(?:[-–—]|&ndash;)(?:&nbsp;|\s)?)@Month\s@DD,? @Year(?=[^\d][^\w\d])/gi, "$1@LMonth1 @Day1, @Year1 – @LMonth2 @Day2, @Year2");

    //nbsp in publication archive & access dates
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*@dd)&nbsp;(@month)/gi, '$1 $2');
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(@month)&nbsp;(@dd)(, @yyyy|)(?=[ ]*[\|\}])/gi, '$1$2 $3$4');

    ohc_regex(/([ =]+)@DD-@Mon-@YYYY(?![-–\d\/])/gi, "$1@Day @LMonth @YYYY");
    ohc_regex(/([ =]+)@DD-@MM-@YYYY(?![-–\d\/])/gi, "$1@Day @LMonth @YYYY");

    //remove leading zero from 'Retrieved' date
    ohc_regex(/(?:Retrieved|Accessed)(?: on |:? )@Month\s@ZD,?\s@YYYY/gi, "Retrieved @LMonth @Day, @YYYY");
    ohc_regex(/(?:Retrieved|Accessed)(?: on |:? )@ZD\s@Month\s@YYYY/gi, "Retrieved @Day @LMonth @YYYY");

    ohc_regex(/(?:Retrieved|Accessed)(?: on |:? )@Month\s@ZD,?/gi, "Retrieved @LMonth @Day");
    ohc_regex(/(?:Retrieved|Accessed)(?: on |:? )@ZD\s@Month/gi, "Retrieved @Day @LMonth");

    // spaces and commas between month and year (again)
    ohc_regex(/@Month(?:,\s+|\s+of\s+)@YYYY([\w\d])/gi, "@LMonth @YYYY$1");

}

/** ------------------------------------------------------------------------- */
/// ISO to long format

function ohc_ISO_to_dmy_in_citations()
{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=[^\w\d][^\d])/gi, "$1@Day @Month @YYYY");
}

function ohc_ISO_to_mdy_in_citations()
{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=[^\w\d][^\d])/gi, "$1@Month @Day, @YYYY");
}

function ohc_ISO_to_dmy_in_references()
{
    ohc_regex(/(<\s?ref.*?>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1$2@Day @Month @YYYY$3");
    ohc_regex(/(\()@YYYY[-–]@MM[-–]@DD(\))/gi, '$1@Day @Month @YYYY$2');
}

function ohc_ISO_to_mdy_in_references()
{
    ohc_regex(/(<\s?ref.*?>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1$2@Month @Day, @YYYY$3");
    ohc_regex(/(\()@YYYY[-–]@MM[-–]@DD(\))/gi, '$1@Month @Day, @YYYY$2');
}

// currently unused
function ohc_ISO_to_dmy_anywhere()
{
    ohc_regex(/([^\w\d\/-])@YYYY[-–]@MM[-–]@DD([^\w\d\/-])/gi, '$1@Day @Month @YYYY$2');
}

// currently unused
function ohc_ISO_to_mdy_anywhere()
{
    ohc_regex(/([^\w\d\/-])@YYYY[-–]@MM[-–]@DD([^\w\d\/-])/gi, '$1@Month @Day, @YYYY$2');
}

function ohc_delink_ISO_to_dmy()
{
//    ohc_regex(/\{\{date\|(@yyyy-@zm-@zd)(?:\|dmy|)\}\}/gi, "$1"); //Template:date converts to dmy by default
    regex(/(\|[ ]*accessdate[ ]*=)[ ]*([\d\w])/gi, '$1$2');
    ohc_ISO_to_dmy_in_citations();
    ohc_ISO_to_dmy_in_references();
    regex(/([^\d\w\/\-%,])@YYYY-@MM-@DD(<\s?\/ref.*?>)/g, '$1@Day @Month @Year$2');
}
 
function ohc_delink_ISO_to_mdy()
{ 
    regex(/(\|[ ]*accessdate[ ]*=)[ ]*([\d\w])/gi, '$1$2');
    ohc_ISO_to_mdy_in_citations();
    ohc_ISO_to_mdy_in_references();
    regex(/([^\d\w\/\-%,])@YYYY-@MM-@DD(<\s?\/ref.*?>)/g, '$1@Month @Day, @Year$2');
}

function ohc_delink_year_in_X()
{
    ohc_regex(/{{(?:avyear|by|baseball\syear|fy|ly|mlby|mlb\syear|scy|sdy)\|(@year)}}/gi, "$1");
    ohc_regex(/{{(?:avyear|by|baseball\syear|fy|ly|mlby|mlb\syear|scy|sdy)\|@year\|(@year)}}/gi, "$1");
    regex(/\[\[\d{1,4}\sin\s[^\|\]]+\|(\d{4})\]\]/gi, '$1');
//  regex(/\[\[(?:\d{4}.\d{2} in English football)\|([^\]]{1,30})\]\]/gi, '$1'); //temp disable to work on English football articles

    regex(/\[\[\d{4} Major League Baseball season\|(\d{4})(\sseason)?\]\]/gi, '$1$2');
}


// format parameter must be 'dmy' or 'mdy'
function ohc_fix_dts_template(format)
{
    // dts - format=dmy in this case (per D12000)
    // per {{dts}} doc, remove the obsolete link=off param
    regex(/(\{\{dts[^}]*)(?:\|link=off)/gi, '$1');
    // clean up existing format...
    regex(/(\{\{dts[^}]*)(?:\|format=(dmy|mdy))/gi, '$1');
    // then ensure format=dmy is applied if year is first
    regex(/(\{\{dts[^|]*\|)(?=\d{4}[-\|])/gi, '$1format=' + format + '|');
}

// format parameter must be 'dmy' or 'mdy'
function ohc_fix_gr_template(format)
{
    // GR - date formatting parameter
    // per {{GR}} doc, insert date formatting param
    regex(/(\{\{GR\|\d\d?)(?!\|date)/gi, '$1|dateform=' + format);
}

function ohc_dates_to_dmy()
{
    //change start and end templates
    regex(/(\{\{(start\sdate|end\sdate)[^|}]*\|)df=ye?s?\|([^}]*df=ye?s?)/gi, '$1$2');

    //change deprecated birth and death templates
    regex(/(\{\{(?:Birth|Death|launch|release|start|end)[^}]*)\|mf=ye?s?/gi, '$1'); //rem pipe symbol to treat cases where mf is at the end
    regex(/(\{\{(?:Birth|Death|launch|release|start|end|film date|Wayback)[^|}]*\|)/gi, '$1df=yes|');
    regex(/(\{\{(?:Birth|Death|launch|release|start|end|film date|Wayback)[^|}]*\|)df=ye?s?\|([^}]*df=ye?s?)/gi, '$1$2');

    //convert date ranges (md,d,d-dy to d,d,d-dmy; md,d,d-d to d,d,d-dm; md,d,d to d,d,dm)
    ohc_regex(/([^\d][^\w\d])@Month((?:\s@day?,?){1,6}),?(\/|\s?(?:[-–]|&ndash;)\s?|(?:[ _]|&nbsp;)(?:and|&|to|or)(?:[ _]|&nbsp;)+?)@Day,?\s(?:of\s)?([12]\d{3}[^\w\d][^\d])/gi, "$1$2$3@Day @LMonth $4");
    ohc_regex(/([^\d][^\w\d])@Month((?:\s@day?,?){1,6}),?(\/|\s?(?:[-–]|&ndash;)\s?|(?:[ _]|&nbsp;)(?:and|&|to|or)(?:[ _]|&nbsp;)+?)@Day(?=[^\w\d][^\d]|\b)/gi, "$1$2$3@Day @LMonth");
    ohc_regex(/([^\d][^\w\d])@Month((?:\s@day?,?){1,6}),? @Day(?=[^\w\d][^\d]|\b)/gi, "$1$2, @Day @LMonth");

    //'md, md,' to 'dm, dm,'
    ohc_regex(/([^\d][^\w\d])@Month @Day, @Month @Day(?=[,\.][^\w\d][^\d])/gi, "$1@Day1 @LMonth1, @Day2 @LMonth2");

    //convert simple mdy dates to dmy
    ohc_regex(/([^\d][^\w\d])@Month\s@Day,?\s@Year(\s?(?:AD|BC|CE|BCE)[^\w\d][^\d])/gi, "$1@Day @LMonth @Year$2");
    ohc_regex(/([^\d][^\w\d])@Month\s@Day,?\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@Day @LMonth @Year");
    ohc_regex(/([^\d][^\w\d])@Month\s@Day(?=[^\w\d][^\d]|\b)/gi, "$1@Day @LMonth");

    //Month+day_number " 7th March" -> "7 March"
    ohc_regex(/([^\d][^\w\d])@Day\s@Month\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@DD @LMonth @Year");
    ohc_regex(/([^\d][^\w\d])@Day\s@Month(?=[^\w\d][^\d]|\b)/gi, "$1@DD @LMonth");

    //remove comma
    ohc_regex(/@Day\s@Month,\s?@Year(?=[^\w\d])/gi, "@Day @LMonth @Year");

    //	remove "the" from "on the dd month"
    ohc_regex(/(on |by )the @Day\s@Month(?=[ ,\)\.])/gi, "$1@Day @LMonth");
}

function ohc_dates_to_mdy()
{
    //change deprecated birth and death templates
    regex(/({{(?:Birth|Death|release|start|end)[^}]*)\|df=ye?s?/gi, '$1');
    regex(/({{(?:Birth|Death|release|start|end)[^|]*\|)mf=ye?s?\|([^}]*mf=ye?s?)/gi, '$1$2');

    //convert date ranges (d,d,d-dmy to md,d,d-dy; d,d,d-dm to md,d,d-d; dm,d,d to md,d,d)
    ohc_regex(/([^\d][^\w\d])@Day((?:, @day){0,5})(\/|\s?(?:[-–]|&ndash;)\s?|\s(?:and|&|to|or)\s+?)@Day,?\s@Month,? @YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day1$2$3@Day2, @YYYY");
    ohc_regex(/([^\d][^\w\d])@Day((?:, @day){0,5})(\/|\s?(?:[-–]|&ndash;)\s?|\s(?:and|&|to|or)\s+?)@Day,?\s@Month(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day1$2$3@Day2");
    ohc_regex(/([^\d][^\w\d])@Day((?:, @day){0,5})\s@Month(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day1$2,");

    //'dm, dm,' to 'md, md,'
    ohc_regex(/([^\d][^\w\d])@Day @Month, @Day @Month(?=[,\.]?[^\w\d][^\d])/gi, "$1@LMonth1 @Day1, @LMonth2 @Day2");

    //convert simple dmy dates to mdy
    ohc_regex(/([^\d][^\w\d])@Day[\s]{1,3}@Month,?\s@Year(\s?(?:AD|BC|CE|BCE)[^\w\d][^\d])/gi, "$1@LMonth @Day, @Year$2");
    ohc_regex(/([^\d][^\w\d])@Day[\s]{1,3}@Month,?\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day, @Year");
    ohc_regex(/([^\d][^\w\d])@Day[\s]{1,3}@Month(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @Day");

    //Month+day_number "March 7th" -> "March 7"
    ohc_regex(/([^\d][^\w\d])@Month\s@Day,?\s@YYYY(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @SD, @YYYY");
    ohc_regex(/([^\d][^\w\d])@Month\s@Day(?=[^\w\d][^\d]|\b)/gi, "$1@LMonth @SD");

    //add comma where missing
    ohc_regex(/([^\d][^\w\d])@Month\s@Day(?=\s@yyyy[^\w\d][^\d])/gi, "$1@LMonth @Day,");
}

function ohc_fix_dmy_redundancies()
{
    //month-range redundancies (repeating month names)
    ohc_regex(/([\D]@dd) @Month[ ]{1,2}((?:to|and|-|–)[ ]{1,2}@dd) @Month/, "$1 $2 @LMonth", function(d1, d2) {
        if (d1.m == d2.m) return true;
        return false;
    });
}
//not currently used
function ohc_fix_mdy_redundancies()
{
    //month-range redundancies (repeating month names)
    ohc_regex(/(@Month[ ]{1,2}@dd) (to|and|-|–)[ ]{1,2}@Month[ ]{1,2}@DD/, "$1 $2 @LDay", function(d1, d2) {
        if (d1.m == d2.m) return true;
        return false;
    });
}

function ohc_expand_ref_dates()

{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Day @Month @YYYY(?=[ ]*[\|\}])/gi, "$1@SD @FullMonth @YYYY");
    ohc_regex(/(<\s?ref.*?>[^<]+?[\s\(])@Day @Month @YYYY([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1@SD @FullMonth @YYYY$2");

    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Month @Day, @YYYY(?=[ ]*[\|\}])/gi, "$1@FullMonth @SD, @YYYY");
    ohc_regex(/(<\s?ref.*?>[^<]+?[\s\(])@Month @Day, @YYYY([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1@FullMonth @SD, @YYYY$2");
}
 
function ohc_expand_all_dates()
{
    ohc_regex(/([^\w\d\/-])@Day @Month @YYYY([^\w\d\/-])/gi, '$1@SD @FullMonth @YYYY$2');
    ohc_regex(/([^\w\d\/-])@Month @Day, @YYYY([^\w\d\/-])/gi, '$1@FullMonth @SD, @YYYY$2');
}

function ohc_abbrev_ref_dates()

{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Day @Month @YYYY(?=[ ]*[\|\}])/gi, "$1@SD @Mon @YYYY");
    ohc_regex(/(<\s?ref.*?>[^<]+?[\s\(])@Day @Month @YYYY([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1@SD @Mon @YYYY$2");

    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Month @Day, @YYYY(?=[ ]*[\|\}])/gi, "$1@Mon @SD, @YYYY");
    ohc_regex(/(<\s?ref.*?>[^<]+?[\s\(])@Month @Day, @YYYY([^\d\w\/\-%,<]*?<\s?\/ref>)/gi, "$1@Mon @SD, @YYYY$2");
}
 
//not currently used
function ohc_abbrev_all_dates()
{
    ohc_regex(/([^\w\d\/-])@Day @Month @YYYY(?=[^\w\d\/-])/gi, '$1@SD @Mon @YYYY');
    ohc_regex(/([^\w\d\/-])@Month @Day, @YYYY(?=[^\w\d\/-])/gi, '$1@Mon @SD, @YYYY');
}
 
//not currently used
function ohc_publication_dates()
{
    ohc_regex(/(\|[ ]*(?:date|archivedate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=[^\w\d][^\d])/gi, "$1@Day @Month @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=[^\w\d][^\d])/gi, "$1@Month @Day, @YYYY");
}

/** ------------------------------------------------------------------------ **/
/// EDIT SUMMARIES & USE DMY TEMPLATE

// format parameter must be 'dmy' or 'mdy'
function ohc_use_dates_template(format)
{
    var txt=document.editform.wpTextbox1;
    // current month-year
    var currentDate = new Date();
    var currmonth = currentDate.getMonth();
    var curryear  = currentDate.getFullYear();
    var myMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var curryyyymm = myMonths[currmonth] + ' ' + curryear;

    // insert the uses dates template
    regex(/{{(use (dmy|mdy) dates|dmy|mdy)[ ]*(\|[^}]*)?}}/gi, '{{Use '+format+' dates|date='+curryyyymm+'}}');

    var rxpdd = new RegExp("{{Use "+format+" dates\\|date="+curryyyymm+"}}", "gi");
    var dflagfound = txt.value.search(rxpdd);
    if (dflagfound == -1)
    {
        txt.value='{{Use '+format+' dates|date='+curryyyymm+'}}\r\n'+txt.value;
    }
}

function ohc_protect_dates()
{
    // protects specific dates 
// retraining 
   regex(/(\[\[May\s1968)(\]\])/gi, '$1 in France|May 1968$2');
    // album names
    regex(/(4th\sof)( July, Asbury)/gi, '$1♫$2');
    regex(/((?:\[\[|\'\')June)( 1, 1974)(?=[\]\'])/gi, '$1♫$2');
    regex(/((?:\[\[|\'\')July)( 15, 1972)(?=[\]\'])/gi, '$1♫$2');
    regex(/(Les Bains Douches 18)( December 1979)/gi, '$1♫$2');
    regex(/(Live \(at Raji's June)( 12\))/gi, '$1♫$2');
    regex(/(August)( 7, 4:15)/gi, '$1♫$2');
    regex(/(In Concert, Zürich, October)( 28, 1979)/gi, '$1♫$2');
    regex(/(CBGB OMFUG Masters: Live June)( 29, 2001)/gi, '$1♫$2');
    ohc_regex(/(Live at [^\|\]]* [1-3]\d)( @fullmonth)/gi, "$1♫$2");
    ohc_regex(/(Live at [^\|\]]* @fullmonth)( \d\d?)/gi, "$1♫$2");

}

function ohc_unprotect_dates()
{
	//unprotect all dates
    regex(/[♫]/g, '');

}

function ohc_MOSNUM_edit_summary()
{
    //Add a tag to the summary box
    setoptions(minor='true'); 		
    setreason('date formats per [[WP:MOSNUM]] by [[WP:MOSNUMscript|script]]', 'append');
    doaction('diff');
}

function ohc_slash_edit_summary()
{
    //Add a tag to the summary box
    setoptions(minor='true'); 		
    setreason('resolved [[WP:MOSNUM|ambiguous date formats]]', 'append');
    doaction('diff');
}

function ohc_expand_edit_summary()
{
    //Add a tag to the summary box
    setoptions(minor='true'); 		
    setreason('expand month names', 'append');
    doaction('diff');
}

function ohc_abbrev_edit_summary()
{ 
    //Add a tag to the summary box
    setoptions(minor='true'); 		
    setreason('abbrev month names', 'append');
    doaction('diff');
}

/** ------------------------------------------------------------------------ **/
/// CUSTOMIZATION POINTS
function ohc_customize_all_to_dmy() {}
function ohc_customize_all_to_mdy() {}
function ohc_customize_body_to_dmy() {
	ohc_regex(/(\|date ?= ?)@YYYY-@MM-@DD( ?\|accessdate= ?)(2012)-05-@DD/gi, '$1@Day @Month @YYYY$2 @SD2 May $3');
	ohc_regex(/(?:\|date ?= ?|)( ?\|accessdate= ?)(2012)-05-@DD/gi, '$1 @SD May $2');
	ohc_regex(/\(@YYYY-@MM-@DD(\)\. Retrieved )(?:on |)(2012)-05-@DD/gi, '(@Day @Month @YYYY$1@SD2 May $2');
	ohc_regex(/(Retrieved )(?:on |)(2012)-05-@DD/gi, '$1@SD May $2');
	ohc_regex(/\(@YYYY-@MM-@DD(\)\. )/gi, '(@Day @Month @YYYY$1');
}
function ohc_customize_body_to_mdy() {
	ohc_regex(/(\|date ?= ?)@YYYY-@MM-@DD( ?\|accessdate= ?)(2012)-05-@DD/gi, '$1@Month @Day, @YYYY$2 May @SD2, $3');
	ohc_regex(/(?:\|date ?= ?|)( ?\|accessdate= ?)(2012)-05-@DD/gi, '$1 May @SD, $2');
	ohc_regex(/\(@YYYY-@MM-@DD(\)\. Retrieved )(?:on |)(2012)-05-@DD/gi, '(@Month @Day, @YYYY$1April @SD2, $2');
	ohc_regex(/(Retrieved )(?:on |)(2012)-05-@DD/gi, '$1May @SD, $2');
	ohc_regex(/\(@YYYY-@MM-@DD(\)\. )/gi, '(@Month @Day, @YYYY$1');
}
function ohc_customize_ISO_to_dmy() {}
function ohc_customize_ISO_to_mdy() {}

/** ------------------------------------------------------------------------ **/
/// DRIVER FUNCTIONS

function ohc_body_dates_to_dmy()
{
    ohc_use_dates_template('dmy');

    ohc_fix_common_errors();
    ohc_fix_dts_template('dmy');
    ohc_fix_gr_template('dmy');
    ohc_delink_dates();
    ohc_dates_to_dmy();
    ohc_remove_leading_zeroes();
    ohc_fix_dmy_redundancies();
}

function ohc_body_dates_to_mdy()
{
    ohc_use_dates_template('mdy');

    ohc_fix_common_errors();
    ohc_fix_dts_template('mdy');
    ohc_fix_gr_template('mdy');
    ohc_delink_dates();
    ohc_dates_to_mdy();
    ohc_remove_leading_zeroes();
    ohc_fix_mdy_redundancies();
}


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

function ohc_all_to_dmy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_fix_unambiguous_dates();
    ohc_body_dates_to_dmy();
    ohc_delink_ISO_to_dmy();
    ohc_delink_year_in_X();
    ohc_customize_all_to_dmy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_MOSNUM_edit_summary();
}

function ohc_all_to_mdy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_fix_unambiguous_dates();
    ohc_body_dates_to_mdy();
    ohc_delink_ISO_to_mdy();
    ohc_delink_year_in_X();
    ohc_customize_all_to_mdy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_MOSNUM_edit_summary();
}

function ohc_body_to_dmy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_body_dates_to_dmy();
    ohc_customize_body_to_dmy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_MOSNUM_edit_summary();
}

function ohc_body_to_mdy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_body_dates_to_mdy();
    ohc_customize_body_to_mdy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_MOSNUM_edit_summary();
}

function ohc_ISO_to_dmy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_delink_ISO_to_dmy();
    ohc_customize_ISO_to_dmy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

//  ohc_MOSNUM_edit_summary();
}

function ohc_ISO_to_mdy_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_delink_ISO_to_mdy();
    ohc_customize_ISO_to_mdy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    //  ohc_MOSNUM_edit_summary();
}

function ohc_delink_year_in_X_driver()
{
    ohc_delink_year_in_X();
}

function ohc_unambiguous_dates_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_fix_unambiguous_dates();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_slash_edit_summary();
}

function ohc_US_slash_dates_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_US_slash_dates_to_mdy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_slash_edit_summary();
}

function ohc_UK_slash_dates_driver()
{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_UK_slash_dates_to_dmy();
    ohc_unprotect_dates();
    ohc_unprotect_strings();

    ohc_slash_edit_summary();
}

function ohc_expand_ref_dates_driver()

{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_expand_ref_dates();
    ohc_unprotect_dates();
    ohc_unprotect_strings();
    ohc_expand_edit_summary();
}

function ohc_expand_all_dates_driver()

{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_expand_ref_dates();
    ohc_expand_all_dates();
    ohc_unprotect_dates();
    ohc_unprotect_strings();
    ohc_expand_edit_summary();
}

function ohc_abbrev_ref_dates_driver()

{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_abbrev_ref_dates();
    ohc_unprotect_dates();
    ohc_unprotect_strings();
    ohc_abbrev_edit_summary();
}

function Ohc_abbrev_all_dates_driver()

{
    ohc_protect_strings();
    ohc_protect_dates();
    ohc_abbrev_ref_dates();
    ohc_abbrev_all_dates();
    ohc_unprotect_dates();
    ohc_unprotect_strings();
    ohc_abbrev_edit_summary();
}

$(function () {
    if(document.forms.editform) {
	mw.util.addPortletLink('p-tb', 'javascript:ohc_all_to_dmy_driver()', 'ALL dates to dmy', 'ca-unitfixer', 'Align all dates to dmy', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_all_to_mdy_driver()', 'ALL dates to mdy', 'ca-unitfixer', 'Align all dates to mdy', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_body_to_dmy_driver()', 'Body dates to dmy', 't-dmy', 'day month', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_body_to_mdy_driver()', 'Body dates to mdy', 't-mdy', 'month day', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_ISO_to_dmy_driver()', 'ISO to dmy', 't-ISOdmy', 'ISO day month', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_ISO_to_mdy_driver()', 'ISO to mdy', 't-ISOmdy', 'ISO month day', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_delink_year_in_X_driver()'  , 'Del year-in-X dates', 't-yearinX',  'Year in X', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_expand_ref_dates_driver()', 'Expand ref dates', 't-expand', 'Expand month names within refs', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_expand_all_dates_driver()', 'Expand all dates', 't-expand', 'Expand month names throughout', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_abbrev_ref_dates_driver()', 'Abbrev ref dates', 't-expand', 'Abbreviate month names within refs', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_abbrev_all_dates_driver()', 'Abbrev all dates', 't-expand', 'Abbreviate month names throughout', '', '');
//	mw.util.addPortletLink('p-tb', 'javascript:ohc_unambiguous_dates_driver()', 'unambiguous dates', 't-slash', 'resolves slash date format', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_US_slash_dates_driver()', 'US-slash dates', 't-US', 'US-slash', '', '');
	mw.util.addPortletLink('p-tb', 'javascript:ohc_UK_slash_dates_driver()', 'UK-slash dates', 't-UK', 'UK-slash', '', '');
    }
});
//</nowiki>