Jump to content

User:Trappist the monk/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 17:33, 22 April 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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');
			}
		});
	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 (/[„“”]/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\/(\d+)\s*([^\}]*\|\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*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*[Cc]it[ae][^\}]+)/g, url_status_strip ("$1"));
			}
		});
	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:'native_name',
		script: function(editor) 
			{
			editor
				.replace(/name_native/g, 'native_name')
				.replace(/\|\s*native_name_lang[^\|\}]*([\|\}])/g, '$1');
			}
		});
*/	});
}

function url_status_strip (template)
	{
	const url_status = /\| *url\-status *= *live/;
	const archive_url = /\| *archive\-?url *=([^\|\}]+)/;	// requires some sort of value
	const archive_urlx = /\| *archive\-?url *=([^\|\}]*)/;	// with or without value; used for removal
	const archive_date = /\| *archive\-?date *=[^\|\}]*/;	// not checked; removed when |archive-url= is removed
	const white_space = /^\s+$/;							// used to determine if value assigned to |archive-url= is a value

	if (null == template.match (url_status))
		return template;
	else
		{
		let archive = template.match (archive_url);			// is |archive-url= present with some sort of value?
		if (null == archive)								// when null, no match; remove |archive-url=, |archive-date=, |url-status=
			return template.replace (archive_urlx, "").replace (archive_date, "").replace (url_status, "");
		else												// here when |archive-url= present with some sort of value
			if (archive[1].match (white_space))				// is that value only whitespace?
				return template.replace (archive_urlx, "").replace (archive_date, "").replace(url_status, "");
			else
				return template;							// return the citation as-is because |archive-url= has something
		}
	} 

//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
window.charinsertCustom = {
 "Wiki markup": ' custom: {\{native.name||+}} <code><nowiki>+</nowiki></code>',
};