User:Ohconfucius/test/YMD dot dates.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Ohconfucius/test/YMD dot dates. |
// *********************************************************************************************
//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( |\s)?(?:(?:[-–—]|&[mn]dash;)(?: |\s)?)@YYYY\/@MM\/@DD(?=[^-–/\w&])/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
ohc_regex(/([^-\d/:\.])@YYYY\.@MM\.@DD( |\s)?(?:(?:[-–—]|&[mn]dash;)(?: |\s)?)@YYYY\.@MM\.@DD(?=[^-–/\w&])/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
ohc_regex(/(\| ?)@YYYY\/@MM\/@DD( |\s)?(?:(?:[-–—]|&[mn]dash;)(?: |\s)?)@YYYY\/@MM\/@DD(?=\s*\|)/gi, "$1@Day1 @Mon1 @Year1$2 – @Day2 @Mon2 @Year2");
ohc_regex(/(\| ?)@YYYY\.@MM\.@DD( |\s)?(?:(?:[-–—]|&[mn]dash;)(?: |\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>