User:Omegatron/monobook.js/headingformattingfixer.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:Omegatron/monobook.js/headingformattingfixer. |
/*
== Heading formatter ==
This script adds a tab to fix some formatting in the article. When you press the tab, it adds whitespace to space headings the way they are when you press the '''+''' button on a talk page, and it also removes extra newlines in the article.
Then it adds an edit summary and presses ''Show changes'' for you so you can check it for mistakes.
By [[User:Omegatron]]
<pre><nowiki> */
function headingfixer() {
var txt = document.editform.wpTextbox1;
// Format heading markup the way it is generated with the + tab
txt.value = txt.value.replace(/((^|\n)={2,})( |\t)*(.*?)( |\t)*(={2,})/g, '$1 $4 $6\n\n');
// 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');
// Format External links and See also according to the Manual of Style
txt.value = txt.value.replace(/\=\= external links? \=\=/ig, '== External links ==');
txt.value = txt.value.replace(/\=\= see also \=\=/ig, '== See also ==');
// Add a tag to the summary box
var txt = document.editform.wpSummary;
var summary = "[[User:Omegatron#Regular expressions|Regex heading fixer]]";
if (txt.value.indexOf(summary) == -1) {
if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
txt.value += " | ";
}
txt.value += summary;
}
// Press the diff button to check it
document.editform.wpDiff.click()
}
// </nowiki></pre>