Content deleted Content added
documentation; don't add same edit summary twice |
set event handler below definition of event handler (JavaScript hoisting of variable declarations is bizarre and the previous code might be wrong, but I don't know) |
||
(28 intermediate revisions by the same user not shown) | |||
Line 1:
/* <nowiki>
Moves refs and citation needed tags after punctuation.
*/
var namespaceNumber = mw.config.values.wgNamespaceNumber;▼
// Add a link just above the edit box, if you're in the main namespace.▼
if ( namespaceNumber === 0 )▼
$("#editform").prepend('<div id="footnote-cleanup"><a href="javascript:cleanupFootnotes()">clean up footnotes</a></div>');▼
▲// Add a link just above the edit box, if you're in the main or Draft namespace.
▲if ( namespaceNumber === 0 || namespaceNumber === 118 )
{
mw.loader.load("//en.wiktionary.org/w/index.php?title=User:Erutuon/styles/wikitext-cleanup.css&action=raw&ctype=text/css", "text/css");
if ( textbox )▼
var cleanUpFootnotes = function ()
{
var
const oldContents = textbox.val();
var contents = oldContents;
var escaped = [];
var i = 0;
var replacements = [];▼
var count = 0;
var escape = function(text, regexString)
Line 30 ⟶ 36:
{
escaped[i] = match;
var replacement = "%%" + i + "%%";
i += 1;
return replacement;
}
);
return text;
var reorder = function(match, capture1, capture2)
count += 1;
return replacement;
};
var fixPunctuationPlacement = function(text)
{
while ( puncRegex.test(text) )
text = text.replace(
/((?:%%\d+%%)+)([\.\,\;\:\"]{1,3})/g,
reorder
);
return text;
};
Line 42 ⟶ 69:
contents = escape(
contents,
"<ref[^>]*>[^<]+<\\/ref>"
);
// footnote templates▼
contents = escape(
contents,
"
);
Line 54 ⟶ 79:
contents = escape(
contents,
"\\{\\{(?:[Cc]itation needed|[Cc]n|[Ff]act|[Cc]b|[Cc]tn|[Rr]ef\\?)\\|[^\}]+\\}\\}"
);
contents = fixPunctuationPlacement(contents);
▲ // footnote templates
/* Handles up to one level of nested templates.
Any more, and there may be problems. */
contents = escape(
contents,
"\\{\\{(?:sfn|efn|rfn)\\|(?:[^\\}]*?(?:\\{\\{[^\\}]+\\}\\})?)+\\}\\}"
);
Line 62 ⟶ 97:
}
contents = fixPunctuationPlacement(contents);
▲ var punctuation = "[\.\,\;\:\)]{1,3}";
mw.notify(count + " correction" + ( ( count > 1 && "s" ) || "" ) + " made: " + replacements.join());▼
▲ var replacements = [];
}
This has to be done twice, since escaping was done twice. */
contents = contents.replace(
/
function(
number = Number(number);
▲ {
▲ var replacement = capture2 + capture1;
▲ replacements.push(replacement);
▲ return replacement;
}
);
▲ mw.notify(count + " correction" + ( ( count > 1 && "s" ) || "" ) + " made: " + replacements.join());
▲ // Unescape the various things escaped above.
contents = contents.replace(
/%%(\d+)%%/g,
Line 92 ⟶ 124:
if ( isUnchanged )
{
mw.notify("No misplaced footnotes or tagging templates were found.");
}
Line 98 ⟶ 130:
$("#wpSummary").val(function(index, summary)
{
var addition = "made sure refs are after punctuation with [[User:Erutuon/footnoteCleanup.js|JavaScript]]; see [[WP:REFPUNC]]";▼
mw.notify(summary);▼
▲ var addition = "made sure refs are after punctuation with [[User:Erutuon/footnoteCleanup.js|JavaScript]]";
▲ afterSectionName = summary.match(/^\/\*[^\*]+\*\/\s+(.+)/);
if ( afterSectionName && afterSectionName[1].length > 1 )
Line 109 ⟶ 139:
}
if ( !isUnchanged && ( !afterSectionName || !afterSectionName[1].includes(addition) ) )
▲ {
return summary + addition;
}
);
};
if ( !$("#wikitext-cleanup-button-wrapper").length )
$("#editform").prepend('<div id="wikitext-cleanup-button-wrapper"></div>');
$("#wikitext-cleanup-button-wrapper")
▲
$("#footnote-cleanup")
.click(cleanUpFootnotes);
}
// </nowiki>
|