User:Ingenuity/punctuationFixer.js

mw.util.addPortletLink("p-tb", "#", "Fix punctuation", "fix-punctuation");

function runPunctuationRegexes(text, list) { for (let item of list) { text = text.replaceAll(item[0], item[1]); }	return text; }

$('#fix-punctuation').on("click", async => {	try {		let title = mw.config.get("wgPageName");		let api = new mw.Api;		let data = await api.get({ action: "query", prop: "revisions", titles: title, rvslots: "*", rvprop: "content", formatversion: "2" });

let content = data["query"]["pages"][0]["revisions"][0]["slots"]["main"]["content"];

let regexes = [ [/ ?(]{0,30}")?>(?:(?!<\/ref>).)+?<\/ref>)/g, "$1"], [/ ?(]{0,30}" ?\/>)/g, "$1"], [/(]{0,30}")?>(?:(?!<\/ref>).)+?<\/ref>)([,\.\?\!\;])/g, "$2$1"], [/(]{0,30}" ?\/>)([,\.\?\!\;])/g, "$2$1"] ];		let cleaned = runPunctuationRegexes(content, regexes);

while (cleaned !== content) { content = cleaned; cleaned = runPunctuationRegexes(content, regexes); }

let token = (await api.get({ action: "query", format: "json", meta: "tokens" }))["query"]["tokens"]["csrftoken"];

await api.post({			title: title,			action: "edit",			text: cleaned,			summary: "Fixed punctuation using punctuationFixer.js.",			minor: true,			nocreate: true,			token: token		});

location.reload; } catch (e) { console.log("An error went wrong when trying to fix punctuation: " + e); } });