User:Tol/AutoCleanup.js
Appearance
< User:Tol
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Tol/AutoCleanup. |
/*
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.html
Attribution:
* [[User:Tol]] [https://en.wikipedia.org/wiki/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 summary
return 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>