User:Dl2000/DateFix2.js

// ', "\"", ' ', "''" );

var numDigits = "0123456789";

var currSectionType = 0; var currSectionIndex = 0; var currSectionError = 0; var hasQMismatch = 0; var hasUTmismatch = 0;

var currpos = 0; var last_currpos = 0;

// Initial pass to parse out the text sections... // Set up local variables for text content and length to slightly improve efficiency - txt won't change in this first loop

var txtValue = txt.value; var txtValueLength = txt.value.length;

while (currpos < txtValueLength) {       if (currSectionType == 0) {           // assume normal text - look for the start of another type e.g. [[, {{, "            var nextSectionType = 0;            var ixNext = txtValueLength;            for (ii = 1; ii < arrSectStarts.length; ii++)            {                var ixPos = txtValue.indexOf(arrSectStarts[ii], currpos);                if (ixPos < 0)                {                    ixPos = txtValueLength + 1;                }                if (ixPos < ixNext)                {                    ixNext = ixPos;                    nextSectionType = ii;                }            }

if (ixNext < txtValueLength) {               // special case for double quote - stay in normal section type if this is not a starting // double quote (generally inches designation e.g. 7", as immediately preceded by digit(s))

if (nextSectionType == 7) // double quotation per arrSectStarts[] {                   var beforequote = txtValue.slice(ixNext - 1, ixNext); if (beforequote) {                       if (numDigits.indexOf(beforequote) >= 0) {                           nextSectionType = 0; // start another normal section ixNext++; }                   }                }

if (ixNext > currpos) {                   arrSectionText[currSectionIndex] = txtValue.slice(currpos, ixNext); arrSectionType[currSectionIndex] = 0; currSectionIndex++; }               currSectionType = nextSectionType; currpos = ixNext; }           else {               // no more sections found - write out to the end arrSectionText[currSectionIndex] = txtValue.slice(currpos); arrSectionType[currSectionIndex] = 0; currpos = txtValueLength; }       }        else {           if (currSectionType >= arrSectEnds.length) {               currpos++; // fail-safe trap currSectionType = 0; }           else {               var levels_in = 1; // up a level for each  - outer template ends when level gets to 0 var ixEnd = currpos + 1; var sStarter = arrSectStarts[currSectionType]; var sTerminator = arrSectEnds[currSectionType]; var identical_ends = 0; if (sStarter == sTerminator) {                   identical_ends = 1; }

while ((levels_in > 0) && (ixEnd < txtValueLength)) {                   var chk = txtValue.slice(ixEnd, ixEnd + sTerminator.length);

// if starter == terminator, this is only one level, only watch for the terminator! if ((chk == sStarter) && (identical_ends == 0)) {                       levels_in++; ixEnd = ixEnd + sStarter.length; }                   else {                       if (chk == sTerminator) {                           levels_in--; ixEnd = ixEnd + sTerminator.length; }                       else {                           var nxtStarter = txtValue.indexOf(sStarter, ixEnd + 1); var nxtTerminator = nxtStarter; if (identical_ends == 0) {                               nxtTerminator = txtValue.indexOf(sTerminator, ixEnd + 1); }                           if ((nxtStarter < nxtTerminator) && (nxtStarter >= 0)) {                               ixEnd = nxtStarter; }                           else {                               if (identical_ends == 1) {                                   // in quotation sections, automatically terminate at the end of the para and reset section scan var nxtPara = txtValue.indexOf("\n", ixEnd + 1); if (nxtPara < 0) {                                       nxtPara = txtValueLength; }                                   if (nxtTerminator > nxtPara) {                                       // alert("unterminated quote (" + sTerminator + ") at end of para:\n" + txtValue.slice(currpos, nxtPara) + " ixEnd=" + ixEnd); ixEnd = nxtPara; levels_in = 0; currSectionError = nxtPara; }                               }

if (nxtTerminator < 0) {                                   ixEnd = txtValueLength; }                               else {                                   ixEnd = nxtTerminator; }                           }                        }                    }                }

if (levels_in > 0) {                   var err_pos = currpos + 100; if (err_pos > txtValueLength) {                       err_pos = txtValueLength - 1; }

// alert('unterminated section of text! (missing ' + arrSectEnds[currSectionType] + //      ')\nCheck from start of this section:\n' +  txtValue.slice(currpos, err_pos) + '...');

hasUTmismatch = hasUTmismatch + 1; arrSectionText[currSectionIndex] = '' + txtValue.slice(currpos); arrSectionType[currSectionIndex] = currSectionType; currpos = txtValueLength; }               else {                   // write out section var nextSectionText; if (currSectionError > 0) {                       nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                       if (ixEnd > currSectionError + 2) {                           nextSectionText = nextSectionText + txtValue.slice(currSectionError + 1, ixEnd); }                       hasQMismatch++; }                   else {                       nextSectionText = txtValue.slice(currpos, ixEnd); }                   arrSectionText[currSectionIndex] = nextSectionText; arrSectionType[currSectionIndex] = currSectionType; currSectionError = 0; currSectionIndex++; currpos = ixEnd; }               currSectionType = 0; }       }    }

// Second pass - now refactor each section of text according to its type var newtxt = "";

for (j = 0; j < arrSectionText.length; j++) {       if (isdebug == 1) {           newtxt += Dl2_DebugColourStart(arrSectionType[j]); }       newtxt += Dl2_process_text_section(arrSectionText[j], arrSectionType[j]); if (isdebug == 1) {           newtxt += Dl2_DebugColourEnd; }   }

txt.value = newtxt;

// ??? to re-do //change start and end templates //change deprecated birth and death templates

} function Dl2_MOSNUM_edit_summary(nation){ // Add a tag to the summary box var txt=document.editform.wpSummary; var summary = "unify formatting per MoS, script-assisted (" + nation + " subject)"; if (txt.value.indexOf(summary) == -1) { if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) { txt.value += ", "; }		txt.value += summary; } //	document.forms.editform.wpMinoredit.checked = true; document.editform.wpDiff.click; } function Dl2_ADollar { var txt=document.editform.wpTextbox1;

txt.value=txt.value.replace(/{{AUD\|([^}]+)}}/gi, ''); txt.value=txt.value.replace(/\[\[Australian\sdollar\|AU\$\]\]/gi, ''); txt.value=txt.value.replace(/AU\$/gi, 'A\$'); }

// type - the full word of the English variant type to be tagged to function Dl2_EngTag(type) { var txt=document.editform.wpTextbox1;

// If there's an existing tag, prompt to override langvars = new Array("American", "Australian", "British (Oxford)", "British", "Canadian", "Hong Kong", "Indian", "Irish", "Jamaican", "New Zealand", "Pakistani", "South African" ); var curryyyymm = Dl2_CurrMY;

for (i = 0; i < langvars.length; i++) {       var rxTemp = new RegExp('', 'gi'); if ((langvars[i] != type) && (txt.value.search(rxTemp) >= 0)) {           if (confirm(langvars[i] + ' English tag already exists - override?')) {               var newtag=''; txt.value=txt.value.replace(rxTemp, newtag); }           else {               return; }       }    }

// add tag if not already present (or changed above) var rxpdd = new RegExp("{{Use " + type + " English", "gi"); var dflagfound = txt.value.search(rxpdd); if (dflagfound == -1) {       txt.value='\r\n'+txt.value; } }

function Dl2_nobots_check { var txt=document.editform.wpTextbox1; var rxnobot = new RegExp("\{\{bots *\| *deny=all", "i"); // If there's an existing nobots tag, prompt and return 1

if (txt.value.search(rxnobot) >= 0) {       alert("Article is flagged to deny all bots"); return 1; }   return 0; }

function Dl2_all_dates_to_dmy(nation, isdebug) { Dl2_formatgeneral; Dl2_dates_to_dmy(isdebug); // Dl2_delink_ISO_to_dmy; Dl2_MOSNUM_edit_summary(nation); }

function Dl2_CommonStyleFix(engtag, countrycode, isdebug) { if (Dl2_nobots_check == 0) {   if (engtag != "") {     Dl2_EngTag(engtag); }   Dl2_all_dates_to_dmy(countrycode, isdebug); } }

function Dl2_Australianate { Dl2_ADollar; Dl2_CommonStyleFix("Australian", "AUS", 0); }

function Dl2_NewZealandate { Dl2_CommonStyleFix("New Zealand", "NZL", 0); }

function Dl2_UKanate { Dl2_CommonStyleFix("British", "UK", 0); }

function Dl2_IntlDate { if (Dl2_nobots_check == 0) {   Dl2_all_dates_to_dmy("international", 0); } }

function Dl2_Debugged_Intl { Dl2_all_dates_to_dmy("international", 1); }

function Dl2_AusNZate { Dl2_all_dates_to_dmy("AUS/NZL", 0); }

function Dl2_IEanate { Dl2_CommonStyleFix("Irish", "IRL", 0); }

function Dl2_HKanate { Dl2_CommonStyleFix("Hong Kong", "HKG", 0); }

function Dl2_JAManate { Dl2_CommonStyleFix("Jamaican", "JAM", 0); }

function Dl2_ZAanate { Dl2_CommonStyleFix("South African", "ZAF", 0); } function Dl2_Indianate { Dl2_CommonStyleFix("Indian", "IND", 0); } $(function { if(document.forms.editform) {  mw.util.addPortletLink('p-tb', 'javascript:Dl2_Australianate', 'Australianate', 'ca-unitfixer', 'Format for Australia', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_NewZealandate', 'New Zealandate', 'ca-unitfixer', 'Format for New Zealand', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_AusNZate', 'AUS/NZ', 'ca-unitfixer', 'Format for Australia/New Zealand', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_UKanate', 'UKanate', 'ca-unitfixer', 'Format for UK', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_IEanate', 'Irelandate', 'ca-unitfixer', 'Format for Ireland', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_ZAanate', 'South Africanate', 'ca-unitfixer', 'Format for South Africa', , );  mw.util.addPortletLink('p-tb', 'javascript:Dl2_Indianate', 'Indianate', 'ca-unitfixer', 'Format for India', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_JAManate', 'Jamaicanate', 'ca-unitfixer', 'Format for Jamaica', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_HKanate', 'HKandate', 'ca-unitfixer', 'Format for Hong Kong', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_IntlDate', 'International dates', 'ca-unitfixer', 'International date formatting', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_IntlTag1', 'Intl tag', 'ca-unitfixer', 'International dmy date tag', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_BDA', 'BDA', 'ca-unitfixer', 'Standardise infobox birth dates', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_Fdate', 'FDate', 'ca-unitfixer', 'Standardise infobox film date', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_Debugged_Intl', '(Debug Test)', 'ca-unitfixer', 'Debug test', , );

} }); //