User:Vozul/common.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.

/**
 * 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() {
	pathoschild.TemplateScript.add([
		// add your own templates or scripts here
		{ name: 'Simplify quotes', script: simplifyQuotes },
		{ name: 'Clean page', script: clean },
		{ name: 'Replace dashes', script: replaceDashes },
		{ name: 'Remove newlines', script: removeExtraNewlines },
		{ name: 'Small superscript', script: smallSuperscript },
	]);
});

function simplifyQuotes(editor) {
	editor.replace(/[“”]/g, '"')
		.replace(/[‘’]/g, '\'') ;
}

function clean(editor) {
	editor.replace(/ ([;:\?!,])/g, '$1') // remove spaces around punctuation
		.replace(/[\t ]+\n/g, '\n') // remove trailing spaces at the end of each line
		.replace(/-\n/g,'') // join hypenated words accross line break
		.replace(/\s+$/, '') // remove trailing whitespace at the end of the page
		.replace(/(?<!\n)\n(?!\n)/g, ' ') // replace single newlines with spaces, leave 2+ newlines alone
		.replace(/[\t ]*"[\t ]*/g, '"'); // remove spaces around double quotes
}

function replaceDashes(editor) {
	editor.replace(/{{--}}/g, '—')
		.replace(/&mdash;/g, '—')
		.replace(/&ndash;/g, '-')
		.replace(/--/g, '—');
}

function removeExtraNewlines(editor) {
	editor.replace(/(?<!\n)\n(?!\n)/g, ' '); // replace single newlines with spaces, leave 2+ newlines alone
}

function smallSuperscript(editor) {
	editor.replaceSelection(function(selected) {
		return '<sup>{{sm|'+selected+'}}</sup>';
	});
}
// </nowiki>