User:Ohconfucius/script/MOSNUM dates.js: Difference between revisions

Content deleted Content added
(except bda)
Tag: Reverted
fully protect archive-url with built-in spacces
 
(14 intermediate revisions by the same user not shown)
Line 21:
script: ohc_all_to_mdy_driver
},
/*{
name: "Expand ref datesDMY_cs1=ly",
tooltip: "ExpandAccess month& namesarchive within refsdates",
script: ohc_expand_ref_dates_driverohc_dmy_cs1_ly_driver
},
{
name: "Expand all datesMDY_cs1=ly",
tooltip: "Expandsay month names throughouthello",
script: ohc_expand_all_dates_driverohc_mdy_cs1_ly_driver
},*/
{
name: "US-slash dates",
Line 189:
 
// century
.replace(/\[\[((?:first|second|third|(?:(?:thir|four|fif|six|seven|eigh|nin|ten|eleven|twelf|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteennine)teen|twentie)th|twenty[\s\-]first[\s\-])centur(?:y|ies)(?:\s(?:AD|BC|CE|BCE))?)\]\]/gi, "$1")
.replace(/\[\[(?:first|second|third|(?:(?:thir|four|fif|six|seven|eigh|nin|ten|eleven|twelf|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteennine)teen|twentie)th|twenty[\s\-]first)[\s\-_]centur(?:y|ies)(?:\s(?:AD|BC|CE|BCE))?\|([^\]]{1,30})\]\]/gi, "$1")
.replace(/\[\[(\d{1,2}(?:st|[nr]d|th))[\s\-_](centur(?:y|ies))(\s(?:AD|BC|CE|BCE)|)\]\]/gi, "$1 $2$3")
.ohc_regex(/\[\[\d{1,2}@th[\s\-_]centur(?:y|ies)(?:\s(?:AD|BC|CE|BCE)|)\|([^\]]{1,30})\]\]/gi, "$1")
 
// months
Line 202:
.replace(/\[\[(\d{1,3}0)\'?s\]\]/g, "$1s")
.replace(/\[\[\d{1,3}0\'?s?\|([^\]]{1,30})\]\]/g, "$1")
// .replace(/\[\[(\d{12,3}0)\'?(s)?\s(AD|BC|CE|BCE)\]\]/gi, "$1$2 $3")
// .replace(/\[\[(\d{1,4}[\s_]?)(AD|BC|CE|BCE)\]\]/gi, "$1$2")
.replace(/\[\[(AD|BC|CE|BCE)([\s_]?)(\d{1,4})\]\]/gi, "$1$2$3")
.ohc_regex(/([-–])\[\[\s?@yyyy\s?\|\s?(\d{2,4})\s?\]\]/gi, "$1$2") // piped year
Line 227:
// Identify surprise or 'Easter egg' diversions linking months to year or "year in" articles.
.ohc_regex(/\[\[\d{1,4}#[^|\]]+\|@Month\]\]/gi, "@LMonth")
// .ohc_regex(/\[\[@yyyy \w[^|\]]{3,12}\|@Year\]\]/gi, "@Year") //disable easter egg removal per request 5 November 2022
 
// month and day piped
Line 276:
 
// remove parasitic metadata - days of the week/descriptives
.ohc_regex(/(\|\s*(?:publication|archive|access|air|)-?date\s*=\s*)(?:[^|}\d]*|)@Day\s+@Month\s+@YYYY((?:[a-z]\b|)\s?)[^|<}]*(?=[\s\n]*[<|}])/gi, "$1@Day @Month @YYYY$2") // rem negate "<", "hyphens" "dashes" 26/6/2020
.ohc_regex(/(\|\s*(?:publication|archive|access|air|)-?date\s*=\s*)[^|}]*@Month\s+@Day,\s+@YYYY((?:[a-z]\b|)\s?)[^|<}]*(?=[\s\n]*[<|}])/gi, "$1@Month @Day, @YYYY$2") // rem negate "<", "hyphens" "dashes" 26/6/2020
//.ohc_regex(/(\|\s*date\s*=\s*)(?:[^|}–<]*\D|)@Day\s+@Month\s+@YYYY ?– ?@Day\s+@Month\s+@YYYY[^|}–<]*(?=[\s\n]*[<|}])/gi, "$1@Day1 @Mon1 @Year1 – @Day2 @Mon2 @Year2")
//.ohc_regex(/(\|\s*date\s*=\s*)[^|}–<]*@Month\s+@Day,\s+@YYYY ?– ?@Month @Day, @YYYY[^|}–<]*(?=[\s\n]*[<|}])/gi, "$1@Mon1 @Day1, @Year1 – @@Mon2 Day2, @Year2")
Line 345:
// long-hand palliative for observed failure to insert spaces
.ohc_regex(/(\|\s*(?:archive|access|air)-?date\s*=\s*)(?:on |)([0-2]?\d|30|31)\s*(January|February|March|April|May|June|July|August|September|October|November|December)\s*((?:19\d|20[0-1])\d\s?)[^|{}\s<>]*(?=\s*[|}])/gi, "$1$2 $3 $4")
//.ohc_regex(/(\|\s*(?:publication|)-?date\s*=\s*)(?:on |)@Day\s*@Month\s*@YYYY([a-z]\s?|\s?)\w*[^|{}<>]*(?=[|}])/gi, "$1@Day @Month @YYYY$2")
//.ohc_regex(/(\|\s*(?:publication|)-?date\s*=\s*)(?:on |)@Month\s*@Day,\s*@YYYY([a-z]\s?|\s?)\w*[^|{}<>]*(?=[|}])/gi, "$1@Month @Day, @YYYY$2")
 
// remove extraneous bracket //from Batty
Line 520 ⟶ 518:
editor
// ISO dates within single citation
.ohc_regex(/(\|\s*(?:publication|archive|access|air|designation\d?_|)-?date\s*=\s*)@YYYY[-–]@MM[-–]@DD(?=\W\D)/gi, "$1@Day @Month @YYYY"); //regex which converts ymd to dmy or mdy throughout (within citation templates)
// .ohc_regex(/(\|\s*(?:publication|)-?date\s*=\s*)(?:on |)@Day\s*@Month\s*@YYYY([a-z]\s?|\s?)\w*@MM[^|{}<>-–]*@DD(?=[|}]\W\D)/gi, "$1@Day @Month @YYYY$2");
}
 
Line 526 ⟶ 525:
editor
// ISO dates within single citation
.ohc_regex(/(\|\s*(?:publication|archive|access|air|designation\d?_|)-?date\s*=\s*)@YYYY[-–]@MM[-–]@DD(?=\W\D)/gi, "$1@Month @Day, @YYYY"); //regex which converts ymd to dmy or mdy throughout (within citation templates)
// .ohc_regex(/(\|\s*(?:publication|)-?date\s*=\s*)(?:on |)@Month\s*@Day,\s*@YYYY([a-z]\s?|\s?)\w*@MM[^|{}<>-–]*@DD(?=[|}]\W\D)/gi, "$1@Month @Day, @YYYY$2");
}
 
Line 627:
.ohc_regex(/(\|\s*(?:begin|end|first|last)\s*=\s*)@YYYY[-–]@MM[-–]@DD(?=\W\D)/gi, "$1@Day @Month @YYYY") // spotted at [[Template:Infobox recurring event]]
 
// change datebirth templatesand withdeath calculationtemplates
.replace(/(\{\{(?:(?:Birth|Death|Disappeared) date(?: and (?:given |)age|)|launch|release|start)[^}]*\|)m(f=ye?s?)/gi, "$1d$2") // substitute df for mf
.replace(/(\{\{(?:(?:Birth|Death|Disappeared) date(?: and (?:given |)age|)|launch|release|start|film ?date|Wayback)[\|\d]+)(\}\})/gi, "$1|df=y$2") // insert df parameter
.replace(/(\{\{(?:(?:Birth|Death|Disappeared) date(?: and (?:given |)age|)|launch|release|start|film ?date|Wayback)[\|\d]+\|)df=ye?s?\|([^}]*df=ye?s?)/gi, "$1$2") // put date format parameter at the end
.replace(/(\{\{(?:start date|end date)\|)df=ye?s?\|((?:1[789]|20)\d{2}\}\})/gi, "$1$2") // date format parameter not needed – year only
 
Line 799:
}
 
function ohc_Bigendian_ref_datesohc_refs_to_yyyymmdd(editor) {
editor
// Add a tag to the summary box
.ohc_regex(/(\|\s*(?:archive|access)-?date\s*=\s*)@Day @Month @YYYY/gi, "$1@YYYY-@MM-@DD") // conversion of dmy dates to ymd within citation templates
.replace(/(\{\{use (?:mdy|dmy) dates)(?:\|cs1-dates=\w{1,2}|)(\|[^\}\|]+\}\})/gi, "$1|cs1-dates=y$2");
.ohc_regex(/(\|\s*(?:archive|access)-?date\s*=\s*)@Month @Day, @YYYY/gi, "$1@YYYY-@MM-@DD") // conversion of mdy dates to ymd within citation templates
.replace(/(\{\{use (?:mdy|dmy) dates)(?:\|cs1-dates=\w{1,2}|)(\s*\|date=[^\}\|]+)(?=\}\})/gi, "$1$2|cs1-dates=y$2ly");
}
 
Line 830 ⟶ 832:
// step 2: look for a canonical template name (the above might not have had anthing to work on) ; if found continue, write new template else
var rxpdd = new RegExp("{{Use " + format + " dates", "gi"); // here we search for canonical form {{use xxx dates}} that has current date
var dflagfoundorigtext = editor.get().search(rxpdd);
var dflagfound = origtext.search(rxpdd);
if (dflagfound === -1) {
editor.prepend(var dflagtxt = "{{Use " + format + " dates|date=" + curryyyymm + "}}\r\n"); // not found so insert new template at topwithin ofhead wikitextmatter
// Strategy: line by line
// Strip comments and outside whitespace
// If the line begins with {{ or is blank
// If it has balanced {{ and }} and ends with }}, and has no content outside braces
// If it starts with short description prepend to result
// Else append to result
// Else if it has one more {{ than }} and starts with {{Multiple issues or a redirect
// Append to result
// Keep reading lines and append until a line that has the matching }}
// Otherwise insert the flag text before any trailing blank lines or comment-only lines
var lines = origtext.split(/\r*\n/);
var result= "";
var nesting = 0;
var inmulti = false;
for (let i = 0; i < lines.length; i++) {
var line = lines[i];
var linenosp = line.replaceAll(/<!--.*?-->/g, "").replaceAll(/".*?"/g, '""').replaceAll(/\s+/g, "");
var openbrct = linenosp.includes("{{") ? linenosp.match(/\{\{/g).length : 0;
var closebrct = linenosp.includes("}}") ? linenosp.match(/}}/g).length : 0;
var hascontent = !linenosp.startsWith("{{") || !linenosp.endsWith("}}") || linenosp.search(/}}[^{]+?\{\{/) >= 0;
nesting += openbrct - closebrct;
inmulti |= (nesting > 0 && linenosp.search(/\{\{((multiple)?issues|mi)(\||$)/i) >= 0); // Several redirects in use; extend this list?
// The following are circumstances where we want to transfer the line now. If nesting ever goes negative something is badly wrong.
if (linenosp.length === 0 ||
(nesting === 0 && (!hascontent || inmulti)) || // If nesting goes to 0 when inmulti, this line closes the multi
(nesting > 0 && inmulti)) {
// Hoist Short Description, unless it is preceded on the same line by other text
if (i > 0 && linenosp.search(/^\{\{shortdesc/i) === 0 && !inmulti) {
result = line + "\r\n" + result;
} else {
result += line + "\r\n";
}
if (nesting === 0) {
inmulti = false;
}
} else {
// Detected end of preceding templates
// You might think that we should add a trailing newline if there was one in the original, but the source editor adds one unconditionally
editor.set((result.includes("\r\n") ? result.replace(/\r\n((\r\n)*)$/, "\r\n" + dflagtxt + "$1") : dflagtxt) + lines.slice(i).join("\r\n"));
return;
}
}
// Somehow there is no content, only hatnotes and stuff
editor.set(result + dflagtxt);
return; // and done
}
Line 872 ⟶ 920:
.replace(/(\{(?:See ?also|Main))(\|[^}]*)(\})/gi, protect_function)
.replace(/(\{\{(?:double ?|external ?|wide ?)image\s?\|)([^}]+)(\})/gi, protect_function)
.replace(/(\{\{(?:Single chart|harvnb|r|wikisource)\|)([^}]+)(\})/gi, protect_function)
.replace(/(\{\{(?:About-distinguish-text)\s*\|)([^}]+)(\}\})/gi, protect_function)
.replace(/(\{\{(?:Canadian federal by-election))([^}]+)(\}\})/gi, protect_function)
 
.replace(/(\[(?:https?:|ftp:))([^\]]*)(\])/gi, protect_function)
.replace(/(>[ ]*(?:https?:|ftp:))([^< >\]]*)([ ]*<)/gi, protect_function)
.replace(/(\|\s*archive-?url\s*=\s*https?:|ftp:)([^\s\|]*)(\s?\|)/gi, protect_function) //repurposed unused line to protect archive url links (archive.today)
.replace(/(<blockquote>)([\s\S]*?)(<\/blockquote>)/gi, protect_function)
.replace(/(<noinclude>)([\s\S]*?)(<\/noinclude>)/gi, protect_function)
Line 899 ⟶ 948:
// retraining redirects containing a date
.replace(/(\[\[)(?:September 3 Society)(\]\]|\|)/gi, "$1Jiusan Society$2")
.replace(/(\[\[)(?:June 4th incident)(\]\]|\|)/gi, "$1Tiananmen11989 Tiananmen Square protests ofand 1989massacre$2")
.replace(/(\[\[)(?:5@th? October (?:\(Serbia\)|overthrow))(\]\]|\|)/gi, "$1Overthrow of Slobodan Milošević$2")
.replace(/(\[\[)(?:October 5@th?(?: \(Serbia\)| overthrow|, 2000))(\]\]|\|)/gi, "$1Overthrow of Slobodan Milošević$2")
Line 1,240 ⟶ 1,289:
}
 
function ohc_dmy_publication_dates_driverohc_dmy_cs1_ly_driver(editor) {
ohc_extend_editor(editor);
 
//ohc_delink_dates(editor);
ohc_protect_dates(editor);
ohc_fix_unambiguous_dates(editor);
ohc_body_dates_to_dmy(editor);
ohc_customize_body_to_dmyohc_delink_ISO_to_dmy(editor);
ohc_dmy_publication_dates//ohc_delink_year_in_X(editor);
ohc_customize_all_to_dmy(editor);
ohc_ISO_to_dmy_in_citations(editor);
ohc_unprotect_dates(editor);
ohc_refs_to_yyyymmdd(editor);
ohc_MOSNUM_edit_summary(editor);
}
 
function ohc_mdy_cs1_ly_driver(editor) {
ohc_extend_editor(editor);
 
ohc_delink_dates(editor);
ohc_protect_dates(editor);
ohc_fix_unambiguous_dates(editor);
ohc_body_dates_to_mdy(editor);
ohc_delink_ISO_to_mdy(editor);
//ohc_delink_year_in_X(editor);
ohc_customize_all_to_mdy();
ohc_ISO_to_mdy_in_citations(editor);
ohc_unprotect_dates(editor);
ohc_refs_to_yyyymmdd(editor);
ohc_MOSNUM_edit_summary(editor);
}
 
function ohc_bigendian_driver(editor) {
ohc_refs_to_yyyymmdd(editor);
 
ohc_bigendien_edit_summary(editor);
}
 
function ohc_dmy_publication_dates_driver(editor) {
ohc_dmy_publication_dates(editor);
 
ohc_MOSNUM_edit_summary(editor);