Jump to content

User:Ryuch/common.js

From Wikisource
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.

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:AzaToth/morebits.js&action=raw&ctype=text/javascript');
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Ioeth/friendlytag.js&action=raw&ctype=text/javascript');
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Ioeth/friendlytalkback.js&action=raw&ctype=text/javascript');

// wiktionary Popup
mw.loader.load('//en.wikinews.org/w/index.php?title=MediaWiki:Gadget-dictionaryLookupHover.js&action=raw&ctype=text/javascript');

// set some global parameters for selection
var isIndianBio = (-1 != location.href.indexOf("Page:The_Indian_Biographical_Dictionary.djvu/"));
var isIrishBio = (-1 != location.href.indexOf("Page:A_Compendium_of_Irish_Biography"));
var isCollins =  (-1 != location.href.indexOf("Page:An_account_of_the_English_colony"));
var isDNB00 = (-1 != location.href.indexOf("DNB00"));
var isCopyright = (-1 != location.href.indexOf("Page:Copyright_Act"));
var isMainspace = (-1 == location.href.search(/title=(Talk|User|User_talk|Wikisource|Wikisource_talk|File|File_talk|Mediawiki|Mediawiki_talk|Template|Template_talk|Help|Help_talk|Category|Category_talk|Portal|Portal_talk|Author|Author_talk|Page|Page_talk|Index):/));

/**
 * 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() {
	// register scripts
	pathoschild.TemplateScript.add([
		// all pages
		{ name: '{{rh}}', script: RunningHeader },
		{ name: 'clean up', script: cleanup },
		{ name: 'nop', script: nop },
		{ name: 'DefSort Subpage', script: defsortsubpage },
		{ name: 'Join lines', script: joinlines },
		
		// An account of the English colony
		{ name: 'Float poem', script: floatpoem, enabled: isCollins },
		{ name: 'Collins cleanup', script: Collins_cleanup, enabled: isCollins },
		
		// Copyright Act
		{ name: 'Ch74 left', script: copyheadch74, enabled: isCopyright },
		{ name: '4and5 left', script: copyhead45, enabled: isCopyright },
		
		// DNB00
		{ name: 'DNBset', script: DNBset, enabled: isDNB00 },
		
		// Indian Biographical Dictionary
		{ name: 'IndianBio', script: IndianBio, enabled: isIndianBio },
		
		// A Compendium of Irish Biography
		{ name: 'Anchor', script: anchor, enabled: isIrishBio },
		{ name: 'internal nslink', script: nslink, enabled: isIrishBio },
		{ name: 'djvu pagelink', script: djvupagelink, enabled: isIrishBio },
		
		// main namespaces
		{ name: 'override transl', script: trans, enabled: isMainspace },
		{ name: 'remove TQ', script: rmTextQual, enabled: isMainspace },
		{ name: '{{similar}}', script: similar, enabled: isMainspace },
		{ name: 'Ch.prev & next', script: PrevNext, enabled: isMainspace },
		{ name: 'Pages', script: tag_pages, enabled: isMainspace },
		
		// user namespace
		{ name: 'welcome', script: welcome, forNamespaces: 'user talk' },
		{ name: 'welcomeip & test', script: weliptest, forNamespaces: 'user talk' },
		{ name: 'welcomeip', script: welcomeip, forNamespaces: 'user talk' },
		{ name: 'toggle in Page:', script: HeaderToggle, forNamespaces: 'user talk' }
	]);
	
	//USER TALK namespace
	// welcome() applies {{welcome}}
	// weliptest() subst {{welcomeip}} applies {{test}}
	// welcomeip() subst {{welcomeip}}
	// HeaderToggle() subst: User:Billinghurst/HeaderToggle which tells how to toggle header in Page:
	
	function welcome(editor) {
		editor
			.append('{{welcome}} ~~~~')
			.appendEditSummary('welcome');
	}
	
	function weliptest(editor) {
		editor
			.append('{{subst:welcomeip}} ~~~~\n{{test}}')
			.appendEditSummary('welcome & test');
	}
	
	function welcomeip(editor) {
		editor
			.append('{{subst:welcomeip}} ~~~~')
			.appendEditSummary('welcome');
	}
	
	function HeaderToggle(editor) {
		editor.append('{{subst:User:Billinghurst/HeaderToggle}}');
	}
	
	
	//MAIN namespace
	// DNBset() - replaces {{header}} with {{DNBset}}
	// rmTextQual() removes {{TextQuality}}
	// trans() replaces translator with override_translator
	// similar() adds {{similar}} above header
	// PrevNext() from {{header}} section, wikis Chapter -1/+1 to Prev/Next
	// tag_pages() applies <pages> using #tag and  index {{BASEPAGENAME}} fromsection tosection take {{SUBPAGENAME}}
	// defsortsubpage()
	
	function DNBset(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}}');
	}
	
	function rmTextQual(editor) {
		editor.replace(/\{\{TextQuality\|\d{2,3}%\}\}/, '');
	}
	
	function trans(editor) {
		editor.replace(/ translator\w+\=/, ' override_translator = ');
	}
	
	function similar(editor) {
		editor
			.replace(/\{\{header/g, '{{similar|}}\n{{header')
			.appendEditSummary('{{similar}}');
	}
	
	function PrevNext(editor) {
		var newtitle = parseInt(mw.config.get('wgTitle').replace(/^.*\/Chapter (\d+)$/,'$1'))-1;
		var headerprevious = '[[../Chapter '+newtitle+' /]]';
		var headernext = '[[../Chapter '+newtitle+' /]]';
		editor.replace(/\| previous   \= \n \| next       \= /, '| previous   \= ' + headerprevious +'\n \| next       \= ' + headernext);
	}
	
	//subst: #tag and some fields
	function tag_pages(editor) {
		var pages = '{{subst:#tag:pages||index="{{subst:BASEPAGENAME}}.djvu"|from=|to=|fromsection="{{subst:SUBPAGENAME}}"|tosection="{{subst:SUBPAGENAME}}"}}';
		editor.replaceSelection(pages);
	}
	
	function defsortsubpage(editor) {
		editor.append('{{DEFAULTSORT:{{subst:SUBPAGENAME}}}}');
	}
	
	function joinlines(editor){
		editor
			.replace(/[\.!?"] \n([A-Z])/g, "\.\n\n$1")
			.replace(/- \n/g, '')
			.replace(/ \n/g, ' ')
			.setEditSummary('hypen del')
			.clickDiff();
	}
	
	//ALL NAMESPACE USE
	// cleanup() - all sorts of text cleaning from OCR
	// floatpoem() -  float center, adds <poem> tag, and makes smaller
	// nop() - nop, though it puts it at the bottom of editbox, so only useful in Page:
	function cleanup(editor) {
		editor
			// Digitized by Google (kill)
			.replace(/Digitized[\s\n]+by[\s\n]+Google/, '')
			
			// remove trailing whitespace at the end of each line
			.replace(/ \n/g, '\n')
			
			// remove trailing whitespace at the end of input
			.replace(/\s+$/g, '')
			
			// convert double-hyphen to mdash
			.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:— "
			.replace(/ +— +/g, '—')
			
			// join words that are hyphenated across a line break
			.replace(/-\n/g, '');
			
		// 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.
				.replace(/([^>\n])\n([^<\n])/g, '$1 $2')
				
				// collapse sequences of spaces into a single space
				.replace(/  +/g, ' ');
		}
			
		editor
			// remove unwanted spaces around punctuation marks
			.replace(/ ([;:\?!,])/g, '$1')
	
			//OCR fixes
			// convert i9 to 19, etc.
			.replace(/[il]([0-9])/g, '1$1')
			
			// "the", "them", "their", etcetera
			.replace(/tlie/g, 'the')
			
			// "U" -> "ll" when preceded by a lowercase letter.
			.replace(/([a-z])U/g, '$1ll');
	}
	
	// Takes poetic text, makes it float center, adds <poem> tag, and makes font-size:smaller
	function floatpoem(editor) {
		editor.replaceSelection(function(pre) {
			return '{{float center|<poem>{{smaller|'+pre+'}}</poem>}}';
		});
	}
	
	// nop at bottom of editbox
	function nop(editor) {
		editor.append('\n{{nop}}');
	}
	
	
	//SPECIFIC TO Page: namespace
	// IndianBio() - cleanup for Page:The Indian Biographical Dictionary.djv
	// anchor() - applies {{anchor+}}
	
	// RunningHeader() - puts {{RunningHeader}} in to headerbox
	// endword() - puts {{right}} into footerbox
	// smallrefs_rep() - puts {{smallrefs}} into footerbox
	
	/* Page:The Indian Biographical Dictionary.djvu tools */
	function IndianBio(editor) {
		editor.forField('#wpHeaderTextbox').append('{{center|INDIAN BIOGRAPHICAL DICTIONARY, 1915.}}\n\n{{sc|}}');
		cleanup(editor);
		editor
			.replace(/([\n\s])([bmsd]\.)/g, '$1\'\'$2\'\'')
			.replace (/([\n\s])(Address|Clubs?|educ|Publications|Recreations)\:/g, '$1\'\'$2\'\':')
			.replace (/CLE\.?/g, 'C.I.E.')
			
			// VU -> VII; followed by U in the middle of a word -> ll
			.replace(/VU/g, 'VII')
			.replace(/(\w)U(\w)/, '$1ll$2');
	}
	
	/* Page:A Compendium of Irish Biography tools */
	function anchor(editor) {
		editor.replaceSelection(function(pre) {
			return '{{anchor+|'+pre+'}}';
		});
	}
	
	//adds template 'DJVU page link'
	function djvupagelink(editor) {
		editor.replaceSelection(function(pre) {
			var work = isIrishBio ? 24 : '';
			return '{{DJVU page link|'+pre+'|'+work+'}}';
		});
	}
	
	//set to internal namespace link, set specifically for subpage xref
	function nslink(editor) {
		editor.replaceSelection(function(pre) {
			return '{{namespace link|'+pre+'|'+pre+'|..}}';
		});
	}
	
	//PAGE NAMESPACE  headerbox or footerbox
	function RunningHeader(editor) {
		editor.forField('#wpHeaderTextbox').append('{{RunningHeader|||}}');
	}
	
	//*Footerbox app; for archaic books where the first word on the next page is placed at bottom
	function endword(editor) {
		editor.forField('#wpFooterTextbox').replace(/(<\/div>)/,'{{right|}}$1');
	}
	
	function smallrefs_rep(editor) {
		editor.forField('#wpFooterTextbox').replace(/(<references\/>)/,'{{smallrefs}}');
	}
	
	//merge cleanup
	function Collins_cleanup(editor) {
		RunningHeader(editor);
		endword(editor);
		smallrefs_rep(editor);
	}
	
	function copyhead45(editor) {
		editor.forField('#wpHeaderTextbox').append("{{sidenotes begin}}{{RunningHeader|4 & 5 ELIZ. 2|''Copyright Act'', 1956|{{sc|Ch.}} 74}}");
		editor.forField('#wpFooterTextbox').replace(/(<\/div>)/,'{{right|}}{{sidenotes end}}$1');
	}
	
	function copyheadch74(editor) {
		editor.forField('#wpHeaderTextbox').append("{{sidenotes begin}}{{RunningHeader|{{sc|Ch.}} 74|''Copyright Act'', 1956|4 & 5 ELIZ. 2}}");
		editor.forField('#wpFooterTextbox').replace(/(<\/div>)/,'{{right|}}{{sidenotes end}}$1');
	}	
});
// </nowiki>

if (mw.toolbar) {
	$('#wpTextbox1').wikiEditor('addToToolbar', {
		section: 'main',
		group: 'format',
		tools: {
			'custom-spm': {
				label: 'spm',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{float center|<poem style="font-size:smaller">',
						post: '',
						sampleText: ''
					}
				}
			},
			'custom-Wikipedia link': {
				label: 'Wikipedia link',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/7/7f/Button_link_to_Wikipedia.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '[[w:',
						post: '|]]',
						sampleText: ''
					}
				}
			},
			'custom-Larger': {
				label: 'Larger',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/5/56/Button_big.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{larger|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Sm': {
				label: 'Sm ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{smaller|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-f/s': {
				label: 'f/s ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{'+'subst:ucfirst:{{'+'subst:lc:',
						post: '}}}}',
						sampleText: ''
					}
				}
			},
			'custom-Centre text': {
				label: 'Centre text',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/5/5f/Button_center.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{center|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Author link': {
				label: 'Author link',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/83/Button_biocitas.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '[[Author:',
						post: '|]]',
						sampleText: ''
					}
				}
			},
			'custom-Reference': {
				label: 'Reference',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/7/79/Button_reflink.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<ref>',
						post: '</ref>',
						sampleText: ''
					}
				}
			},
			'custom-sc': {
				label: 'sc ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{sc|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-hws': {
				label: 'hws ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hyphenated word start|',
						post: '|}}',
						sampleText: ''
					}
				}
			},
			'custom-hwe': {
				label: 'hwe ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hyphenated word end|',
						post: '|}}',
						sampleText: ''
					}
				}
			},
			'custom-br': {
				label: 'br ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<br />',
						post: '',
						sampleText: ''
					}
				}
			},
			'custom-section begin': {
				label: 'section begin',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Tag_green.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<section begin=',
						post: ' />',
						sampleText: ''
					}
				}
			},
			'custom-section end': {
				label: 'section end',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/d/dd/Tag_red.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<section end=',
						post: ' />',
						sampleText: ''
					}
				}
			},
			'custom-pg': {
				label: 'pg ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<pages index=".djvu" from= to= fromsection="" tosection=""',
						post: '/>',
						sampleText: ''
					}
				}
			},
			'custom-bk': {
				label: 'bk ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '\'\'[[',
						post: ']]\'\'',
						sampleText: ''
					}
				}
			},
			'custom-A/L': {
				label: 'A/L ',
				type: 'button',
				icon: '',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{anchor link|anchor=',
						post: '|pageno=}}',
						sampleText: ''
					}
				}
			},
			'custom-Force s/n': {
				label: 'Force s/n',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/c/cf/Left.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{Force sidenote|left|',
						post: '}}',
						sampleText: ''
					}
				}
			},
		}
	});

	if (isIrishBio) {
		$('#wpTextbox1').wikiEditor('addToToolbar', {
			section: 'main',
			group: 'format',
			tools: {
				'custom-IBr': {
					label: 'IBr ',
					type: 'button',
					icon: '',
					action: {
						type: 'encapsulate',
						options: {
							pre: '{{IrishBio ref|page=|ref=',
							post: '}}',
							sampleText: ''
						}
					}
				}
			}
		});
	}

	if (isCopyright) {
		$('#wpTextbox1').wikiEditor('addToToolbar', {
			section: 'main',
			group: 'format',
			tools: {
				'custom-DIV1': {
					label: 'DIV1 ',
					type: 'button',
					icon: '',
					action: {
						type: 'encapsulate',
						options: {
							pre: '<div style="inherit; text-indent:-2em; margin-left:4em">',
							post: '</div>',
							sampleText: ''
						}
					}
				},
				'custom-DIV2': {
					label: 'DIV2 ',
					type: 'button',
					icon: '',
					action: {
						type: 'encapsulate',
						options: {
							pre: '<div style="inherit; text-indent:1em; margin-left:1em">(',
							post: '</div>',
							sampleText: ''
						}
					}
				}
			}
		});
	}
}
//</nowiki>

// [[Category:Bots/Script files to update]]