User:DutchTreat/common.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.

/* Refer to User:Moondyne/common.js */

// refer User:William Maury Morris II/common.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 [[File:pathoschild/templatescript.js]]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	pathoschild.TemplateScript.add([
		// clean up pages
		{
			name: 'Cleanup',
			forNamespaces: 'page',
			script: function(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, '')
				
					// 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, '');

				// 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')
						
						// collapse sequences of spaces into a single space
						.replace(/  +/g, ' ');
				}

				editor
					// remove unwanted spaces around punctuation marks
					.replace(/ ([;:\?!,])/g, '$1')
					
					// Replace smart double quotes with straight double quotes
					.replace(/[\u201C\u201D\u201E\u201F\u2033\u2036]/g, '"')
					
					// Replace smart single quotes with straight single quote (apostrophe)
					.replace(/[\u2018\u2019\u201A\u201B\u2032\u2035]/g, "'")
					
					// OCR SCANNOS
					.replace(/tlie/g, 'the')             // "the", "them", "their", etcetera
					.replace(/([a-z])U/g, '$1ll')        // "U" -> "ll" when preceded by a lowercase letter.
					.replace(/--/g, '—')                 // convert double-hyphen to mdash
					.replace(/-—/g, '—')                 // convert ndash mdash to mdash
					.replace(/—-/g, '—')                 // convert mdash ndash to mdash
					.replace(/[il]([0-9])/g, '1$1')      // convert i9 to 19, etc.
					.replace(/labcur/g, 'labour')
					.replace(/I-I/g, 'H')
					.replace(/Fromantle/g, 'Fremantle')
					.replace(/Eremantle/g, 'Fremantle')
					.replace(/Oolonel/g, 'Colonel')
					.replace(/ aud /g, ' and ')
					.replace(/Govermnent/g, 'Government')
					.replace(/dicult/g, 'difficult')
					.replace(/ ocer/g, ' officer')
					.replace(/ ocial/g, ' official')
					.replace(/ \uFFFD(?=[1-9])/g, ' \u00A3')   // � --> £
					.replace(/ iu/g, ' in')
					//.replace(/ tile/g, ' the')
					.replace(/Oouucil/g, 'Council')
					.replace(/ laud/g, ' land')
					.replace(/ Loudon/g, ' London')
					//.replace(/ ou /g, ' on ')
					.replace(/\u005E/g, ',')                   // ^ --> ,
					.replace(/Digitized by\s/g, '')
					.replace(/Google/g, '');
			}
		},

		// small caps
		{
			name: 'Smallcaps',
			forNamespaces: 'page',
			script: function(editor) {
				editor
					.replaceSelection(function(selected) {
						// Applying small-caps to all-caps text is pointless...
						// ... unless the all-caps is OCR of text that is actually small-caps.
						// Check if text is all-caps, and if it is, convert it to title case before applying small-caps.
						if (selected == selected.toUpperCase())
							selected = titlecase(selected);
						return '{{small-caps|'+selected+'}}';
					});
			}
		}
	]);
});