Wikipedia:Date formattings/script/MOSNUM dates

/*

 */

/**
 * Regex menu framework
 * by [[m:user:Pathoschild]] <http://meta.wikimedia.org/wiki/User:Pathoschild/Scripts/Regex_menu_framework>
 * - adds a sidebar menu of user-defined scripts.
 */
mw.loader.load('http://meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/Regex_menu_framework.js&action=raw&ctype=text/javascript');

importScript("User:Ohconfucius/script/MOSNUM_utils.js");

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

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

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

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

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

function ohc_delink_dates()
{
    //delink piped md|dm, md|dd and my|m dates
    ohc_regex(/\[\[@month @dd(?:#[^\|]+|)\|([^\|\]]+)\]\]/gi, "$1");
    ohc_regex(/\[\[@dd @month(?:#[^\|]+|)\|([^\|\]]+)\]\]/gi, "$1");
    ohc_regex(/\[\[@month @yyyy(?:#[^\|]+|)\|([^\|\]]+)\]\]/gi, "$1");

    //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)/gi, "@LMonth @Day");

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

    //month+day+year pseudo-ISO dates
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*@dd)-@Month-@YYYY/gi, '$1 @Month @YYYY');

    ohc_regex(/\[\[@Month @DD\|\d\d-\d\d\]\]-(?:\[\[)?@YYYY(?:\|@yyyy)?(?:\]\])/gi, "@Day @Month @YYYY");
    ohc_regex(/\[\[@DD\s@Month\|\d\d-\d\d\]\]-(?:\[\[)?@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}|[1-9]\d{0,2})\]\]/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|_|@th )(?:of[\s_]|)?@month\|([^\]]{1,30})\]\]/gi, "$1");

    //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|Division|Group|Infantry|Regiment|Squadron))/g, "@Month, $1");

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

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

    //zap redundant {{date}}, {{accessdate}} and {{#dateformat}}
    ohc_regex(/\{\{date\|@YYYY-@MM-@DD\}\}/gi, "@Day @Month @YYYY");
    regex(/\{\{date\|([^\}\|\-]+)(?:\|l?(?:[dmy]{3}|iso|none|link))?\}\}/gi, '$1');
    ohc_regex(/[ ]*\{\{accessdate\|@YYYY-@MM-@DD[ ]*\}\}/gi, " Retrieved @YYYY-@MM-@DD");
    regex(/\{\{#dateformat:♫?([^\}\|]+)(?:\|dmy|\|mdy)?\}\}/gi,'$1');
    regex(/\{\{(?:start|end)[-\s]date[^|}]*\|((?:1[789]|20)\d{2})\}\}/gi, '$1');  //stripping start/end template

    //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 and links 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)@DD@th?, @DD@th?(\s(?:and|&|to|or)\s)@DD@th?(?:\sof\s?)?(?:[ ]|&nbsp;)@Month(?=\W)/gi, '$1@Day1, @Day2$2@Day3 @LMonth');
    ohc_regex(/(\W)@DD@th?(\s(?:and|&|to|or)\s)@DD@th?(?:\sof\s?)?(?:[ ]|&nbsp;)@Month(?=\W)/gi, '$1@Day1$2@Day2 @LMonth');
    ohc_regex(/(\W)(?:the |)@DD@th?(?:\sof)\s@Month,? @YYYY(?=\W\D)/gi, '$1@Day @LMonth @YYYY');
    ohc_regex(/(\W)(?:the |)@DD@th?(?:\sof)\s@Month(?=\W\D)/gi, '$1@Day @LMonth');
    ohc_regex(/(\W)@Day(?:\sof|)(?:[ _]|&nbsp;)@Month(?=[^\|\]\w\d])/gi, '$1@Day @LMonth');
    ohc_regex(/(\W)@DD<sup>@th?<\/sup>(?:\sof\s?)?\s(@Month)(?=\W\D)/gi, '$1@Day @LMonth');
    ohc_regex(/(\W)@Month\s(?:the\s)?@DD@th?,(?:\sthe\s)?(?: |&nbsp;)@DD@th?(\s(?:and|&|to|or)(?:[ _]|&nbsp;))(?:the\s)?@DD@th?(?=[^\]\|\w\d])/gi, '$1@LMonth @Day1, @Day2$2@Day3');
    ohc_regex(/(\W)(@month)\s(@day)((?:,\s@day){0,6}),?(\/|\s?[-–]\s?|\s(?:and|&|to|or)\s+?)@Day(?:,?(?:[ _]|&nbsp;)|\sof\s)?(\d{3,4}\W\D)/gi, '$1$2 $3$4$5@Day, $6');
    ohc_regex(/(\W)@Month(?:[ ]|&nbsp;)(?:the\s)?@DD@th?(?=[^\]\|\w\d]\D)/gi, '$1@LMonth @Day');
    ohc_regex(/(\W)@Month(?:[ ]|&nbsp;)(?:the\s)?@DD<sup>@th<\/sup>(?=[^\]\|\w\d]\D)/gi, '$1@LMonth @Day');

    // consolidating
    regex(/(\|[ ]*date=[ ]*@day)[ ]*\|month=[ ]*(@month)[ ]*\|[ ]*year=[ ]*(@yyyy[ ]*[\|\}])/gi, "$1 $2 $3");

    // eliminating time of day
    ohc_regex(/(\|[ ]*author[ ]*=[ ]*)(?:posted|published)(?: by| on|)[\s:](?=\w)/gi, "$1");
    regex(/(\|[ ]*(?:date|archivedate|accessdate|author)[ ]*=[ ]*)[0-2]?\d:[0-5]\d(?:[ ]|&nbsp;)(?:[ap]m |[ap]\.m\. |)([A-Z]{1,2}T|UTC)[\.,]?[ ]?/gi, "$1");

    // eliminating days of the week
    regex(/(\|[ ]*(?:date|archivedate|accessdate|author)[ ]*=[ ]*)(?:(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day,?)\s/gi, "$1");
    regex(/(\|[ ]*(?:date|archivedate|accessdate|author)[ ]*=[ ]*)(?:(?:Mon|Tues?|Wed|Thur?|Fri|Sat|Sun)[\.,]?)\s/gi, "$1");

    // eliminating dates placed in author parameter
    ohc_regex(/(\|\s*author\s*=\s*)@month @day,? @yyyy\s?(?:,? \d?\d:\d\d(?:\s*|&nbsp;)(?:[apAP][mM]\s*|)(?:GMT\s*|UTC\s*|[A-Z]{1,2}[DS]T\s*|)|)\|/g, "|");
    ohc_regex(/(\|\s*author\s*=\s*)@day @month,? @yyyy\s?(?:,? \d?\d:\d\d(?:\s*|&nbsp;)(?:[apAP][mM]\s*|)(?:GMT\s*|UTC\s*|[A-Z]{1,2}[DS]T\s*|)|)\|/g, "|");

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

    ohc_regex(/([ =]+)@DD-@Mon-((?:\d|1[0-9]|20)\d{2})(?![-–\d\/])/gi, "$1@Day @Month $2"); //narrowing choice of years by not using @YYYY
    ohc_regex(/([ =]+)@DD-@MM-((?:\d|1[0-9]|20)\d{2})(?![-–\d\/])/gi, "$1@Day @Month $2");

// eliminating other errant formats
    ohc_regex(/([ ]\()@DD-@MM-@YYYY(\)\W)/gi, '$1@Day @Month @YYYY$2');
    ohc_regex(/([ ]\()@DD-@Month-@YYYY(\)\W)/gi, '$1@Day @Month @YYYY$2');
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Month, @DD, @YYYY(?=[ ]*\|)/gi, "$1@Day @Month @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@DD, @Month, @YYYY(?=[ ]*\|)/gi, "$1@Day @Month @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@YYYY,? @Month @DD(\D)/gi, "$1@Day @Month @YYYY$2");
    ohc_regex(/[ ]\(@YYYY,? @Month @DD\)/gi, " (@Day @Month @YYYY)");
    ohc_regex(/[ ]\(@YYYY, @DD @Month\)/gi, " (@Day @Month @YYYY)");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@YYYY,? @Month([ ]?\D)/gi, "$1@Month @YYYY$2");
    ohc_regex(/[ ]\(@YYYY, @Month\)/gi, " (@Month @YYYY)");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][–—]\[\[|[–—])@MM[–—]@DD(?:\]\])?(?=\W\D)/gi, "$1@Day @Month @YYYY"); //ndashes and emdashes
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[–—]@MM[–—]@DD([^\w\/\-%,<][^<]*?<\/ref>)/gi, "$1$2@Day @Month @YYYY$3");

    // spaces and commas between month and year
    ohc_regex(/@Month(?:,\s+|\s+of\s+|,?&nbsp;)@YYYY(\D)/gi, "@LMonth @YYYY$1");

// fix month names (capitalize, remove dots)
    ohc_regex(/(\W)@DD\s@FullMonth(\s+|\s*,)/gi, "$1@Day @FullMonth$2");
    ohc_regex(/(\W)@DD\s@Mon(\s+|\s*,)/gi, "$1@Day @Mon$2");
    ohc_regex(/(\W)@FullMonth\s@DD(\s+|\s*,)/gi, "$1@FullMonth @Day$2");
    ohc_regex(/(\W)@Mon\s@DD(\s+|\s*,)/gi, "$1@Mon @Day$2");
    regex(/(\d)[ ]?(?:-|–|&[mn]dash;)[ ]?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))(?![ \-][A-Z]\w*|''|")/g, '$1c$2');

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

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

    // spaces and commas - md-md and dm-dm date ranges
    ohc_regex(/(\D\W)@Month\s@DD(&nbsp;|\s)(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?@Month\s@DD(?=\W\D)/gi, "$1@LMonth1 @Day1$2– @LMonth2 @Day2");
    ohc_regex(/(\D\W)@Month\s@DD(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?@Month\s@DD(?=\W\D)/gi, "$1@LMonth1 @Day1 – @LMonth2 @Day2");
    ohc_regex(/(\D\W)@DD\s@Month(&nbsp;|\s)(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?@DD\s@Month(?=\W\D)/gi, "$1@Day1 @LMonth1$2– @Day2 @LMonth2");
    ohc_regex(/(\D\W)@DD\s@Month(?:[-–—]|&[mn]dash;)(?:&nbsp;|\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)@Month\s@DD(?:(?:\s*|&nbsp;)[-–]\s*)@DD(?=\W\D)/gi, "$1@LMonth1 @Day1–@Day2");
    ohc_regex(/(\D\W)@DD(?:(?:\s*|&nbsp;)[-–]\s*)@DD\s@Month(?=\W\D)/gi, "$1@Day1–@Day2 @LMonth1");

    //reformat dmy-dmy and mdy-mdy (birth-death ranges)
    ohc_regex(/(\D\W)@DD\s@Month,? @Year(&nbsp;|\s)(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@DD\s@Month,? @Year(?=\D\W)/gi, "$1@Day1 @LMonth1 @Year1$2– @Day2 @LMonth2 @Year2");
    ohc_regex(/(\D\W)@Month\s@DD,? @Year(&nbsp;|\s)(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@Month\s@DD,? @Year(?=\D\W)/gi, "$1@LMonth1 @Day1, @Year1$2– @LMonth2 @Day2, @Year2");
    ohc_regex(/(\D\W)@DD\s@Month,? @Year(?:,?(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@DD\s@Month,? @Year(?=\D\W)/gi, "$1@Day1 @LMonth1 @Year1 – @Day2 @LMonth2 @Year2");
    ohc_regex(/(\D\W)@Month\s@DD,? @Year(?:,?(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@Month\s@DD,? @Year(?=\D\W)/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@Month $2');

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

}

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

function ohc_ISO_to_dmy_in_citations()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Day @Month @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Day @Month @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Day @Month @YYYY");
}

function ohc_ISO_to_mdy_in_citations()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi,  "$1@Month @Day, @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi,  "$1@Month @Day, @YYYY");
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Month @Day, @YYYY");
}

function ohc_ISO_to_dmy_in_references()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\w\/\-%,<][^<]*?<\/ref>)/gi, "$1$2@Day @Month @YYYY$3");
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\w\/\-%,<][^<]*?<\/ref>)/gi, "$1$2@Day @Month @YYYY$3");
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD(<\/ref>)/gi, "$1$2@Day @Month @YYYY$3");
}

function ohc_ISO_to_mdy_in_references()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\w\/\-%,<][^<]*?<\/ref>)/gi, "$1$2@Month @Day, @YYYY$3");
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD([^\w\/\-%,<][^<]*?<\/ref>)/gi, "$1$2@Month @Day, @YYYY$3");
    ohc_regex(/(<ref[^>]*>[^<]+?)([\s\(])@YYYY[-–]@MM[-–]@DD(<\/ref>)/gi, "$1$2@Month @Day, @YYYY$3");
}

// currently unused
function ohc_ISO_to_dmy_anywhere()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Day @Month @YYYY$2');
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Day @Month @YYYY$2');
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Day @Month @YYYY$2');
}

// currently unused
function ohc_ISO_to_mdy_anywhere()
{
    //multiple calls for multiple dates within single citation
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Month @Day, @YYYY$2');
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Month @Day, @YYYY$2');
    ohc_regex(/([^\w\/\-])@YYYY[-–]@MM[-–]@DD([^\w\/\-])/gi, '$1@Month @Day, @YYYY$2');
}

function ohc_delink_ISO_to_dmy()
{
    ohc_ISO_to_dmy_in_citations();
    ohc_ISO_to_dmy_in_references();
    regex(/([^\w\/\-%,])@YYYY-@MM-@DD(<\s?\/ref.*?>)/g, '$1@Day @Month @Year$2');
}

function ohc_delink_ISO_to_mdy()
{
    ohc_ISO_to_mdy_in_citations();
    ohc_ISO_to_mdy_in_references();
    regex(/([^\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');
    ohc_regex(/(\{\{dts\s*\|\s*@year\s*\|\s*@month\s*\|\s*@day\s*\|\s*format=)(?:dmy|mdy)(\}\})/gi, '$1'+format+'$2');
    ohc_regex(/(\{\{dts\s*\|\s*format=)(?:dmy|mdy)(\s*\|\s*@year\s*\|\s*@month\s*\|\s*@day\s*\}\})/gi, '$1'+format+'$2');
    ohc_regex(/(\{\{dts\s*\|\s*@year\s*\|\s*@month\s*\|\s*@day\s*)(\}\})/gi, '$1|format='+format+'$2');
}

// 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)@Month((?:\s@day?,?){1,6}),?(\/|\s?(?:[-–]|&ndash;)\s?|(?:[ _]|&nbsp;)(?:and|&|to|or)(?:[ _]|&nbsp;)+?)@Day,?\s(?:of\s)?(@yyyy\W\D)/gi, "$1$2$3@Day @LMonth $4");
    ohc_regex(/(\D\W)@Month((?:\s@day?,?){1,6}),?(\/|\s?(?:[-–]|&ndash;)\s?|(?:[ _]|&nbsp;)(?:and|&|to|or)(?:[ _]|&nbsp;)+?)@Day(?=\W\D)/gi, "$1$2$3@Day @LMonth");
    ohc_regex(/(\D\W)@Month((?:\s@day?,?){1,6}) @Day(?=\W\D)/gi, "$1$2 @Day @LMonth");

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

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

    //Month+day_number " 7th March" -> "7 March"
    ohc_regex(/(\D\W)@Day\s@Month\s@YYYY(?=\W\D)/gi, "$1@Day @LMonth @Year");
    ohc_regex(/(\D\W)@Day\s@Month(?=\W\D)/gi, "$1@Day @LMonth");

    //remove comma
    ohc_regex(/(\D\W)@Day\s@Month,\s?@Year(?=\W\D)/gi, "$1@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)@Day((?:, @day){0,5})(\/|\s?(?:[-–]|&ndash;)\s?|\s(?:and|&|to|or)\s+?)@Day\s@Month,? @YYYY(?=\W\D)/gi, "$1@LMonth @Day1$2$3@Day2, @YYYY");
    ohc_regex(/(\D\W)@Day((?:, @day){0,5})(\/|\s?(?:[-–]|&ndash;)\s?|\s(?:and|&|to|or)\s+?)@Day\s@Month(?=\W\D)/gi, "$1@LMonth @Day1$2$3@Day2");
    ohc_regex(/(\D\W)@Day((?:, @day){0,5})\s@Month,? @YYYY(?=\W\D)/gi, "$1@LMonth @Day1$2, @YYYY");
    ohc_regex(/(\D\W)@Day((?:, @day){0,5})\s@Month(?=\W\D)/gi, "$1@LMonth @Day1$2");

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

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

    //Month+day_number "March 7th" -> "March 7"
    ohc_regex(/(\D\W)@Month\s@Day,?\s@YYYY(?=\W\D)/gi, "$1@LMonth @Day, @YYYY");
    ohc_regex(/(\D\W)@Month\s@Day(?=\W\D)/gi, "$1@LMonth @Day");

    //add comma where missing
    ohc_regex(/(\D\W)@Month\s@Day(?=\s@yyyy\W\D)/gi, "$1@LMonth @Day,");
    //rem superfluous comma where applicable
    ohc_regex(/(\D[^\w\/])@Month\s@ZD,,(?=\W\D|\b)/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(?=\s*[\|\}])/gi, "$1@Day @FullMonth @YYYY");
    ohc_regex(/(<ref[^>]*>(?:[^<]+?[\s\(]|))@Day @Month @YYYY(?=(?:[^\w\/\-%,<][^<]*?|)<\/ref>)/gi, "$1@Day @FullMonth @YYYY");
    ohc_regex(/([ ]\()@Day @Month @YYYY(?=\)\W)/gi, "$1@Day @FullMonth @YYYY");

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

function ohc_expand_all_dates()
{
    ohc_regex(/([^\w\/\-])@Day @Month @YYYY([^\w\/\-])/gi, '$1@Day @FullMonth @YYYY$2');
    ohc_regex(/([^\w\/\-])@Month @Day, @YYYY([^\w\/\-])/gi, '$1@FullMonth @Day, @YYYY$2');
}

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

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

//not currently used
function ohc_abbrev_all_dates()
{
    ohc_regex(/([^\w\/\-])@Day @Month @YYYY(?=[^\w\/\-])/gi, '$1@Day @Mon @YYYY');
    ohc_regex(/([^\w\/\-])@Month @Day, @YYYY(?=[^\w\/\-])/gi, '$1@Mon @Day, @YYYY');
}

//not currently used
function ohc_dmy_publication_dates()
{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Month @Day,? @YYYY/gi, "$1@Day @Month @YYYY"); // conversion of dates in references outside of the protection loop (eg for quotations etc.)
    ohc_regex(/(\|[ ]*(?:date|archivedate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Day @Month @YYYY");
    //replace ISO dates (within parentheses) only within refs
    ohc_regex(/(<ref[^>]*>[^<]+?)([ ]\()@YYYY-@MM-@DD(\)\W)([^<]*?<\/ref>)/gi, "$1$2@Day @Month @YYYY$3$4");

}

function ohc_mdy_publication_dates()
{
    ohc_regex(/(\|[ ]*(?:date|archivedate|accessdate)[ ]*=[ ]*)@Day @Month @YYYY/gi, "$1@Month @Day, @YYYY"); // conversion of dates in references outside of the protection loop (eg for quotations etc.)
    ohc_regex(/(\|[ ]*(?:date|archivedate)[ ]*=[ ]*)(?:\[\[)?@YYYY(?:\]\][-–]\[\[|[-–])@MM[-–]@DD(?:\]\])?(?=\W\D)/gi, "$1@Month @Day, @YYYY");
    //replace ISO dates (within parentheses) only within refs
    ohc_regex(/(<ref[^>]*>[^<]+?)([ ]\()@YYYY-@MM-@DD(\)\W)([^<]*?<\/ref>)/gi, "$1$2@Month @Day, @YYYY$3$4");

}

function ohc_ISO_access_dates(){
// Add a tag to the summary box
 var txt=document.editform.wpTextbox1;
    ohc_regex(/(\|[ ]*(?:accessdate))[ ]*=[ ]*@Day @Month @YYYY(?=[\s]*[\|\}])/gi, "$1=@YYYY-@MM-@DD");
    ohc_regex(/(\|[ ]*(?:accessdate))[ ]*=[ ]*@Month @Day, @YYYY(?=[\s]*[\|\}])/gi, "$1=@YYYY-@MM-@DD");

    ohc_regex(/(?:Retrieved |Accessed)[ ]*@Day @Month @YYYY(?=\W)/gi, "Retrieved @YYYY-@MM-@DD");
    ohc_regex(/(?:Retrieved |Accessed)[ ]*=[ ]*@Month @Day, @YYYY(?=\W)/gi, "Retrieved @YYYY-@MM-@DD");
}

/** ------------------------------------------------------------------------ **/
/// DATE PROTECTION

var linkmap=[];
function ohc_protect_dates()
{
    // protects dates within links, quotes, etc
    // the sensitive part is stored and replaced with a unique identifier,
    // which is later replaced with the stored part.

    var protect_function = function(s, begin, replace, end) {
        linkmap.push(replace);
        return begin + "⍌"+(linkmap.length-1)+"⍍" + end;
    }

    regex(/(<timeline>)([\s\S]*?)(<\/timeline>)/gi, protect_function);
    regex(/(<ref)([^>]+)(>)/gi, protect_function);
    regex(/((?:Category|Image|File):)([^|\]]*)([\|\]])/gi, protect_function);
    regex(/(\{(?:See ?also|Main))(\|[^\}]*)(\})/gi, protect_function);
    regex(/(\{\{externalimage\s?\|)([^\}]+)(\})/gi, protect_function);
    regex(/(\[(?:https?:|ftp:))([^\]]*\])(.)/gi, protect_function);
    regex(/(>[ ]*(?:https?:|ftp:))([^< >\]]*)([ ]*<)/gi, protect_function);
//    regex(/(https?:|ftp:)([^\s\]]*)([\s\]])/gi, protect_function);
    regex(/(<blockquote>)([\s\S]*?)(<\/blockquote>)/gi, protect_function);
    regex(/((?:image\d?|image_skyline|image[ _]location\d?|image[ _]name|img|pic)\s*=)([^\|\}]*)([\|\}])/gi, protect_function);
    regex(/([\|\{]\s*(?:file|image\d?|image location\d?|img|pic|title|quote|chapter|journal|url|archiveurl|work|doi)\s*=)([^\|\}⍍]*)([\|\}])/gi, protect_function);
    regex(/([\|\{]\s*(?:file|image\d?|image location\d?|img|pic|title|quote|chapter|journal|url|archiveurl|work|doi)\s*=)([^\|\}⍍]*)([\|\}])/gi, protect_function);
    regex(/([\|\{]\s*(?:season)\s*=)(\s*\[\[[^\]]*\]\]\s*)([\|\}])/gi, protect_function);
    regex(/(\{[^\{]{0,6}(?:quot[^\|]{1,7}\s?|sic)\|)([^\}]+)(\})/gi, protect_function);
    regex(/(\{\{navsource\|)([^\}]+)(\})/gi, protect_function);
    regex(/([\s\(]")([^"\n]*)(")/gi, protect_function);
    regex(/([\s\(]“)([^”\n]*)(”)/gi, protect_function);
    regex(/(.)(\(\{\{by\|[12]\d{3}\}\}[-–]\{\{by\|)([12]\d{3}[ ]*\}\}\))/gi, protect_function);
    regex(/(.)(\(\{\{by\|[12]\d{3}\}\}(?:[-–]present|))(\))/gi, protect_function);

    //retraining redirects containing a date and some text
    regex(/(\[\[)(September 3 Society)(\]\]|\|)/gi, '$1Jiusan Society$2');
    regex(/(\[\[)(June 4th incident)(\]\]|\|)/gi, '$1Tiananmen Square protests of 1989$2');

    //protect 'may' when the intended meaning is not a month (note: not case insensitive)
    ohc_regex(/([^\w])(@day,?\s{0,3}may)([^\w][^\d])/g, protect_function);

    //protect all links containing a date and some text
    ohc_regex(/(\[\[)(\s*\w[^\]\|\n]+?@day[^\]\|\n]+?@fullmonth[^\]\|\n]*)(\]\]|\|)/gi, protect_function);
    ohc_regex(/(\[\[)([^\]\|\n]*?@day[^\]\|\n]+?@fullmonth(?:,? @year|) \D[^\]\|\n]+\w\s*)(\]\]|\|)/gi, protect_function);
    ohc_regex(/(\[\[)(\s*\w[^\]\|\n]+?@fullmonth[^\]\|\n]+?@day[^\]\|\n]*)(\|)/gi, protect_function);
    ohc_regex(/(\[\[)([^\]\|\n]*?@fullmonth[^\]\|\n]+?@day(?:, @year|) \D[^\]\|\n]+\w\s*)(\]\]|\|)/gi, protect_function);

    //protects certain dates
    regex(/(\[\[)(4AD)(.)/gi, protect_function);
    regex(/([\s\S])(March (?:8|14) Alliance)([\s\S])/gi, protect_function);
    regex(/([\s\S])(23rd March 1931: Shaheed)([\s\S])/gi, protect_function);
    regex(/([\s\S])(Long March [1-7])([\s\S])/gi, protect_function);
    regex(/([\s\S])(Bombay March 12)([\s\S])/gi, protect_function);
    regex(/([\s\S])(movement 2 June)([\s\S])/gi, protect_function);
    regex(/([\s\S])(6th\sof October City)([\s\S])/gi, protect_function);
    regex(/([\s\S])(Party Workers' Liberation Front 30th of May)([\s\S])/gi, protect_function);
    regex(/([\s\S])(4th\sof August regime)([\s\S])/gi, protect_function);
    regex(/([\s\S])(19th\sof April movement)([\s\S])/gi, protect_function);
    regex(/([\s\S])(June 4th incident)([\s\S])/gi, protect_function);
    regex(/([\s\S])(July 31st \(film\))([\s\S])/gi, protect_function);
    regex(/([\s\S])(2\.13\.61)([\s\S])/gi, protect_function);
    regex(/(\[\[)(13 May incident)(\|)/gi, protect_function);
    regex(/(\[\[)(November 1828)(\]\])/gi, protect_function);
    regex(/(\[\[)(4th\sof July)(.)/gi, protect_function);
    regex(/(\[\[)((?:2300|2000)\s?AD)(\]\])/gi, protect_function);
    regex(/(\[\[)(29 February)(\]\])/gi, protect_function);
    regex(/(\[\[)(February 29)(\]\])/gi, protect_function);

    // works names
//    regex(/(.)(protect string)(.)/gi, protect_function);
    regex(/(.)(11\/22\/63)(.)/gi, protect_function);
    regex(/(.)(1\.1\.1994)(.)/gi, protect_function);
    regex(/(.)(4th of July, Asbury)(.)/gi, protect_function);
    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(/(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 [\w ]*,? [1-3]\d)( @fullmonth)/gi, "$1♫$2");
    ohc_regex(/(Live at [\w ]*,? @fullmonth)( @dd)/gi, "$1♫$2");
    ohc_regex(/(Bootmoon Series: [\w ]*[-–] @fullmonth)( @day, @YYYY)/gi, "$1♫$2");
}


function ohc_unprotect_dates()
{

    //unprotect all dates
    regex(/[♫]/g, '');
    regex(/⍌([0-9]+)⍍/g, function(x, n) {
        var res = linkmap[n];
        res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
            var res = linkmap[n];
            res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
                var res = linkmap[n];
                res = res.replace(/⍌([0-9]+)⍍/g, function(x, n) {
                    return linkmap[n];
                });
                return res;
            });
            return res;
        });
        return res;
    });
}

/** ------------------------------------------------------------------------ **/
/// 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_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() {}
function ohc_customize_body_to_mdy() {}
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_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_ISO_to_dmy_in_citations();

    ohc_MOSNUM_edit_summary();
}

function ohc_all_to_mdy_driver()
{
    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_ISO_to_mdy_in_citations();

    ohc_MOSNUM_edit_summary();
}

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

    ohc_MOSNUM_edit_summary();
}

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

    ohc_MOSNUM_edit_summary();
}

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

//  ohc_MOSNUM_edit_summary();
}

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

    //  ohc_MOSNUM_edit_summary();
}

function ohc_delink_year_in_X_driver()
{
    ohc_delink_year_in_X();
}

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

    ohc_slash_edit_summary();
}

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

    ohc_slash_edit_summary();
}

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

    ohc_slash_edit_summary();
}

function ohc_expand_ref_dates_driver()
{
    ohc_protect_dates();
    ohc_expand_ref_dates();
    ohc_unprotect_dates();
    ohc_expand_edit_summary();
}

function ohc_expand_all_dates_driver()
{
    ohc_protect_dates();
    ohc_expand_ref_dates();
    ohc_expand_all_dates();
    ohc_unprotect_dates();
    ohc_expand_edit_summary();
}

function ohc_abbrev_ref_dates_driver()
{
    ohc_protect_dates();
    ohc_abbrev_ref_dates();
    ohc_unprotect_dates();
    ohc_abbrev_edit_summary();
}

function Ohc_abbrev_all_dates_driver()
{
    ohc_protect_dates();
    ohc_abbrev_ref_dates();
    ohc_abbrev_all_dates();
    ohc_unprotect_dates();
    ohc_abbrev_edit_summary();
}


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

/*

*/