Jump to content

User:Steven Crossin/common.js

From Wikisource
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([
		// all namespaces
		{ name: 'clean up', script: cleanup },
		{ name: 'Indent', script: Indent },

		// page namespace
		{ name: 'Header', script: RunningHeader, forNamespaces: 'page' },
		{ name: 'Footer', script: RunningFooter, forNamespaces: 'page' },
		{ name: 'nop', script: nop, forNamespaces: 'page' },

		// user talk pages
		{ name: 'welcome', script: welcome, forNamespaces: 'user talk' },
		{ name: 'welcomeip & test', script: weliptest, forNamespaces: 'user talk' },
		{ name: 'welcomeip', script: welcomeip, forNamespaces: 'user talk' },
		{ name: 'toggle in Page:', script: HeaderToggle, forNamespaces: 'user talk' }
	]);

	/*********
	** Define scripts
	*********/
	//USER TALK namespace
	/**
	 * Apply {{welcome}}.
	 */
	function welcome(editor) {
		editor
			.append('{{welcome}} ~~~~')
			.appendEditSummary('welcome');
	}

	/**
	 * Apply {{test}} via {{subst:welcomeip}}.
	 */
	function weliptest(editor) {
		editor
			.append('{{subst:welcomeip}} ~~~~\n{{test}}')
			.appendEditSummary('welcome & test');
	}

	/**
	 * Add {{subst:welcomeip}}.
	 */
	function welcomeip(editor) {
		editor
			.append('{{subst:welcomeip}} ~~~~')
			.appendEditSummary('welcome');
	}

	/**
	 * Add {{subst:User:Billinghurst/HeaderToggle}} which tells how to toggle header in Page namespace.
	 */
	function HeaderToggle(editor) {
		editor.append('{{subst:User:Billinghurst/HeaderToggle}}');
	}

	//ALL NAMESPACE USE
	/**
	 * All sorts of text cleaning from OCR.
	 */
	function cleanup(editor) {
		editor
			// Digitized by Google (kill)
			.replace(/Digitized[\s\n]+by[\s\n]+Google/, '')

			// remove trailing spaces at the end of each line
			.replace(/ +\n/g, '\n')

			// remove trailing whitespace preceding a hard line break
			.replace(/ +<br *\/?>/g, '<br />')

			// remove trailing whitespace at the end of page text
			.replace(/\s+$/g, '')

			// remove trailing spaces at the end of refs
			.replace(/ +<\/ref>/g, '</ref>')

			// remove trailing spaces at the end of template calls
			.replace(/ +}}/g, '}}')

			// convert double-hyphen to mdash (avoiding breaking HTML comment syntax)
			.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:— ",
			// bearing in mind that the space will already be gone if at end of line).
			.replace(/ +— +/g, '—')

			// join words that are hyphenated across a line break
			// (but leave "|-" table syntax alone)
			.replace(/([^\|])-\n/g, '$1');

		// 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.
				// but not if there's a tag, template or table syntax either side of the line break
				.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')

			//OCR fixes
			// convert i9 to 19, etc.
			.replace(/[il]([0-9])/g, '1$1')

			// "the", "them", "their", etcetera
			.replace(/tlie/g, 'the')

			// "U" -> "ll" when preceded by a lowercase letter.
			.replace(/([a-z])U/g, '$1ll');
	}

	/**
	 * Add {{nop}} to the bottom of editbox.
	 */
	function nop(editor) {
		editor.append('{{nop}}');
	}

	//SPECIFIC TO Page: namespace
	/**
	 * Add {{RunningHeader}} to the headerbox.
	 */
	function RunningHeader(editor) {
		editor.forField('#wpHeaderTextbox').append('{{RunningHeader|||}}');
	}

	/**
	 * Footerbox app; for archaic books where the first word on the next page is placed at bottom.
	 */
	function RunningFooter(editor) {
		editor.forField('#wpFooterTextbox').replace(/(<\/div>)/, '{{RunningHeader|||}}$1');
	}

	//Indent
	function Indent(editor) {
		editor.forField('#wpHeaderTextbox').replace(/(<div class="pagetext">)/g, '<div class="pagetext" style="text-indent:1em;">');
	}
});
// </nowiki>

importScript('User:Inductiveload/Roman numerals.js');
importScript('User:Inductiveload/Running header.js');
if (mw.toolbar) {
	$('#wpTextbox1').wikiEditor('addToToolbar', {
		section: 'main',
		group: 'format',
		tools: {
			'custom-hws': {
				label: 'hws ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hyphenated word start|',
						post: '|}}',
						sampleText: ''
					}
				}
			},
			'custom-hwe': {
				label: 'hwe ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hyphenated word end|',
						post: '|}}',
						sampleText: ''
					}
				}
			}
		}
	});
}