User:Arcorann/templatescript.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.arcorann.custom', name: 'Custom tools', url: '//en.wikisource.org/wiki/User:Arcorann', description: 'More custom scripts', categories: [ {				name: 'Custom', scripts: [ { key: 'long-s', name: 'Insert or convert long S', script: long_s}, { key: 'asc-adbc', name: 'asc AD/BC/AH/AM', script: asc_adbc}, { key: 'pseudoheading', name: 'pseudoheading', script: pseudoheading}, { key: 'ellipsis', name: 'ellipsis', script: ellipsis}, { key: 'trim-newl', name: 'condense newlines', script: trim_newl} ]			}		]	});

/*********	** Private methods *********/

/*********	** Script methods *********/

/**	 * Convert an s (or f) into a long s template, or insert a long s template. * @param {object} editor The script helpers for the page. */	function long_s(editor) { editor.replaceSelection(function(selected) {			if (selected.length == 0) {				return ;			}			else if (selected.length == 1) {				// assume we've selected the characters we want replaced 				return ;			}			else {				switch(selected) {					// two characters long					case 'ff':						return ;					case 'ss':						return ;					case 'fs':						return 's';					default:						// replace any f or s found that's followed by a letter						return selected.replace(/([fs])(\w)/g, '$2');				}			}		}); }	function ellipsis(editor) { editor.replaceSelection(function(selected) {			if (selected.length == 0) {				return '…';			}			else {				return selected.replace(/\s?\.\s?\.\s?\.\s?/g, '…');			}		}); }	function pseudoheading(editor) { editor.replaceSelection(function(selected) {			ret = "";			return ret.replace(/(\s)+}}/g,"}}$1");		}); }	function asc_adbc(editor) { editor.replace(/([^|])(A\. ?D\.|B\. ?C\.|A\. ?H.|A\. ?M\.)/g, '$1'); }	function trim_newl(editor) { editor.replaceSelection(function(selected) {				return selected.replace(/\n\n/g, '\n');		}); }

}); //