User:Ohconfucius/test/YMD dot 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.
// *********************************************************************************************

//This is a test (non-production) script, and may have untested errors. Please exercise due care should you decide to use it.

// *********************************************************************************************

// This script aligns dates in the format YYYY.MM.DD or YYYY/MM/DD into abbreviated dmy format allowed by [[WP:MOSNUM]].

// Feedback and constructive criticism are welcome

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:pathoschild/templatescript.js]]
 */
mw.loader.load('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js');

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

function ohc_dot_dates_to_dmy()
{
//ranges
	ohc_regex(/([^-\d/:\.])@YYYY\/@MM\/@DD(&nbsp;|\s)?(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@YYYY\/@MM\/@DD(?=[^-–/\w&])/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
	ohc_regex(/([^-\d/:\.])@YYYY\.@MM\.@DD(&nbsp;|\s)?(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@YYYY\.@MM\.@DD(?=[^-–/\w&])/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
	ohc_regex(/(\| ?)@YYYY\/@MM\/@DD(&nbsp;|\s)?(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@YYYY\/@MM\/@DD(?=\s*\|)/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
	ohc_regex(/(\| ?)@YYYY\.@MM\.@DD(&nbsp;|\s)?(?:(?:[-–—]|&[mn]dash;)(?:&nbsp;|\s)?)@YYYY\.@MM\.@DD(?=\s*\|)/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");

// resolvable ambiguous date formats
	ohc_regex(/([^-\d/:\.])@YYYY\/@MM\/@DD(?=[^-–/\w&])/gi, '$1@Day @Mon @YYYY');
	ohc_regex(/([^-\d/:\.])@YYYY\.@MM\.@DD(?=[^-–/\w&])/gi, '$1@Day @Mon @YYYY');
	ohc_regex(/([^-\d/:\.])@DD[-–]@MM[-–]@YYNN(?=[^-–/\w&])/gi, '$1@Day @Mon @YYYY');
	ohc_regex(/(\| ?)@YYYY\/@MM\/@DD(?=\s*\|)/gi, '$1@Day @Mon @YYYY');
	ohc_regex(/(\| ?)@YYYY\.@MM\.@DD(?=\s*\|)/gi, '$1@Day @Mon @YYYY');
//	ohc_regex(/(\| ?)@DD[-–]@MM[-–]@YYNN(?=\s*\|)/gi, '$1@Day @Mon @YYYY');
}

/** ------------------------------------------------------------------------ **/
/// 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;
	};
//\|x=
	regex(/(<timeline>)([\s\S]*?)(<\/timeline>)/gi, protect_function);
	regex(/(<math>)([\s\S]*?)(<\/math>)/gi, protect_function);
	regex(/((?:{(?:Wikimedia |)Commons ?|C ?))(Cat(?:egory|))\|[^{}*](})/gi, protect_function);
	regex(/(<ref)([^>]+)(>)/gi, protect_function);
	regex(/((?:Category|Image|File):)([^|\]]*)([|\]])/gi, protect_function);
	regex(/(\{(?:See ?also|Main))(\|[^}]*)(\})/gi, protect_function);
	regex(/(\{\{(?:double ?|external ?|wide ?)image\s?\|)([^}]+)(\})/gi, protect_function);
	regex(/(\{\{(?:harvnb|r|wikisource)\|)([^}]+)(\})/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(/(<noinclude>)([\s\S]*?)(<\/noinclude>)/gi, protect_function);
	regex(/(<gallery)([\s\S]*?)(<\/gallery>)/gi, protect_function);
	regex(/(<poem>)([\s\S]*?)(<\/poem>)/gi, protect_function);
	regex(/(\{[^\{]{0,6}(?:quot[^|]{1,7}\s?|sic)\|)([^}]+)(\})/gi, protect_function);
	regex(/((?:image\d?|image_skyline|image[ _]location\d?|image[ _]name|image[ _]file|img|pic)\s*=)([^|}]*)([|}])/gi, protect_function);
	regex(/([\|\{]\s*(?:file(?:name\d?|)|image\d?|image location\d?|img|pic|Cover|(?:trans-?|)title|quote|chapter|journal|url|archiveurl|work|doi|club|at|volume|reporter)\s*=)([^|\}⍍]*)([|}])/gi, protect_function);
	regex(/([\|\{]\s*(?:file(?:name\d?|)|image\d?|image location\d?|img|pic|Cover|(?:trans-?|)title|quote|chapter|journal|url|archiveurl|work|doi|club|at|volume|reporter)\s*=)([^|\}⍍]*)([|}])/gi, protect_function);
	regex(/([\|\{]\s*(?:season)\s*=)(\s*\[\[[^\]]*\]\]\s*)([|}])/gi, protect_function);
	regex(/([\|\{]\s*(?:[xy])\s*=)(\s*[^\|]+\s*)(\|)/gi, protect_function);
	regex(/(\{\{navsource\|)([^}]+)(\})/gi, protect_function);
	regex(/(\{\{(?:singlechart)\|)([^}]+)(\})/gi, protect_function);
	regex(/(\{\{(?:defaultsort|graph):)([^}]+)(\})/gi, protect_function);
	regex(/(["“])([^"”\n]*)(["”])/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(/([\s\S])(2\.13\.61)([\s\S])/gi, protect_function);
//	regex(/(.)(4th of July, Asbury)(.)/gi, protect_function); //protected above globally

	
}


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;
	});
}


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

function ohc_dot_dates(){
// Add a tag to the summary box
 var txt=document.editform.wpTextbox1;
	ohc_protect_dates();
	ohc_dot_dates_to_dmy();
	ohc_unprotect_dates();
	ohc_dot_edit_summary();
}

$(function () {
 if(document.forms.editform) {
   mw.util.addPortletLink('p-tb', 'javascript:ohc_dot_dates()', 'DOT to dmy dates', 't-UK', 'UK-dot', '', '');

  }
});

// </pre>