User:Trappist the monk/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.
/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 */
// <pre>

mw.config.set('userjs-templatescript', { regexEditor: false });					// disable the regex editor
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
	$.ajax(
		'//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js',
		{dataType: 'script', cache: true}
	).done(function () {
		var add = function() {
		return mw.util.addPortletLink.apply(mw.util, arguments);
		};

	pathoschild.TemplateScript.add(
		{
		name:'hyphenate',
		script: function(editor) 
			{
			editor
				.replace(/(\| *)accessdate/g, '$1access-date')
				.replace(/(\| *)archivedate/gi, '$1archive-date')
				.replace(/(\| *)archiveurl/gi, '$1archive-url')
				.replace(/(\| *)airdate/gi, '$1air-date')
				.replace(/(\| *)authorlink/gi, '$1author-link')					// also fixes same with trailing enumerator
				.replace(/(\| *)author(\d+)link/gi, '$1author$2-link')
				.replace(/(\| *)orig\-?year/gi, '$1orig-date')

				.replace(/\| *access\-date\s*=\s*([\|\}])/g, '$1')				// delete empties
				.replace(/\| *archive\-date\s*=\s*([\|\}])/g, '$1')
				.replace(/\| *archive\-url\s*=\s*([\|\}])/g, '$1')
				.replace(/\| *url\-status\s*=\s*([\|\}])/g, '$1')
				.replace(/\| *author\d*\-link\d*\s*=\s*([\|\}])/g, '$1')		// plain & both enumerated forms
				.replace(/\| *orig\-date\s*=\s*([\|\}])/g, '$1');
			}
		});
	pathoschild.TemplateScript.add(
		{
		name:'cleanup',
		script: function(editor) 
			{
			editor
				.replace(/< *[Bb][Rr] *>/g, '<br />')
				.replace(/<R[Ee][Ff]\b/g, '<ref')
				.replace(/<\/R[Ee][Ff]\b/g, '</ref')
				.replace (/Hawai[‘’']i/g, "Hawaiʻi")							// replace various appostrophes with okina (U+02BB)
				.replace (/[„“”]/g, '"')
				.replace (/[‘’]/g, "'")
				.replace (/([\.;,!\?\>\]\}]) +(<ref)/g, "$1$2")
				.replace (/(\|\s*title[^\|\}]*) : /g, "$1: ")					// extraneous space before colon introducing subtitle
				.replace (/(\|\s*trans\-title[^\|\}]*) : /g, "$1: ")			// extraneous space before colon introducing subtitle
				.replace (/\|\s*url\s*=\s*https?:\/\/[^\|\}]*worldcat\.org[^\|\}]*\/(?:oclc|title)\/(\d+)[^\|\}]*([^\}]*\|\s*oclc\s*=\s*\1)/g, "$2")		// delete worldcat url when |oclc= matches
				.replace (/\|\s*url\s*=\s*https?:\/\/[^\|\}]*doi\.org\/([^\|\}]+)\s*([^\}]*\|\s*doi\s*=\s*\1)/g, "$2")				// delete doi url when |doi= matches
				.replace (/\|\s*url\s*=\s*https?:\/\/[^\|\}]*jstor\.org\/stable\/([^\|\}]+)\s*([^\}]*\|\s*jstor\s*=\s*\1)/g, "$2")	// delete jstor url when |jstor= matches
				.replace (/\|\s*url *=[^\|\}]+PMC(\d+)\/?([^\}]*\| *[Pp][Mm][Cc] *= *\1)/g, "$2")									// delete pmc url when |pmc= matches
				.replace (/\|\s*url\s*=\s*https?:\/\/pubmed\.ncbi\.nlm\.nih\.gov\/(\d+)\/?\s*([^\}]*\|\s*pmid\s*=\s*\1)/g, "$2")	// delete pmid url when |pmid= matches
				
				.replace (/(\|\s*oclc\s*=\s*(\d+)[^\}]*)\|\s*url\s*=\s*https?:\/\/[^\|\}]*worldcat\.org[^\|\}]*\/(?:oclc|title)\/\2[^\|\}]*/g, "$1")	// oclc other way round
				.replace (/(\|\s*doi\s*=\s*([^\|\s]+)[^\}]*)\|\s*url\s*=\s*https?:\/\/[^\|\}]*doi\.org\/\2/g, "$1")					// doi other way round
				.replace (/(\|\s*jstor\s*=\s*([^\|\s]+)[^\}]*)\|\s*url\s*=\s*https?:\/\/[^\|\}]*jstor\.org\/stable\/\2/g, "$1")		// jstor other way round
				.replace (/(\|\s*pmid\s*=\s*(\d+)[^\}]*)\|\s*url\s*=\s*https?:\/\/pubmed\.ncbi\.nlm\.nih\.gov\/(\d+)\/?\s*/g, "$1")	// pmid other way round

				.replace (/\|\s*url\-status\s*=\s*(?:live|dead)?\s*\|\s*archive\-url\s*=\s*\|\s*archive\-date\s*=\s*([\|\}])/g, "$1")	// a common parameter order mostly with 'live'; sometimes 'dead', sonetimes empty
				.replace (/\|\s*url-status\s*=\s*dead[^\|\}]*/g, "")																// delete |archive-url=
				.replace (/(\|\s*(?:isbn|ISBN)\s*=\s*)(97[89]\-?\d{9}\-?\d)/g, "$1{{Format ISBN|$2}}")								// format ISBN13
				.replace (/(\|\s*(?:isbn|ISBN)\s*=\s*)(\d{9}\-?[\dX])/g, "$1{{Format ISBN|$2}}")									// format ISBN10
				.replace (/(\{\{\s*(?:ISBN|[Ii]sbn)\s*\|\s*)(97[89]\-?\d{9}\-?\d)/g, "$1{{Format ISBN|$2}}")						// format {{ISBN|<isbn13>}}
				.replace (/(\{\{\s*(?:ISBN|[Ii]sbn)\s*\|\s*)(\d{9}\-?[\dX])/g, "$1{{Format ISBN|$2}}")								// format {{ISBN|<isbn10>}}
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-01(\s*\|)/g, "$1January $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-02(\s*\|)/g, "$1February $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-03(\s*\|)/g, "$1March $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-04(\s*\|)/g, "$1April $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-05(\s*\|)/g, "$1May $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-06(\s*\|)/g, "$1June $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-07(\s*\|)/g, "$1July $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-08(\s*\|)/g, "$1August $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-09(\s*\|)/g, "$1September $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-10(\s*\|)/g, "$1October $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-11(\s*\|)/g, "$1November $2$3")
				.replace (/(\|\s*date\s*=\s*)(\d{4})\-12(\s*\|)/g, "$1December $2$3")
				.replace (/(\|\s*page)s(\s*=\s*(?:\d+|[clxvi]+))(\s*[\|\}])/g, "$1$2$3")	// replace |pages= with |page=
				.replace (/(\|\s*|\s*location\s*=\s*)М\./g, "$1Moscow");		// replace Cyrillic М<dot> with Moscow
			}
		});
	pathoschild.TemplateScript.add(
		{
		name:'normalize cs1|2',
		script: function(editor) 
			{
			editor
				.replaceSelection(function(selected) {
					var selected_len = selected.length;
					while (true)
						{
						selected = selected.replace (/ *= */g, '=');				// remove whitespace around an assignement operator
						selected = selected.replace (/ *\| */g, '|');				// remove spaces around pipes
						selected = selected.replace (/\{ +/g, '{');					// remove spaces ahead of template name
						selected = selected.replace (/ +\}/g, '}');					// remove spaces at end of template
	
						selected = selected.replace (/\|[\-a-z0-9]+=\|/g, '|');		// remove empty parameters =|
						selected = selected.replace (/\|[\-a-z0-9]+=\}/g, '}');		// remove empty parameters =}
						
						selected = selected.replace(/\|/g, ' |');					// normalize spacing around pipes
						if (selected_len == selected.length)
							break;
						selected_len = selected.length;
						}
					return selected;
				});
			}
		});

	pathoschild.TemplateScript.add(
		{
		name:'dmy',
		script: function(editor) 
			{
			editor.replace (/\|\s*df\s*=\s*dmy[^\|\}]*/g, '');
			}
		});

	pathoschild.TemplateScript.add(
		{
		name:'mdy',
		script: function(editor) 
			{
			editor.replace (/\|\s*df\s*=\s*mdy[^\|\}]*/g, '');
			}
		});
/*	pathoschild.TemplateScript.add(
		{
		name:'native_name',
		script: function(editor) 
			{
			editor
				.replace(/name_native/g, 'native_name')
				.replace(/\|\s*native_name_lang[^\|\}]*([\|\}])/g, '$1');
			}
		});
*/	});
}

//importScript('User:Trappist the monk/HarvErrors.js');
mw.loader.load( '/w/index.php?title=User:Trappist_the_monk/HarvErrors.js&action=raw&ctype=text/javascript' ); // Backlink: [[User:Trappist the monk/HarvErrors.js]]

//importScript('User:Trappist the monk/Sandbox/MediaWiki talk:RefToolbarLegacy.js');

//importScript('User:Ucucha/HarvErrors.js');
//importScript('User:Trappist the monk/script/MOSNUM dates.js');				// [[Trappist the monk/script/MOSNUM dates.js]]
//importScript('User:Ohconfucius/script/MOSNUM dates.js');						// reference

// Add custom CharInsert entries [[Help:CharInsert]]
window.charinsertCustom = {
 "Wiki markup": ' custom: <syntaxhighlight.lang="wikitext".inline="1">+</syntaxhighlight> <code><nowiki>+</nowiki></code>',
};

//Tell the Remember the dot syntax highlighter gadget to ignore unclosed br and hr tags
syntaxHighlighterSiteConfig = {
    voidTags: ["br", "hr", "BR", "HR"],
};