User:Mpaa/Regexp toolbar.js

/** * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. * @see https://meta.wikimedia.org/wiki/TemplateScript * @update-token */ // $.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function {	/*********	** Register scripts	*********/	pathoschild.TemplateScript.add([ { name: 'Leading ', script: leading_nop }, //{ name: 'Metadata', script: add_metadata }, { name: 'Rh', script: RunningHeader }, //{ name: 'Rh (lookup)', script: set_running_header }, { name: 'Clean up', script: cleanup }, { name: 'Scriptstyle', script: scriptstyle }, { name: 'Derelative', script: derelative }, { name: 'Single returns', script: singleReturns }, { name: 'Quotes', script: quotes }, { name: 'Block center mid', script: blockcentermid }, { name: 'Convert to blk-c mid', script: blockcenterconvert }, { name: 'Sidenotes', script: sidenotes }, { name: 'Colons', script: colons }, { name: 'Poem', script: poem }, { name: 'Small center', script: smallcenter }, { name: 'Convert redirect', script: convert_redirect }, { name: 'Convert ſ to ', script: convert_long_s } ]);	/*********	** Define scripts	*********/	function leading_nop(editor) {		editor.prepend('\n\n');	}	function convert_long_s(editor) {		editor.replace(/ſ/g, );	}	function scriptstyle(editor) {		editor.replace(/(.*)<\/math>/g, '$$\\scriptstyle\{$1\}$$');	}	function colons(editor) {		editor.replace(/::/g, );	}	function smallcenter(editor) {		editor.replace(/ ([^<]*)<\/center>\n/g, '');	}	function poem(editor) {		editor			.prepend(' ')			.append(' ');	}	function derelative(editor) {		editor.replace(/\[\[\/(.*)\/\]\]/g, '\[\[$1\|$1\]\]');	}	function singleReturns(editor) {		editor			// remove single line breaks; preserve multiple.			.replace(/([^>\n])\n([^<\n])/g, '$1 $2') //linux			.replace(/([^>\r\n])\r\n([^<\r\n])/g, '$1 $2'); //windows	}	function quotes(editor) {		editor			.replace(/[\u201C\u201D]/g, '\x22') //double smart quotes			.replace(/[\u2018\u2019\x60]/g, '\x27') //curly single quotes and backticks .replace(/&quot;/g, '\x22'); //curly single quotes and backticks }	function blockcenterconvert(editor) { editor.replace(//g, '$2'); //remove block centres in the main block blockcentermid(editor); //add split BC in the header/footer }	function blockcentermid(editor) { editor.forField('#wpHeaderTextbox').append('\n'); editor.forField('#wpFooterTextbox').prepend('\n'); }	function sidenotes(editor) { editor.forField('#wpHeaderTextbox').append('\n'); editor.forField('#wpFooterTextbox').prepend('\n'); }	function cleanup(editor) { quotes(editor); editor // remove trailing whitespace at the end of each line .replace(/ \n/g, '\n') // remove trailing whitespace at the end of input .replace(/\s+$/g, '') // convert double-hyphen to mdash .replace(/([^\!])--([^>])/g, '$1—$2') // remove spacing around mdash, but only if it has spaces on both sides // (we don't want to remove the trailing space from "...as follows:— "			.replace(/ +— +/g, '—')			// join words that are hyphenated across a line break			.replace(/['-]\n/g, '')			// " word -> "word, only if preceded by a space.			.replace(/ \x22 /g, ' \x22');		// stuff to do only if the page doesn't contain a tag:		if (editor.get.indexOf("\n])\n([^<\n])/g, '$1 $2') //linux				.replace(/([^>\r\n])\r\n([^<\r\n])/g, '$1 $2') //windows				// collapse sequences of spaces into a single space				.replace(/ +/g, ' ');		}		editor			// remove unwanted spaces around punctuation marks			.replace(/ ([;:\?!,])/g, '$1')			// : — to :—			.replace(/: +—/g, ':—')			//OCR fixes			// convert i9 to 19, etc.			.replace(/[il]([0-9])/g, '1$1')			// "the", "them", "their", etcetera			.replace(/t[fl]ie/g, 'the') // "liere" .replace(/(\s)liere(\s)/g, '$1here$2') // "aud" to "and" .replace(/(\s)aud(\s)/g, '$1and$2') // "ét" to "ct" (ligature causes this) .replace(/ét/g, 'ct') // "^V", "AV", "\\" to "W" .replace(/\^V/g, 'W') .replace(/\AV[a-z]/g, 'W') .replace(/\\\\/g, 'W') .replace(/\[Il]\^/g, 'L') // remove "�" .replace(/�/g, '') // "U" -> "ll" when preceded by a lowercase letter. .replace(/([a-z])U/g, '$1ll'); }	// RunningHeader - puts in to headerbox function RunningHeader(editor) { editor.forField('#wpHeaderTextbox').append('\n'); }	function convert_redirect(editor) { editor .replace(/#REDIRECT\s*(\[\^\*\]\])/g, '$1') .setEditSummary('Replaced hard subpage redirect with soft redirect.') .options({ minor: false }); } }); //