Jump to content

User:Est. 2021/Script/AutoCleanup.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
Fork of [[User:Qwerfjkl/scripts/autoCleanup.js]], based on [[User:Tol/AutoCleanup.js]]
*/

// <syntaxhighlight lang="js">

importScript('User:Est. 2021/Script/AutoCleanup/Tables.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/Tables.js]]

importScript('User:Est. 2021/Script/AutoCleanup/Templates.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/Templates.js]]

importScript('User:Est. 2021/Script/AutoCleanup/References.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/References.js]]

importScript('User:Est. 2021/Script/AutoCleanup/Quotes.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/Quotes.js]]

importScript('User:Est. 2021/Script/AutoCleanup/Functions.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/Functions.js]]

importScript('User:Est. 2021/Script/AutoCleanup/Minor.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/Minor.js]]

importScript('User:Est. 2021/Script/AutoCleanup/sandbox.js'); // Backlink: [[User:Est. 2021/Script/AutoCleanup/sandbox.js]]

$(function() {
	
	function clean_wikitext(text) {
		const replacements = [

// Piped links // [[MOS:PIPESTYLE]] & [[WP:NOPIPE]]
	[/\[\[(.*)\s\|([\s\w'-]*)\]\]/gi, '[[$1|$2]]'], // Fix wrongly-spaced piped links #OWN
	[/\[\[(.*)\|(\1)([\w']*)\]\]/gi, '[[$2]]$3'], // Fix piped links #OWN
	[/\[\[User\:(.*)\|\1\]\]/gi, '{{U|$1}}'], // Fix piped user-links #OWN
	[/\[\[User\:(.*)\|([\s\w'-]*)\]\]/gi, '{{U|$1|$2}}'], // Fix piped user-links (alias) #OWN

// Spacing
	[/(?:\u00A0|\u00AD|\u200A|\u200B|\u0009|\u0000-\u001F|\u0080-\u009F)/g, ' '], // Replace non-standard spacing with normal spacing #OWN // [[Help:CS1 errors#invisible char]]
	[/( )+/gm, '$1'], // Remove excess spacing #1 #OWN
	[/(\n[^\|\}].+) +\n/g, '$1\n'], // Remove lines' trailing spaces (that don't start with "|" or "}"
	[/ \n/g, '\n'], // Remove excess spacing before line break #1 #OWN

// Spacing +
	// [/([^\n\{ ])\| ([^\]])/g, '$1|$2'], // Remove excess spacing after "|" #OWN
	[/(\w) (\.[\s\n])/g, '$1$2'], // Remove spacing before dot #OWN
	[/(\n *\| *[\s\w'-]*) *\= */g, '$1 = '], // Fix spacing around equal signs (Part 1/2) #OWN
	[/(align|bgcolor|(col|row)span|scope|style) *\= */g, '$1='], // Fix spacing around equal signs (Part 2/2) #OWN

// Tags (spacing)
	[/ *\<[\/]*br *[\/]*\> */gi, '<br />'], // Remove excess spacing around line breaks #OWN // [[H:BR]]
	[/([^=\|]) (\<\!)/gi, '$1$2'], // Remove excess spacing before hidden tags #OWN
	[/([^\s\}]) (\})/g, '$1$2'], // Remove ending space from templates #OWN

// Newlines // Add newlines after specific templates
	[/((\n)\{\{(?:main|further|see also)\|(?:[^\}]+)\}\})(\s+)/gi, '$1$2'], // #OWN
	[/((\n)\{\{(?:main|further|see also)\|(?:[^\}]+)\}\})([^\s]+)/gi, '$1$2$3'], // OWN

// [[MOS:']] & [[MOS:"]] // [[MOS:CQ]]
	[/(?:‘|’|`|´|˹|˺)/g, '\''], // Replace curly single quotes with straight ones #CUSTOM
	[/(?:“|”)/g, '"'], // Replace curly double quotes with straight ones #CUSTOM
	[/(\")+/gm, '$1'], // Remove excess quotes #OWN

// [[MOS:NBSP]] & [[MOS:DASH]]
	[/(?:&mdash;)/g, '—'], // Replace "&mdash;" with "—" #OWN
	[/(?:&ndash;)/g, '–'], // Replace "&ndash;" with "–" #OWN
	[/(?:&nbsp;|\{\{(?:nbsp*|spaces)\}\})(?:–|—)/gi, '{{snd}}'], // Replace "{{nbs}}–" with "{{dash}}" #OWN
	[/(?:–|—)(?:&nbsp;|\{\{(?:nbsp*|spaces)\}\})/gi, '{{snd}}'], // Replace "–{{nbs}}" with "{{dash}}" #OWN
	[/ *(\{\{(?:nbsp*|spaces|snd|nbnd(?:ash)?|dash)\}\}) */gi, '$1'], // Remove spacing around "{{spaces}}" & "{{dash}}" #OWN

// [[MOS:NUM]]
	[/(\| *height *= *\{\{convert\| *[1-2]),([0-9]{1,2} *\| *m)/ig, '$1.$2'], // fix height with comma in {{convert}}
// Date formats
	[/(\| *)([0-9]{2})\/([0-9]{2})\/([0-9]{4})/g, '$1$4-$3-$2'], // replace "| 31/12/2015" with "| 2015-12-31"
//	[/(\| *)([0-9]{4})[–—−\/]([0-9]{2})[–—−\/]([0-9]{2})/g, '$1$2-$3-$4'], // replace "| 2015–12–31" with "| 2015-12-31" (endash/emdash/minus/slash to hyphen) #OFF
	[/(\| *)([0-9]{4})([–—−\/])([0-9]{2})[\3]([0-9]{2})/g, '$1$2-$4-$5'], // replace "| 2015–12–31" with "| 2015-12-31" (endash/emdash/minus/slash to hyphen) #CUSTOM
	[/([0-9]{1,2})\{\{nbs\}\}(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/g, '$1 $2'], // Remove "{{nbs}}" from dates #OWN // [[MOS:NUM#Non-breaking spaces]]

// [[MOS:HEAD]]
	[/^(=+) *\<(big\>)(.*?)\<\/\2 *\1$/gm, '$1 $3 $1'], // Remove "<big>" tags from headers #OWN
	[/^(=+) *(.*?) *\1$/gm, '$1 $2 $1'], // Standardise spacing inside headers
	[/\n+^((=+) .*? \2)$/gm, '\n\n$1'], // Standardise spacing before headers
	[/^((=+) .*? \2)$\n+/gm, '$1\n'], // Standardise spacing after headers #CUSTOM
	[/\n{3,}/g, '\n\n'], // Replace three or more newlines with two newlines #1

// [[MOS:FOOTERS]]
	[/\n(== *)External Links?( *==)/i, '\n$1External links$2'], // #CUSTOM
	[/\n(== *)Further Reading( *==)/i, '\n$1Further reading$2'],
	[/\n(== *)Published Papers( *==)/i, '\n$1Published papers$2'],
	[/\n(== *)See Also( *==)/i, '\n$1See also$2'],
	[/\n(== *)Selected Works( *==)/i, '\n$1Selected works$2'],

// Official websites
	[/(\| *website *\= *\{\{)Official website\|/gi, '$1URL|'], // #OWN
	[/\[(http[^ ]*) Official (web)?site\]/gi, '{{Official website|$1}}'], // #OWN

// Taken from pywikibot safe-syntax replaces, from the fixes.py script
	[/\[\[(?<url>https?:\/\/[^\]]+?)\]\]/g, '[$1]'], // external link in double brackets
	[/\[\[(?<url>https?:\/\/.+?)\]/g, '[$1]'], // external link starting with double bracket
	[/\[(?<url>https?:\/\/[^\|\] \r\n]+?) +\| *(?<label>[^\|\]]+?)\]/, '[$1 $2]'], // external link and description separated by a dash, with whitespace in front of the dash, so that it is clear that the dash is not a legitimate part of the URL.
	[/\[(?<url>https?:\/\/[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?<label>[^\|\]]+?)\]/, '[$1 $3]'], // dash in external link, where the correct end of the URL can be detected from the file extension. It is very unlikely that this will cause mistakes.
	[/\[\[([^\|\]]+)\|\1\]\]/, '[[$1]]'], // link equal to linktext

// Taken from [[User:Sam Sailor/Scripts/autoFormatterSettings.js]]
	// [/ =\n/g, ' = \n'], // add trailing space after = for empty infobox parameters #OFF
	[/\{\{ /g, '{{'], // remove space after {{
	[/\|tau\]\]/g, '|τ]]'], // fix unwanted change in category sorting
// spaces after bullets
	[/(\n[*#]+) *([^\s*#:;R])/g, '$1 $2'], // space after one or more * or # at start of line

// End cleaning
	[/( )+/gm, '$1'], // Remove excess spacing #2 #OWN
	[/ \n/g, '\n'], // Remove excess spacing before line break #2 #OWN
	[/\n{3,}/g, '\n\n'], // Replace three or more newlines with two newlines #2
      ];
		for (const replacement of replacements) {
			text = text.replace(...replacement);
		}
		return text;
	}
	
	function process_summary(summary) {
		const ac_summary = 'cleanup via [[User:Est. 2021/Script/AutoCleanup.js|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: true,
				autoHideSeconds: 2
			}
		);
		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);
			if (mw.util.getParamValue('section') != 'new') { // not section title instead of summary
			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);
            
    if (mw.config.get('wgPageContentModel') === 'wikitext' && mw.config.get('wgAction') === 'edit' && mw.util.getParamValue( "ACdisable" ) != "1" ) {
	    mw.util.addPortletLink( "p-cactions", window.location.href + '&ACdisable=1&AC/TABdisable=1&AC/TLdisable=1&AC/Rdisable=1&AC/Qdisable=1&AC/Fdisable=1&AC/Mdisable=1&AC/Sdisable=1', "Disable AutoCleanup", "pt-disableAC" );
		$(main);
	}
});

// </syntaxhighlight>