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

Content deleted Content added
copied fix thanks to User:DavidBrooks
integrated modifications by User:DavidBrooks
Line 830:
// 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
.replace(/(\{\{[\t_ ]*use[\t_ ]+(?:dmy|mdy)[\t_ ]+dates[^{}]*\}\})([\r\n\t ]*)(\{\{[\t ]*short description[^{}]*\}\})/gi, "$3$2$1"); // repositioning "short description" template at the top
// 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
}