User:Tol/AutoCleanup.js

/*Licensing:* Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0)*: https://creativecommons.org/licenses/by-sa/3.0/* Creative Commons Attribution-ShareAlike 4.0 (CC BY-SA 4.0)*: https://creativecommons.org/licenses/by-sa/4.0/* GNU Lesser General Public License (GNU LGPL)*: https://www.gnu.org/copyleft/lgpl.html* GNU Free Documentation License (GNU FDL)*: http://www.gnu.org/licenses/fdl-1.3.htmlAttribution:* [[User:Tol]] [https://www.search.com.vn/wiki/en/User:Tol]*/// <syntaxhighlight lang="js">$(function() {function clean_wikitext(text) {const replacements = [[/‘|’/g, '\''], // Replace curly single quotes with straight ones[/“|”/g, '"'], // Replace curly double quotes with straight ones[/ +\n/g, '\n'], // Remove lines' trailing spaces[/^(=+) *(.*?) *\1$/gm, '$1 $2 $1'], // Standardise spacing inside headers[/\n+^((=+) .*? \2)$/gm, '\n\n$1'], // Standardise spacing before headers[/^((=+) .*? \2)$\n+/gm, '$1\n\n'], // Standardise spacing after headers[/\n{3,}/g, '\n\n'], // Replace three or more newlines with two newlines   [/(?<!^|\||=) {2,}(?! *[\|=])/gm, ' '] // Replace two or more spaces (not preceded or followed by a pipe or equals sign nor at the beginning of a line) with one space];for (const replacement of replacements) {text = text.replace(...replacement);}return text;}function process_summary(summary) {const ac_summary = 'cleanup (via [[User:Tol/AutoCleanup|script]])';if (summary) {if (/^\/\*.*?\*\/ $/.test(summary)) { // auto summaryreturn summary + ac_summary;} else {return summary + '; ' + ac_summary;}} else {return ac_summary;}}function main() {mw.notify('Processing...',{tag: 'tol-autocleanup',title: 'AutoCleanup',type: 'info',autoHide: false});let text_box = $('#wpTextbox1');let old_text = text_box.textSelection('getContents');let new_text = clean_wikitext(old_text);if (old_text != new_text) {text_box.textSelection('setContents', new_text);let summary_box = $('#wpSummary');summary_box.val(process_summary(summary_box.val()));mw.notify('Done',{tag: 'tol-autocleanup',title: 'AutoCleanup',type: 'success'});} else {mw.notify('No changes made',{tag: 'tol-autocleanup',title: 'AutoCleanup',type: 'info'});}}function add_button() {try {if ($('editform').length) {mw.loader.using(['oojs-ui', 'oojs-ui-widgets'], function() {let button = new OO.ui.ButtonWidget({label: 'AutoCleanup'});button.on('click', main);$('.editButtons').append(button.$element);});}} catch(error) {alert(error);}}//$(add_button);$(main);});// </syntaxhighlight>