User:Bobblewik/monobook.js/dates.js: Difference between revisions

Content deleted Content added
Bobblewik (talk | contribs)
m imported updated code from datestest.js
m Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879)
 
(30 intermediate revisions by one other user not shown)
Line 1:
//'winc' function allows buttons to be added toolbar
 
winc('User:Omegatron/monobook.js/addlink.js');
 
function formatdates() {
var txt = document.editform.wpTextbox1;
Line 11 ⟶ 15:
txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]century\s(AD|BC|CE|BCE)\]\]/gi, '$1 century $2');
txt.value = txt.value.replace(/\[\[\d{1,2}(?:st|nd|rd|th)[ \-]century\|(\d{1,2}(?:st|nd|rd|th))[ \-]centuries\s(AD|BC|CE|BCE)\]\]/gi, '$1 centuries $2');
txt.value = txt.value.replace(/(\d(?:st|nd|rd|th))[ \-]Century/gi, '$1 century');
 
// piped decades and years
txt.value = txt.value.replace(/\[\[(\d{1,4}\'?s)\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[\d{1,4}s? (?:AD|BC|CE|BCE)\|(\d{1,4})\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[\d{1,4}s? (?:AD|BC|CE|BCE)\|(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[\d{1,4}s?\|(\d{1,4}s? (?:AD|BC|CE|BCE))\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[\d{1,4}s?\|(\d{1,2}s?)\]\]/gi, '$1');
 
// months
Line 32 ⟶ 39:
txt.value = txt.value.replace(/\[\[November\|(Nov)\]\]/gi, '$1');
txt.value = txt.value.replace(/\[\[December\|(Dec)\]\]/gi, '$1');
 
//month+year
txt.value = txt.value.replace(/\[\[((?:January|February|March|April|May|June|July|August|September|October|November|December) \d{3,4})\]\]/gi, '$1');
 
Line 92 ⟶ 101:
 
//year: examine characters in link on left for date, examine characters in link on right for date
txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^yhletr\d])\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/gi, '$1$2$3');
//year pair: examine characters in link on left for date, examine characters in link on right for date
txt.value = txt.value.replace(/((?:[^yhletramub\s]..|[^rcianlse\d\s].|[^yhletr\d])\]\]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc\s\-]))/gi, '$1$2$3$4$5');
 
//year: examine characters in link on left for date, avoid links on right
Line 102 ⟶ 111:
 
//year: check for line-ends, text on left, avoid links on right. Run twice to deal better with lists.
txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?|\n)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3');
txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?|\n)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3');
//year pair: check for line-ends, text on left, avoid links on right
txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\]([^\[]{4}|\n)/gi, '$1$2$3$4$5');
 
//year: avoid links on left, examine characters in link on right for date
txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/gi, '$1$2$3');
//year pair: avoid links on left, examine characters in link on right for date
txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[(?:[^jfmasond\d]|.[^aepuco\d\s]|..[^jfmasondbrylgptvc \s\-]))/gi, '$1$2$3$4$5');
 
//year:avoid links on left, text on right
txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3');
//year pair: avoid links on left, text on right
txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3$4$5');
 
//year:text on left, text on right
txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3');
//year pair: avoid links on left, text on right
txt.value = txt.value.replace(/([\w\(\);=:.'\*\|\&]\s?,?\-?\s?)\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\](\s?,?\-?\s?[\w\(\);=:.'\*\|\&])/gi, '$1$2$3$4$5');
 
//year:avoid links on left, hyphen but no digits (to avoid ISO date) in link on right. Currently suspended because it isn't fully tested.
//txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](\s?,?\-?\s?\[\[[^\d])/gi, '$1$2$3');
 
//year:avoid links on both sides
Line 126 ⟶ 138:
//year pair: avoid links on both sides
txt.value = txt.value.replace(/([^\]]{4})\[\[(\d{1,4})\]\](.?.?.?.?.?.?)\[\[(\d{1,4})\]\]([^\[]{4})/gi, '$1$2$3$4$5');
 
//'present'
txt.value = txt.value.replace(/\[\[Present \(time\)\|(Present)\]\]/gi, '$1');
 
//Eliminate 'surprise links' also known as 'easter egg links'
txt.value = txt.value.replace(/\[\[\d{1,4}s?\sin\s[^\|]{1,30}\|(\d{1,4}s?)\]\]/gi, '$1');
 
 
// Add a tag to the summary box
var txt = document.editform.wpSummary;
//Made summary more accurate: not all links are removed. Keep summary brief
var summary = "Revising dateDate links. See:\[\[WP:DATE\]\]";
if (txt.value.indexOf(summary) == -1) {
if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
Line 142 ⟶ 161:
}
 
addOnloadHook$(function () {
if(document.forms.editform) {
addLink('p-cactions', 'javascript:formatdates()', 'datestestdates', 'ca-datefixer', 'Fixes some date formatting', '', '');
}
});