User:Waldyrious/whitespacefixer.js
Appearance
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. |
Documentation for this user script can be added at User:Waldyrious/whitespacefixer. |
// Fork of [[User:Omegatron/monobook.js/headingformattingfixer.js]],
// without the code to normalize headings
// (i.e. add spaces within the equal signs, and newlines after the headings)
// since it produced dirty diffs too often.
// Diff of the original fork: https://en.wikipedia.org?diff=625426859&oldid=625426743
function whitespacefixer() {
var txt = document.editform.wpTextbox1;
// 1 blank line before the headings
// (this is super-kludgy because of the newlines conflicting when one heading is right after another)
// (seems to work, though, for now. some other day...)
txt.value = txt.value.replace(/(\n+={2,}) (.*?) (={2,})/g, '\n\n$1 $2 $3');
// Remove all extra newlines?
txt.value = txt.value.replace(/(\n[ \t\v\r\f]*){2,}/g, '\n\n');
// One space after * and # lists, and after : and ;
txt.value = txt.value.replace(/(^|\n)((\*|\#|\;|\:)+)\s*(\w|\[|\"|\'|\{\{)/g, '$1$2 $4');
// Add a tag to the summary box
var editSummary = document.editform.wpSummary;
var appendToSummary = "cleanup whitespace (using [[w:en:User:Waldir/whitespacefixer.js|Regex whitespace fixer]])";
if (editSummary.value.indexOf(appendToSummary) == -1) {
if (editSummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
editSummary.value += "; ";
}
editSummary.value += appendToSummary;
}
}
$(function () {
if(document.forms.editform) {
mw.util.addPortletLink('p-cactions', 'javascript:whitespacefixer()', '_', 'ca-whitespacefixer', 'Fix whitespace', '-', '');
}
});