User:Quarl/date canonicalize.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:Quarl/date canonicalize. |
// [[User:Quarl/date_canonicalize.js]] - canonicalizes date WikiLinks
// Example: July 17, 1982 becomes [[1982-07-17]]
// requires: wikipage.js, util.js, addlilink.js, datetime.js
// quarl 2006-01-31 initial version
//<pre><nowiki>
datez = new autoedit(
'datez',
'DateZ',
'ca-datez',
'Canonicalize dates',
'Date canonicalization');
// Return a string for a regexp that matches possibly wiki-linked dates in
// various date formats. This is a monster regexp, the hardest part of this
// script!
datez.buildRegExp = function() {
var groupIfNeccessary = function(s) {
// I don't know a good way to check against e.g. "(foo)|(bar)"; for now just be conservative in adding grouping
if (s.match(/\|/) /*&& !s.match(/^\(/) */ ) {
return '(?:' + s + ')';
} else {
return s;
}
}
var joinRE = function() {
// desplice arguments
var args = Array.concat.apply(Array, arguments).map(groupIfNeccessary);
return '(?:' + args.join('|') + ')';
}
var linked = function(s) {
return '\\[\\[ *'+s+' *\\]\\]';
}
var maybelinked = function(s) {
return joinRE(linked(s), s);
}
var abbrevMonth = function(s) {
return s.substr(0,3);
}
var word = function(s) {
return '\\b' + s + '\\b';
}
var year4 = word('[012][0-9][0-9][0-9]');
var year42 = word(joinRE(year4, '[890][0-9]'));
var month = word(joinRE('0?[1-9]', '1[012]'));
// monthnames is in datetime.js
var monthS = word(joinRE(monthnames, monthnames.map(abbrevMonth)));
var day = word('[0123]?[0-9](?:\'?st|nd|th)?');
var delimz = '[ ,/-]+';
var all = joinRE(
// YYYY-MM-DD formats
linked( year4 + '-' + month + '-' + day ),
maybelinked(year4)+'-'+maybelinked(month+'-'+day),
maybelinked(year4)+'/'+month+'/'+day,
maybelinked(year4)+'\\.'+month+'\\.'+day,
year4 + ' ' + month + ' ' + day,
maybelinked(year4 + delimz + monthS + delimz + day),
// MM-DD-YYYY formats
month + '-' + day + '-' + year4,
month + '/' + day + '/' + year42,
linked(monthS + delimz + day + delimz + year42),
maybelinked(monthS + delimz + day) + delimz + maybelinked(year42),
linked(monthS) + delimz + linked(day) + delimz + '(?:of\s*)?' + linked(year42),
// DD-MM-YYYY formats: only support monthS, because it's ambiguous
// otherwise
linked( day + delimz + monthS + delimz + year4 ),
maybelinked(day + delimz + monthS) + delimz + maybelinked(year4)
);
return new RegExp(all, 'i');
}
datez.replaceRegExp = function(d, m) {
s = m[0];
s = s.replace(/[\[\]]/g, '');
s = s.replace(/[-.]/g, '/'); // Date only understands '/' as delimiter
var d = new Date(s); // parses date string
if (!d.getFullYear()) {
// couldn't parse
return null;
}
return '[[' + datestampUTCISO(d) + ']]';
}
datez._load = function() {
datez.addTab();
}
addOnloadHook(datez._load);
//</nowiki></pre>