User:Ostrea/typography.js

/*

This page defines a TemplateScript library. It's not meant to be referenced directly. See TemplateScript for usage.

/* global $, pathoschild */

/** * 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 {	/*********	** Define library	*********/	pathoschild.TemplateScript.library.define({ key: 'wikisource.typography', name: 'Typography tools', url: '//en.wikisource.org/wiki/Wikisource:TemplateScript#Typography', description: 'A set of scripts for editing typography: add diacritics (like àéîö) or manipulate text case.', categories: [ {				name: 'Typography tools', scripts: [ //		{ key: 'title-link', name: 'Make title link', script: maketitle, accessKey: 't' }, //		{ key: 'author-link', name: 'Make author link', script: author, accessKey: 'a' }, { key: 'small-caps', name: 'Convert to Sᴍᴀʟʟ Cᴀᴘs', script: smallcaps, accessKey: 'c' }, { key: 'upper', name: 'Convert to UPPER', script: upper, accessKey: 'u' }, //		{ key: 'diaresis', name: 'Add diaeresis (ä)', script: diaeresis, accessKey: ':' }, //		{ key: 'acute', name: 'Add acute (á)', script: acute, accessKey: '\'' }, //		{ key: 'grave', name: 'Add grave (à)', script: grave, accessKey: '`' }, //		{ key: 'circumflex', name: 'Add circumflex (â)', script: circumflex, accessKey: '^' }, //		{ key: 'macro', name: 'Add macron (ā)', script: macron, accessKey: '_' }, //		{ key: 'misc', name: 'Add ligature or hook', script: catchall, accessKey: '-' } ]			}		]	});

/*********	** Private methods *********/	/**	 * Convert the text to title case based on English rules. * @param {string} text The text to convert. */	var _titlecase = function(text) { // split text into individual words and examine them one by one var words = text.toLowerCase.split(' '); for (var i in words) { switch(words[i]) { case "a": case "an": case "and": case "as": case "at": case "but": case "by": case "etcetera": case "etc.": case "for": case "from": case "in": case "nor": case "of": case "o'": case "on": case "or": case "the": case "to": case "with": case "versus": case "vs.": case "v.": case "yet": break; // don't capitalise articles, "to" as part of an infinitive, prepositions or short conjunctions default: // capitalise everything else words[i] = words[i].substring(0, 1).toUpperCase + words[i].substring(1, words[i].length); break; }		}

// capitalise first word regardless words[0] = words[0].substring(0, 1).toUpperCase + words[0].substring(1, words[0].length);

// capitalise last word regardless var last = words.length - 1; words[last] = words[last].substring(0,1).toUpperCase + words[last].substring(1, words[last].length);

// reconstruct title var titleCase = ""; for (i in words) { titleCase += words[i]; if (i < last) titleCase += " "; }

return titleCase; };

/*********	** Script methods *********/	/**	 * Convert the selected text into an appropriately-capitalised title link. * For example, this replaces "THE MAN FROM SNOWY RIVER" with * "THE MAN FROM SNOWY RIVER". * @param {object} editor The script helpers for the page. */	function maketitle(editor) { editor.replaceSelection(function(text) {			var target = _titlecase(text);			return target == text				?  + text + 				:  + text + ;		}); }

/**	 * Convert the selected text into an author link (with support for common	 * cases like "Smith, John" → Smith, John). * @param {object} editor The script helpers for the page. */	function author(editor) { editor.replaceSelection(function(name) {			// if name contains a comma, switch around when linking			var commaPos = name.indexOf(',');			var target = commaPos === -1				? name				: name.substr(commaPos+1).replace(/^\s+|\s+$/g, ) + ' ' + name.substr(0, commaPos).replace(/^\s+|\s+$/g, );

// if name is all in capitals, convert target to title case if (target == target.toUpperCase) target = _titlecase(target);

return  + name + ; });	}

/**	 * Convert the selected text to Sᴍᴀʟʟ Cᴀᴘs using. * @param {object} editor The script helpers for the page. */	//Mark selected text up with small-caps function smallcaps(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 ''; });	}

/**	 * Convert the selected text to UPPERCASE. * @param {object} editor The script helpers for the page. */	function upper(editor) { editor.replaceSelection(function(selected) {			return selected.toUpperCase;		}); }

/**	 * Add miscellaneous diacritics to the selected character or pair. * @param {object} editor The script helpers for the page. */	function catchall(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				// fixes that are one character long				case 'c':					return 'ç';				case 'C':					return 'Ç';				case 'ç':					return 'c—'; // so that if I wanted an mdash I can simply invoke it again				case 'Ç':					return 'C—'; // so that if I wanted an mdash I can simply invoke it again				// fixes that are two characters long				case 'ae':					return 'æ';				case 'AE':					return 'Æ';				case 'oe':					return 'œ';				case 'OE':					return 'Œ';				// final catchall: insert em dash				default:					return selected + '—';			}		}); }

/**	 * Modify a selected character (like 'a') with a diaeresis (like 'ä'). * @param {object} editor The script helpers for the page. */	function diaeresis(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				case 'a':					return 'ä';				case 'A':					return 'Ä';				case 'e':					return 'ë';				case 'E':					return 'Ë';				case 'i':					return 'ï';				case 'I':					return 'Ï';				case 'o':					return 'ö';				case 'O':					return 'Ö';				case 'u':					return 'ü';				case 'U':					return 'Ü';				case 'y':					return 'ÿ';				default:					return selected;			}		}); }

/**	 * Modify a selected character (like 'a') with an acute accent (like 'á'). * @param {object} editor The script helpers for the page. */	function acute(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				case 'a':					return 'á';				case 'A':					return 'Á';				case 'c':					return 'ć';				case 'C':					return 'Ć';				case 'e':					return 'é';				case 'E':					return 'É';				case 'g':					return 'ģ';				case 'i':					return 'í';				case 'I':					return 'Í';				case 'o':					return 'ó';				case 'O':					return 'Ó';				case 'l':					return 'ĺ';				case 'L':					return 'Ĺ';				case 'n':					return 'ń';				case 'N':					return 'Ń';				case 'r':					return 'ŕ';				case 'R':					return 'Ŕ';				case 's':					return 'ś';				case 'S':					return 'Ś';				case 'u':					return 'ú';				case 'U':					return 'Ú';				case 'y':					return 'ý';				case 'Y':					return 'Ý';				case 'z':					return 'ź';				case 'Z':					return 'Ź';				default:					return selected;			}		}); }

/**	 * Modify a selected character (like 'a') with a grave accent (like 'à'). * @param {object} editor The script helpers for the page. */	function grave(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				case 'a':					return 'à';				case 'A':					return 'À';				case 'e':					return 'è';				case 'E':					return 'È';				case 'i':					return 'ì';				case 'I':					return 'Ì';				case 'o':					return 'ò';				case 'O':					return 'Ò';				case 'u':					return 'ù';				case 'U':					return 'Ù';				default:					return selected;			}		}); }

/**	 * Modify a selected character (like 'a') with a macron (like 'ā'). * @param {object} editor The script helpers for the page. */	function macron(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				case 'a':					return 'ā';				case 'A':					return 'Ā';				case 'e':					return 'ē';				case 'E':					return 'Ē';				case 'i':					return 'ī';				case 'I':					return 'Ī';				case 'o':					return 'ō';				case 'O':					return 'Ō';				case 'u':					return 'ū';				case 'U':					return 'Ū';				default:					return selected;			}		}); }

/**	 * Modify a selected character (like 'a') with a circumflex (like 'â'). * @param {object} editor The script helpers for the page. */	function circumflex(editor) { editor.replaceSelection(function(selected) {			switch(selected) {				case 'a':					return 'â';				case 'A':					return 'Â';				case 'c':					return 'ĉ';				case 'C':					return 'Ĉ';				case 'e':					return 'ê';				case 'E':					return 'Ê';				case 'g':					return 'ĝ';				case 'G':					return 'Ĝ';				case 'h':					return 'ĥ';				case 'H':					return 'Ĥ';				case 'i':					return 'î';				case 'I':					return 'Î';				case 'j':					return 'ĵ';				case 'J':					return 'Ĵ';				case 'o':					return 'ô';				case 'O':					return 'Ô';				case 's':					return 'ŝ';				case 'S':					return 'Ŝ';				case 'u':					return 'û';				case 'U':					return 'Û';				case 'w':					return 'ŵ';				case 'W':					return 'Ŵ';				case 'y':					return 'ŷ';				case 'Y':					return 'Ŷ';				default:					return selected;			}		}); } }); //