User:Susanarb/common.js

/* global $, mw, pathoschild */

/** * Add several tools useful when proofreading pages in the Page: namespace. * @see https://en.wikisource.org/wiki/Wikisource:TemplateScript */ mw.loader.load('//en.wikisource.org/w/index.php?title=MediaWiki:TemplateScript/proofreading.js&action=raw&ctype=text/javascript');

/** * Add several tools for editing typography like diacritics and letter case. * @see https://en.wikisource.org/wiki/Wikisource:TemplateScript */ mw.loader.load('//en.wikisource.org/w/index.php?title=MediaWiki:TemplateScript/typography.js&action=raw&ctype=text/javascript');

/** * 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([ // stuff I routinely do at the start (Page ns only) { name: 'hws', script: hws, forNamespaces: 104 }, { name: 'hwe', script: hwe, forNamespaces: 104 },

// stuff I only do via shortcuts; if I could create these shortcuts without adding them to the sidebar, I would. { name: 'center', script: makeCenter, accessKey: 'c' }, { name: 'right', script: makeRight, accessKey: 'r' }, { name: 'SIC [?]', script: sic, accessKey: '/' },

// stuff I do at the end (Page ns only) { name: 'uc', script: uc, forNamespaces: 104 },

// stuff I can do any time { name: 'author (surname first)', script: surnameFirst, accessKey: ',' } ]);	/*********	** Define scripts	*********/	/**	 * Convert text to title case.	 */	function _titlecase(text) {		// split text into individual words and examine them one by one		var textArray = text.toLowerCase.split(" ");		for (var i in textArray) {			switch(textArray[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					textArray[i] = textArray[i].substring(0,1).toUpperCase + textArray[i].substring(1,textArray[i].length);					break; }		}

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

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

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

return titleCase; }

function surnameFirst { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var name = editbox.value.substring(selStart, selEnd);

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

// split text into individual words var nameArray = name.split(" ");

// put last first, followed by a comma, then all the rest preceded by spaces name = nameArray[nameArray.length-1]+","; for (var i=0; i<nameArray.length-1; i++) name = name+" "+nameArray[i];

var linked = +name+; editbox.value = editbox.value.substring(0, selStart) + linked + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+linked.length, selStart+linked.length); }

function makeCenter { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd;

editbox.value = editbox.value.substring(0, selStart) +  + editbox.value.substring(selStart, selEnd) +  + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart, selStart+11); }

function makeRight { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd;

editbox.value = editbox.value.substring(0, selStart) +  + editbox.value.substring(selStart, selEnd) +  + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart, selStart+10); }

function sic { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd;

var selText = editbox.value.substring(selStart, selEnd); editbox.value = editbox.value.substring(0, selStart) + '' + selText + '[' + selText + '] [sic]' + editbox.value.substring(selEnd); editbox.setSelectionRange(selEnd+8, selEnd+8); }

//wrap first word in hwe template function hwe { var editbox = document.getElementsByName('wpTextbox1')[0]; var to = editbox.value.search(/\W/); if (to == -1) to = editbox.value.length;

var pre = editbox.value.substring(0, to);

var post = ''; editbox.value = post + editbox.value.substring(to); }

//wrap last word in hws template function hws { var editbox = document.getElementsByName('wpTextbox1')[0]; var from = editbox.value.lastIndexOf(" "); if (from == -1) from = 0; else from = from + 1;

var pre = editbox.value.substring(from);

if (pre.slice(-1) == "-") pre = pre.slice(0, -1);

var post = ''; editbox.value = editbox.value.substring(0, from) + post; }

function uc { var editbox = document.getElementsByName('wpTextbox1')[0]; var matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value); while (matches) { editbox.value = editbox.value.replace(matches[0], matches[1].toUpperCase); matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value); }	} }); //