User:Arcorann/templatescript.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.

/*



This page defines a TemplateScript library. It's not meant to be referenced
directly. See [[Wikisource: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 [[File:Pathoschild/templatescript.js]]
 */
// <nowiki>
$.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}
				]
			}
		]
	});

	/*********
	** 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 '{{ls}}';
			}
			else if (selected.length == 1) {
				// assume we've selected the characters we want replaced 
				return '{{ls}}';
			}
			else {
				switch(selected) {
					// two characters long
					case 'ff':
						return '{{ls}}{{ls}}';
					case 'ss':
						return '{{ls}}{{ls}}';
					case 'fs':
						return '{{ls}}s';
					default:
						// replace any f or s found that's followed by a letter
						return selected.replace(/([fs])(\w)/g, '{{ls}}$2')
				}
			}
				
		});
	}
	
	function asc_adbc(editor) {
		editor.replace(/(?:[^|])(A. ?D.|B. ?C.|A. ?H.|A. ?M.)/g, '{{asc|$1}}');
	}


});
// </nowiki>