User:Dl2000/DateFix7.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) {                       var mismatchReason = currSectionType; switch (currSectionType) {                           case 1: nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                               break; case 2: nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                               break; case 7: nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                               break; case 9: nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                               break; default: nextSectionText = txtValue.slice(currpos, currSectionError + 1) + '';                               break; }                       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 = nation; 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", "Nigerian", "Pakistani", "South African" ); var curryyyymm = Dl2_CurrMY;

if (type == '%B') {       txt.value='\r\n'+txt.value; return; }

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(editsummary, isdebug) { Dl2_formatgeneral; Dl2_dates_to_dmy(isdebug); // Dl2_delink_ISO_to_dmy; Dl2_MOSNUM_edit_summary(editsummary); }

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

function Dl2_Australianate { Dl2_ADollar; Dl2_CommonStyleFix("Australian", "en-AU", 0); }

function Dl2_NewZealandate { Dl2_CommonStyleFix("New Zealand", "en-NZ", 0); }

function Dl2_Nigerianate { Dl2_CommonStyleFix("Nigerian", "en-NG", 0); }

function Dl2_UKanate { Dl2_CommonStyleFix("British", "en-GB", 0); }

function Dl2_ENBanate { Dl2_CommonStyleFix("%B", "en-B", 0); }

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

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

function Dl2_IEanate { Dl2_CommonStyleFix("Irish", "en-IE", 0); }

function Dl2_HKanate { Dl2_CommonStyleFix("Hong Kong", "en-HK", 0); }

function Dl2_JAManate { Dl2_CommonStyleFix("Jamaican", "en-JA", 0); }

function Dl2_ZAanate { Dl2_CommonStyleFix("South African", "en-ZA", 0); } function Dl2_Indianate { Dl2_CommonStyleFix("Indian", "en-IN", 0); } function Dl2_Pakistanate { Dl2_CommonStyleFix("Pakistani", "en-PK", 0); }

function Dl2_SGanate { Dl2_CommonStyleFix("Singapore", "en-SG", 0); }

// addOnloadHook(function { jQuery( document ).ready( function( $ ) { if(document.forms.editform) { mw.util.addPortletLink('p-tb', 'javascript:Dl2_Australianate', 'en-AU', 'ca-unitfixer', 'Format for Australia', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_NewZealandate', 'en-NZ', 'ca-unitfixer', 'Format for New Zealand', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_UKanate', 'en-GB', 'ca-unitfixer', 'Format for UK', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_ENBanate', 'en-B', 'ca-unitfixer', 'Format for B', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_IEanate', 'en-IE', 'ca-unitfixer', 'Format for Ireland', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_ZAanate', 'en-ZA', 'ca-unitfixer', 'Format for South Africa', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_Indianate', 'en-IN', 'ca-unitfixer', 'Format for India', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_Pakistanate', 'en-PK', 'ca-unitfixer', 'Format for Pakistan', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_JAManate', 'en-JA', 'ca-unitfixer', 'Format for Jamaica', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_HKanate', 'en-HK', 'ca-unitfixer', 'Format for Hong Kong', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_SGanate', 'en-SG', 'ca-unitfixer', 'Format for Singapore', , ); mw.util.addPortletLink('p-tb', 'javascript:Dl2_Nigerianate', 'en-NG', 'ca-unitfixer', 'Format for Nigeria', , ); 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', 'Add age to 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', , );

} }); //