User:Jfhutson/Regexp toolbar.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:pathoschild/templatescript.js]]
 */
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	/*********
	** Register scripts
	*********/
	pathoschild.TemplateScript.add([
	    { name: 'Leading {{nop}}', 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 {{ls}}', script: convert_long_s }
	]);
	
	/*********
	** Define scripts
	*********/
	function leading_nop(editor) {
		editor.prepend('{{nop}}\n\n');
	}
	
	function convert_long_s(editor) {
		editor.replace(/ſ/g, '{{ls}}');
	}
	
	function scriptstyle(editor) {
		editor.replace(/<math>(.*)<\/math>/g, '<math>\\scriptstyle\{$1\}</math>');
	}
	
	function colons(editor) {
		editor.replace(/::/g, '');
	}
	
	function smallcenter(editor) {
		editor.replace(/<center>([^<]*)<\/center>\n/g, '{{c|{{smaller|$1}}}}');
	}
	
	function poem(editor) {
		editor
			.prepend('<poem>')
			.append('</poem>');
	}
	
	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(/{{([Ffloat|[Bb]lock) center\|([\s\S]*)}}/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{{block center/s}}');
		editor.forField('#wpFooterTextbox').prepend('{{block center/e}}\n');
	}
	
	function sidenotes(editor) {
		editor.forField('#wpHeaderTextbox').append('\n{{sidenotes begin}}');
		editor.forField('#wpFooterTextbox').prepend('{{sidenotes end}}\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 <poem> tag:
		if (editor.get().indexOf("<poem") === -1) {
			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
				
				// 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 {{RunningHeader}} in to headerbox
	function RunningHeader(editor) {
		editor.forField('#wpHeaderTextbox').append('\n{{RunningHeader|||}}');
	}
	
	function convert_redirect(editor) {
		editor
			.replace(/#REDIRECT\s*(\[\[[^\]]*\]\])/g, '{{subst:dated soft redirect|$1}}')
			.setEditSummary('Replaced hard subpage redirect with soft redirect.')
			.options({ minor: false });
	}
});
// </nowiki>