User:Bodhisattwa/cleanup.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.

// <source lang="javascript">
/* global $, mw, importScript, pathoschild */

//set some global parameters for selection
var isPageNS = mw.config.get('wgNamespaceNumber') == 104;
var isIndianBio = isPageNS && mw.config.get('wgTitle').match('The Indian Biographical Dictionary.djvu/');
var isIrishBio = mw.config.get('wgTitle').match('A Compendium of Irish Biography');
var isBiography = mw.config.get('wgTitle').match(/[\w ]+Biography/);
var isDNB00 = mw.config.get('wgTitle').match('DNB');
var isDictionary = isPageNS && mw.config.get('Dictionary of National');


/**</source>
 * 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]]
 */
// <source lang="javascript">
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	// page NS
	pathoschild.TemplateScript.add(
		[
			// RunningHeader
			{ name: 'RunningHeader', position: 'cursor',
				script: function(editor) {
					editor.forField('#wpHeaderTextbox')
						.append('{{RunningHeader|||}}');
				} },

			// remove refs from footer
			{
				name: 'killrefs',
				position: 'replace',
				script: function(editor) {
					editor.forField('#wpFooterTextbox')
						.replace(/[ \s\n]{0,2}(<references ?\/>|{{smallrefs}})\n?/g, '');
				}
			},

			// add refs to footer
			{
				name: 'addrefs',
				position: 'cursor',
				script: function(editor) {
					editor.forField('#wpFooterTextbox')
						.append('{{smallrefs}}');
				}
			},

			// replaces references with smallrefs
			{
				name: 'smallrefs', position: 'replace',
				script: function(editor) {
					editor.forField('#wpFooterTextbox')
						.replace(/(<references\/>)/,'{{smallrefs}}');
				}
			},

			// convert {{runningheader}} to {{center}} in header and footer, and also <center> to {{center}}
			{
				name: 'rh to c',
				position: 'replace',
				script: function (editor) {
					editor.replace(/<center>(.+?)<\/center>/g, '{{center|$1}}');
					editor.forField('#wpHeaderTextbox').replace(/\n?{{(rh|runningheader)\|/g, '{{center');
					editor.forField('#wpFooterTextbox').replace(/\n?{{(rh|runningheader)\|/g, '{{center');
				}
			},

			// puts {{right}} into footerbox
			{
				name: 'endword', position: 'replace',
				script: function(editor) {
					editor.forField('#wpHeaderTextbox')
						.replace(/(<\/div>)/,'{{right|}}$1');
				}
			},
			// wraps anchor+ around selected text
			/*{
				name: 'anchor+ select',
				script: function(editor) {
					editor.replaceSelection(function(selected) {
						return '{{anchor+|' + selected + '}}';
					});
				}
			}*/
			{
				name: 'cleanup',
				position: 'cursor',
				script: function(editor) {
					var header = editor.forField('#wpHeaderTextbox');
					var footer = editor.forField('#wpFooterTextbox');
					var isPageNS = mw.config.get('wgNamespaceNumber') == 104;
					var isDictionary = isPageNS && mw.config.get('Dictionary of National');

					// various cleanup
					editor
						// Digitized by Google (kill)
						.replace(/Digitized[\s\n]+by[^\n]+\n(Google)?/, '')

						// remove trailing spaces at the end of each line
						.replace(/ +\n/g, '\n')

						// remove trailing whitespace preceding a hard line break
						.replace(/ +<br *\/?>/g, '<br />')

						// remove one break preceding two hard line break
 						.replace(/ <br *\/?>+<br *\/?>/, '<br />')

						// remove trailing whitespace at the end of page text
						.replace(/\s+$/g, '')

						// remove trailing spaces at the end of refs
						.replace(/ +<\/ref>/g, '</ref>')

						// remove trailing spaces at the end of template calls
						.replace(/ +}}/g, '}}')

						// convert double-hyphen to mdash (avoiding breaking HTML comment syntax)
						.replace(/([^\!])--([^>])/g, '$1—$2')

						// remove spacing around mdash, but only if it has spaces on both sides
						// (we don't want to remove the trailing space from "...as follows:— ",
						// bearing in mind that the space will already be gone if at end of line).
						.replace(/ +— +/g, '—')

						// join words that are hyphenated across a line break
						// (but leave "|-" table syntax alone)
						.replace(/([^\|])-\n/g, '$1')

						// remove unwanted spaces around punctuation marks
						.replace(/ ([);:\।?!,])/g, '$1')

						// remove unwanted spaces after bracket start
						.replace(/([(])+ /g, '$1')
						
						// remove unwanted spaces around ॥
						.replace(/ ([॥])/g, '$1')

						// – to —
						.replace(/–/g, '—')

						// ।। to ॥
						.replace(/।।/g, '॥')
						
						// ৷৷ to ॥
						.replace(/৷৷/g, '॥')					

						// remove unwanted spaces around Bengali diactics marks
						.replace(/ ([া ি ী ু ূ ৃ ে ৈ ো ৌ])/g, '$1')

						// replace র্তা with তাঁ at the beginning of words like তাঁর তাঁহার etc 
						.replace(/ র্তা/g, ' তাঁ')

						// replace য়৷ with য়া
						.replace(/য়৷/g, 'য়া')

						// replace য় with য়
						.replace(/য়/g, 'য়')
                                                 
                                                // replace ৰ with র
                                                .replace(/ৰ/g, 'র')

                                                // replace ' with ‘
                                                .replace(/ '/g, ' ‘')

                                                // replace ' with ’
                                                .replace(/' /g, '’ ')

                                                // replace " with “
                                                .replace(/ "/g, ' “')

                                                // replace " with ”
                                                .replace(/" /g, '” ')

						// q.v. to q. v.
						.replace(/q\.v\./g, 'q. v.')

						// bom to born
						.replace(/(-| |\n)bom /g, '$1born ')

						//OCR fixes
						// convert i9 to 19, etc.
						.replace(/[il]([0-9])/g, '1$1')

						// li -> h ... "the", "them", "their", and whe etcetera
						.replace(/([tT])lie/ig, '$1he')
						.replace(/wlie/g, 'whe')
						.replace(/(wlicli|ivhic(li|h))/g, 'which')
						.replace(/liurcli/g, 'hurch')

						// Av -> w
						.replace(/Av(ill|ith)/g, 'w$1')

						// "U" -> "ll" when preceded by a lowercase letter.
						.replace(/([a-z])U/g, '$1ll');

					// {{c}} to {{center}}
					editor.replace(/{{c\|/g, '{{center|');
					header.replace(/{{c\|/g, '{{center|');
					footer.replace(/{{c\|/g, '{{center|');

					// more cleanup
					editor
						// {{di}} expanded
						.replace(/{{di\|/g, '{{dropinitial|')

						// {{sm}} expanded
						.replace(/{{sm\|/g, '{{smaller|')

						// expand diacritical templates
						.replace(/{{(ae|oe|\w[:`'~^-])}}/g, '{{subst'+':$1}}')

						//convert {{—}} to —
						.replace(/{{—}}/g, '—');

					// M<sup>c</sup> to {{Mc}}
					editor.replace(/M<sup>c<\/sup>/g, '{{Mc}}');
					header.replace(/M<sup>c<\/sup>/g, '{{Mc}}');

					// yet more cleanup
					editor
					//DNB link conversion
					.replace(/\[\[(.{0,40}?) \(DNB00\)\|([^\]]+?)\]\]/g, '{{DNB lkpl|$1|$2}}')
					.replace(/\[\[(.{0,40}?) \(DNB01\)\|([^\]]+?)\]\]/g, '{{DNB lkpl|$1|$2|year=01}}')
					.replace(/\[\[(.{0,40}?) \(DNB12\)\|([^\]]+?)\]\]/g, '{{DNB lkpl|$1|$2|year=12}}')

					//section tag fix
					.replace(/<section (begin|end)=(\w[^\/]+)\/>/g, '<section $1="$2"/>');

					// merged ... function smaller() 
					editor

					if (isDictionary) {
					editor
					.replace(/\<small\>(.+?)\<\/small\>/g, '{{smaller block|$1}}')
					.replace(/\<p style="font-size:smaller"\>(.+?)\<\/p\>/g, '{{smaller block|$1}}')
					.replace(/\{\{smaller\|\[/g, '{{smaller block|[')
					} else {
					editor.replace(/\<small\>(.+?)\<\/small\>/g, '{{smaller|$1}}')
					}
					// 
					//if (isDictionary) {
					//editor.replace(/}} ?({{DNB .{2,5}})[ \n]?(\<sect[^\>]+?\>)/g, '}}\n$1$2\n')
					//}

					// stuff to do only if the page doesn't contain a <poem> tag:
					if (editor.get().indexOf("<poem>") === -1) {
						editor
						// remove single line breaks; preserve multiple.
						// not if there's a tag, template, table syntax either side of line break
						.replace(/([^>}\n])\n([^<{\|\n])/g, '$1 $2')
	
						// collapse sequences of spaces into a single space
						.replace(/  +/g, ' ');
					}
				}
			}
		],
		{ category:'page', forNamespaces:'page' } // common fields
	);

	// main NS
	pathoschild.TemplateScript.add(
		[
			// adds subst: #tag for <pages>
			{ name: 'Pages', template: '<pages index=" " from= to=/>', position: 'cursor'},

			//  adds subst: #tag for <pages> with sections
			{ name: 'Pages++', template: '<pages index=" " from= to= fromsection= tosection= />', position: 'cursor'},

			// adds defaultsort for a subpage
			{ name: 'defsortsubpage', template: '{{DEFAULTSORT:{{subst:SUBPAGENAME}}}}', position: 'cursor'},

			// wikis Chapter -1/+1 to Prev/Next
			{
				name: 'PrevNext',
				position: 'replace',
				script: function (editor) {
					var oldtitle = mw.config.get('wgTitle');
					var prevtitle = parseInt(oldtitle.replace(/^.*\/Chapter (\d+)$/,'$1'))-1;
					var headerprevious = '[[../Chapter '+prevtitle+'/]]';
					var nexttitle = parseInt(oldtitle.replace(/^.*\/Chapter (\d+)$/,'$1'))+1;
					var headernext = '[[../Chapter '+nexttitle+'/]]';
					editor.replace(/\| previous   \= \n \| next       \= /, '| previous   \= ' + headerprevious +'\n \| next       \= ' + headernext );
				}
			},

			// prev and next populated for subpages
			{
				name: 'pn [[//]]',
				position: 'replace',
				script: function (editor) {
					var prevnextlink = '[[..//]]';
					editor.replace(/\| previous   \= \n \| next       \= /, '| previous   \= ' + prevnextlink +'\n \| next       \= ' + prevnextlink );
				}
			},

			// converts header template to DNB00
			{
				name: 'DNBset',
				position: 'replace',
				script: function (editor) {
					editor.replace(/\{\{header[^\}]+\}\}\n/, '{{subst:DNBset\n |article= \n |previous= \n |next= \n |volume = \n |contributor = \n |wikipedia = \n |extra_notes= \n |from= \n |to= \n |section= \n}}');
				},
				enabled: isDNB00
			},

			// converts DNB endash title to a redirect
			{
				name: 'DNBredirect',
				position: 'replace',
				editSummary: 'redirect',
				script: function (editor) {
					editor.replace(/{{header\n \| title\s+?\= ([^–]+?)–([^\n]+?)\n[=\|\w\s\n]*}}/g, '#redirect [[$1-$2]]');
				},
				enabled: isDNB00
			},

			// add similar template
			{
				name: 'similar',
				position: 'replace',
				editSummary: '{{similar}}',
				script: function (editor) {
					editor.replace(/\{\{header/g, '{{similar|}}\n{{header');
				}
			},

			// translator to override_translator
			{
				name: 'override transl',
				position: 'replace',
				script: function (editor) {
					editor.replace(/ translator\w+\=/, ' override_translator = ');
				}
			},

			// rm text quality templates
			{
				name: 'text quality',
				position: 'replace',
				script: function (editor) {
					editor.replace(/\{\{TextQuality\|\d{2,3}%\}\}/, '');
				}
			},

			// Littell link replacement
			{ name: 'Littell', position: 'replace', editSummary: 'convert to {{Littell\'s link}}',
				script: function (editor) {
					editor.replace(/\[\[Littell\'s Living Age\/Volume (\d{3})\/Issue (\d{4})\/([^\|]+)\|[^\]]+\]\]/g, "{{Littell's link|$3|$1|$2}}");
				}
			},

			// und2ital() converts underlines to italics (PG adaption)
			{
				name: 'und2ital',
				position: 'replace',
				script: function (editor) {
					editor.replace(/\_/g, '\'\'');
				}
			}
		],
		{ category:'main', forNamespaces:0 } // common fields
	);

	// user talk NS
	pathoschild.TemplateScript.add(
		[
			{ name: 'welcome', template: '{{welcome}} ~~~~', editSummary: 'Welcome!' },
			{ name: 'Welcome+test', template: '{{subst:welcomeip}}\n{{test}} ~~~~', editSummary: 'Welcome and test' },
			{ name: 'welcomeip', template: '{{subst:welcomeip}} ~~~~', editSummary:'Welcome!' },
			{ name: 'headertoggle', template: '{{subst:User:Billinghurst/HeaderToggle}}' }
		],
		{ category:'usertalk', position:'after', forNamespaces:'user talk' } // common fields
	);

	// author NS
	pathoschild.TemplateScript.add(
		[
			// authority control
			{ name: '^auc', template:'\n{{authority control}}', position: 'after'},

			// nop
			{ name: 'nop', template:'\n{{nop}}', position: 'after'},

			// &hellip; conversion
			{ name: 'hellip', position: 'replace',
				script: function (editor) {
					editor.replace(/ (\. ?){3,5}/g, ' &hellip; ');
				}
			},

 			// subst: PAGENAME
			{ name: 'suPG', template:'{{subst:'+'PAGENAME}}', position: 'after'},

			// convert CHAPTER to Chapter
			{ name: 'chapcase', position: 'replace',
				script: function (editor) {
					editor.replace(/CHAPTER/g, '{{sc|Chapter}}');
				}
			}
		]
	);
});
// end of TemplateScript

//CHARINSERT forced outside of edit form like old Edit Tools
//at some point in wmf development, this will no longer be
//possible regardless of Gadget used - just delete all this then
window.charinsertDontMove = true;

//CHARINSERT - quick toggle between last two selected character sets
window.editToolsRecall = true;

//Toolbar buttons
/* </pre>[[Category:Bots/Script files to update]] */

var addExtraButtons = function() {
	mw.toolbar.addButtons(
		{
			"imageId": "mw-customeditbutton-tagtemplateblockcenterpoem",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/4/4f/Button_verse.png",
			"speedTip": "spm",
			"tagOpen": '{{block center|<poem>',
			"tagClose": '</poem>}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-linkwikipedia",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/7/7f/Button_link_to_Wikipedia.png",
			"speedTip": "Wikipedia link",
			"tagOpen": '[[w:',
			"tagClose": '|]]',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatelarger",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/5/56/Button_big.png",
			"speedTip": "Larger",
			"tagOpen": '{{larger|',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatesmaller",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/5/58/Button_small.png",
			"speedTip": "Sm ",
			"tagOpen": '{{smaller|',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-textforcesentencecase",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/1/12/Button_case.png",
			"speedTip": "force sentence case",
			"tagOpen": '{{'+'subst:ucfirst:{{'+'subst:lc:',
			"tagClose": '}}}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatecenter",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/5/5f/Button_center.png",
			"speedTip": "Centre text",
			"tagOpen": '{{center|',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-linkauthor",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/8/83/Button_biocitas.png",
			"speedTip": "Author link",
			"tagOpen": '[[Author:',
			"tagClose": '|]]',
			"sampleText": ""
		},
		/*{
			"imageId": "mw-customeditbutton-insertreference",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/7/79/Button_reflink.png",
			"speedTip": "Reference",
			"tagOpen": '<ref>',
			"tagClose": '</ref>',
			"sampleText": ""
		},*/
		{
			"imageId": "mw-customeditbutton-taginsertreference",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/7/79/Button_reflink.png",
			"speedTip": "ref",
			"tagOpen": '{{'+'subst:#tag:ref|',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatesmallcaps",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/4/4e/Button_romain.png",
			"speedTip": "sc ",
			"tagOpen": '{{sc|',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatehws",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/0/0c/Hyphenated_word_start.svg",
			"speedTip": "hws ",
			"tagOpen": '{{hyphenated word start|',
			"tagClose": '|}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templatehwe",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/0/07/Hyphenated_word_end.svg",
			"speedTip": "hwe ",
			"tagOpen": '{{hyphenated word end|',
			"tagClose": '|}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-linkbreak",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/4/48/Break-button-bg.png",
			"speedTip": "br ",
			"tagOpen": '<br />',
			"tagClose": '',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-substitution",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/2/20/Button_subst.png",
			"speedTip": "s: ",
			"tagOpen": '{{subst:',
			"tagClose": '}}',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-sectionbegin",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/0/06/Tag_green.png",
			"speedTip": "section begin",
			"tagOpen": '<section begin="',
			"tagClose": '" />',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-sectionend",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/d/dd/Tag_red.png",
			"speedTip": "section end",
			"tagOpen": '<section end="',
			"tagClose": '" />',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-pagesdjvu",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/7/74/Documents_icon.svg",
			"speedTip": "pg ",
			"tagOpen": '<pages index=".djvu" from= to= fromsection="" tosection=""',
			"tagClose": '/>',
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-linkitalics",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/4/4d/Farm-Fresh_book_link.png",
			"speedTip": "bk ",
			"tagOpen": "''[[",
			"tagClose": "]]''",
			"sampleText": ""
		},
		{
			"imageId": "mw-customeditbutton-templateanchorlink",
			"imageFile": "",
			"speedTip": "A/L ",
			"tagOpen": '{{anchor link|anchor=',
			"tagClose": '|pageno=}}',
			"sampleText": ""
		}
		/*
		},
		{
			"imageId": "mw-customeditbutton-templateforcesidenote",
			"imageFile": "//upload.wikimedia.org/wikipedia/commons/c/cf/Left.png",
			"speedTip": "Force s/n",
			"tagOpen": '{{Force sidenote|left|',
			"tagClose": '}}',
			"sampleText": ""
		}
		*/
	);
	/*
	if (isIrishBio) {
		mw.toolbar.addButtons({
			"imageId": "mw-customeditbutton-templateirishbioref",
			"imageFile": "",
			"speedTip": "IBr ",
			"tagOpen": '{{IrishBio ref|page=|ref=',
			"tagClose": '}}',
			"sampleText": ""
		});
	}
	*/
};  //end of var addExtraButtons</pre>

if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
	mw.loader.using('user.options', function() {
		if (mw.user.options.get( 'showtoolbar' ) == 1) {
			$.when(
				mw.loader.using('mediawiki.toolbar'),
				$.ready
			).then(addExtraButtons);
		}
	});
}
//</source>