User:Est. 2021/Script/AutoCleanup/sandbox.js

/*
Subpage of [[User:Est. 2021/Script/AutoCleanup.js]]
*/

// <syntaxhighlight lang="js">

$(function() {
	
	function clean_wikitext(text) {
		const replacements = [
//	[//g, ''], // demo

	[/\[\[\:([\w]+)\:([\w\s]+)\|\2\]\]/g, '{{ill|$2|$1}}'], // [[Template:ill]]
	[/\| *[\w]+ *\= *\|/g, '|'], // Remove empty parameters

// Reference spacing
//	[/(cite([^\}]+)*) *\| *([^\s\=\|\}]+) *\= *([^\s\=\|\}]+) *\| */g, '$1 |$3=$4 |'], // Fix spacing inline
//	[/(cite([^\}]+)*) *\| *([^\s\=\|\}]+) *\= *([^\s\=\|\}]+) *\}\}/g, '$1 |$3=$4}}'], // Fix spacing at end
//	[/(cite([^\}]+)*) *\| *([^\s][^\=\|\}]+[^\s]) *\= *([^\s][^\=\|\}]+[^\s]) *\| */g, '$1 |$3=$4 |'], // Fix spacing inline
//	[/(cite([^\}]+)*) *\| *([^\s][^\=\|\}]+[^\s]) *\= *([^\s][^\=\|\}]+[^\s]) *\}\}/g, '$1 |$3=$4}}'], // Fix spacing at end

// [[WP:NBSP]] // Watch out wikilinks
	[/([^\[\|]*) *(&(nbs)p;) *([^\]\|]*)/g, '$1{{$3}}$4'], // Replace "&nbsp;" with "{{nbs}}" #OWN #1
	[/([^\[\|]*) *(&(nbs)p;) *([^\]\|]*)/g, '$1{{$3}}$4'], // Replace "&nbsp;" with "{{nbs}}" #OWN #2
	[/(\{\{nbsp*\}\})+/gi, '$1'], // Delete repetitions #OWN
	[/(\[\[[^\]\|]*)(\{\{nbsp*\}\})([^\]\|]*)/g, '$1 $3'], // Watch out wikilinks #OWN

// Fluctuations
	[/\{\{(steady|(in|de)crease(neutral|(posi|nega)tive)*)\}\}(\{\{nbsp*\}\}| )*([+-\d\.,]+)/gi, '{{fluc|$6}}'], // increase/decrease/steady #OWN
	[/\{\{(steady|(in|de)crease(neutral|(posi|nega)tive)*)\}\}(\{\{nbsp*\}\}| )*(\€|\$)*([+-\d\.,]+)/gi, '{{fluc|$7|display=$6$7}}'], // increase/decrease/steady #OWN

// [[MOS:CIRCA]] #1
	[/([^\w\|])(c\.|circa)(\{\{nbs\}\}| )?([dhnrst\d\–\.,]+)(\{\{nbs\}\}| )(A\.?C\.?|A\.?D\.?|B?\.?C\.?E?\.?)([^\w\|])/g, '$1{{$2|$4 $6}}$7'], // Replace "c.{{nbs}}1500 BCE" with "{{circa|1500 BCE}}" #OWN
	[/([^\w\|])(c\.|circa)(\{\{nbs\}\}| )?([dhnrst\d\–\.,]+)([^\w\|])/g, '$1{{$2|$4}}$5'], // Replace "c.{{nbs}}1500" with "{{c.|1500}}" #OWN
	[/\{\{(c\.|circa)\|([0-9]{1,4})\}\} (A\.?C\.?|A\.?D\.?|B?\.?C\.?E?\.?)/g, '{{$1|$2 $3}}'], // Replace "{{c.|1500}} BCE" with "{{c.|1500 BCE}}" #OWN
	[/\{\{(c\.|circa)\}\} *([dhnrst\d\–\.,]+|[0-9]{1,4})/g, '{{$1|$2}}'], // Replace "{{c.}}1500" with "{{c.|1500}}" #OWN
	[/\{\{circa\|/gi, '{{c.|'], // Shorten "{{circa}}" to "{{c.}}" #OWN
	[/\{\{c\.\|\.\.\}\}/gi, 'c...'], // Repair wrongdoings #OWN

// [[MOS:CIRCA]] #2
	[/([^\w\|])(c\.|circa)(\{\{nbs\}\}| )?([dhnrst\d\–\.,]+)(\{\{nbs\}\}| )(A\.?C\.?|A\.?D\.?|B?\.?C\.?E?\.?)([^\w\|])/g, '$1{{$2|$4 $6}}$7'], // Replace "c.{{nbs}}1500 BCE" with "{{c.|1500 BCE}}" #OWN
	[/([^\w\|])(c\.|circa)(\{\{nbs\}\}| )?([dhnrst\d\–\.,]+)([^\w\|])/g, '$1{{$2|$4}}$5'], // Replace "c.{{nbs}}1500" with "{{c.|1500}}" #OWN
	[/\{\{(c\.|circa)\|([0-9]{1,4})\}\} (A\.?C\.?|A\.?D\.?|B?\.?C\.?E?\.?)/g, '{{$1|$2 $3}}'], // Replace "{{c.|1500}} BCE" with "{{c.|1500 BCE}}" #OWN
	[/\{\{(c\.|circa)\}\} *([dhnrst\d\–\.,]+|[0-9]{1,4})/g, '{{$1|$2}}'], // Replace "{{c.}}1500" with "{{c.|1500}}" #OWN
	[/\{\{circa\|/gi, '{{c.|'], // Shorten "{{circa}}" to "{{c.}}" #OWN
	[/\{\{c\.\|\.\.\}\}/gi, 'c...'], // Repair wrongdoings #OWN


// Others
//	[/'''(.+)'''(\n)+/g, ';$1$2'], // Headers #OWN #OFF

	[/(.)/g, '$1'], // @null
     ];
		for (const replacement of replacements) {
			text = text.replace(...replacement);
		}
		return text;
	}
	
	function process_summary(summary) {
		const ac_summary = 'testcases';
		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-s',
				title: 'AC Sandbox',
				type: 'info',
				autoHide: true,
				autoHideSeconds: 1
			}
		);
		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-s',
					title: 'AC Sandbox',
					type: 'success'
				}
			);
		} else {
			mw.notify(
				'No changes made',
				{
					tag: 'tol-autocleanup-s',
					title: 'AC Sandbox',
					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: 'AC Sandbox'});
					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( "AC/Sdisable" ) != "1" ) {
	    mw.util.addPortletLink( "p-cactions", window.location.href + '&AC/Sdisable=1', "Disable AC Sandbox", "pt-disableAC/S" );
		$(main);
	}
});

// </syntaxhighlight>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.