Jump to content

User:Closeapple/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Probably should merge/update [[User:Closeapple/RMFset.js]] into this someday; it has other complex TemplateScript regexes.
/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see http://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() {
	var nowMajorEdit = false;	// Items should set this flag true whenever they make a major edit.
	pathoschild.TemplateScript.add([
		// add your own templates or scripts here
		{ name: 'welcome', template: '{{subst:welcome}} ~~~~', position: 'after', editSummary: 'welcome!', forNamespaces: 'user talk' },
		{
			category: 'universal fixups',
			name: 'obvious fixes',
			tooltip: 'uncontroversial safe edits, like removing trailing spaces',
			// isMinorEdit: true,	// icky: makes TS force it even if no edit was made
			accessKey: ' ',
			script: function(editor) {
				old = editor.get();
				editor
					.replace(/\n?[ \t]*(\[\[)\s*(Category:[^\{\}\]]+\]\])/gim, "\n$1$2");	// all categories should have 1 newline and no horizontal spaces before; does not eat multiple newlines (because category blocks are supposed to be separated by a blank line)
				if (editor.get() != old) {
					editor.appendEditSummary('spacing fixups');
				}
				editor
					.replace(/[ \t]+$/gm, '');	// trim trailing spaces
				if (editor.get() != old) {
					if (!nowMajorEdit) {
						editor.options({ minor: true });
					}
				}
			}
		},
		{
			category: 'universal fixups',
			name: 'melt wikilinks',
			tooltip: '[[link|links]]→[[link]]s; remove insignificant link spaces',
			// isMinorEdit: true,	// icky: makes TS force it even if no edit was made
			script: function(editor){
				old = editor.get();
				editor
					.replace(/\[\[\s*([^\s\{\}\|\]][^\{\|\]]+[^\s\{\}\|\]])\s*\|\s*\1(\w*)\]\]/g, "[[$1]]$2");	// [[link|links]] -> [[link]]s; doesn't match capitalization difference on first character, which would also be a candidate for conversion
				if (editor.get() != old) {
					editor.appendEditSummary('wikilink fixups');
					if (!nowMajorEdit) {
						editor.options({ minor: true });
					}
				}
			}
		},
		{
			category: 'universal fixups',
			name: 'ref punct',
			tooltip: 'Move punctuation after refs.  (Don\'t use if refs are followed by file extensions starting with .)',
			// isMinorEdit: true,	// icky: makes TS force it even if no edit was made
			script: function(editor){
				old = editor.get();
				editor
					.replace(/([.?,:;]+)(["\)]*)\s*((?:<ref\b[^<]+<\/ref> *'*)+) *\1/gi, '$1$2$3')	// deduplicate same sentence punctuation before and after refs
					.replace(/[,;]\s*((?:<ref\b[^<]+<\/ref> *'*)+) *\./gi, '.$1')	// , or ; before but . after refs, nearly always ends up being . before refs
					.replace(/([\w"\)\]])\s*((?:<ref\b[^<]+<\/ref> *'*)+) *([.?,:;]+)/gi, '$1$3$2');	// sentence punctuation after refs goes before refs
				if (editor.get() != old) {
					editor.appendEditSummary('ref punctuation');
					if (!nowMajorEdit) {
						editor.options({ minor: true });
					}
				}
			}
		},
		{
			category: 'potential fixups',
			name: 'ref spacing',
			tooltip: 'Eliminates weird spacing around refs, both visible and invisible. (Avoids after = or 10k after {{reflist}}.)',
			// isMinorEdit: true,	// icky: makes TS force it even if no edit was made
			script: function(editor){
				old = editor.get();
				//  The \{\{\s*reflist\b.{2,10000} bit in regex is to make it not match within 10,000 characters after the {{reflist}} tag, so that it doesn't try to reorganize the refs inside that tag.  Maybe it should look for the refs= parametes in the reflist also.
				editor
					.replace(/(?<!=|\||\{\{\s*reflist\b.{2,10000})\s*<ref\b([^/>]*)\s*>\s*/gi, '<ref$1>')	// start full ref, not after = or {{reflist}}
					.replace(/(?<!\{\{\s*reflist\b.{2,10000})\s*<\s*\/\s*ref\s*>/gi, '</ref>')	// end full ref, not after = or {{reflist}}
					.replace(/(?<!=|\|)\s*<ref\b(\s?)\s*([^>]*?\s?)\s*\/\s*>/gi, '<ref$1$2/>');	// backreference; primitive attempt at retaining optional 1 space before /; not after =
				if (editor.get() != old) {
					editor.appendEditSummary('ref spacing');
					if (!nowMajorEdit) {
						editor.options({ minor: true });
					}
				}
			}
		}
		// end of custom template/script additions for TemplateScript
	]);
});
// </nowiki>