User:DeirdreAnne/common.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.

/* global $, mw, pathoschild */
importScript('User:Inductiveload/InlinePagenums.js');

importScript('User:Inductiveload/Metadata_form.js');

importScript('User:Inductiveload/Roman numerals.js');
importScript('User:Inductiveload/Running header.js');

importScript('User:Inductiveload/Visibility.js');

// MOST OF THIS IS COPIED STRAIGHT FROM USER:BILLINGHURST/MONOBOOK.JS
popupFixDabs=true; /* No idea what this does */
popupAdminLinks=true; /* I'm guessing this is admin specific */

// == THIS AUTOFILLS SOME OF THE PARAMETERS IN THE AUTHOR HEADER ==
importScript('User:Phe/Author fill.js');

// == THIS SEARCHES FOR WORKS ABOUT THE AUTHOR IN CERTAIN REFERENCES BUT IF THERE AREN'T ANY IT JUST ADDS AN EDIT SUMMARY - IT IS IMPLEMENTED BELOW ==
importScript('User:Phe/Works about.js');

// == GLOBAL PARAMETERS == /*most from User:Billinghurst/monobook.js - those I don't need I'm keeping as models*/
/*set some global parameters for selection */
//preset variables, REMINDER THAT THESE HAVE UNDERSCORE WORD SEPARATORS for location.href
   var isUserNS = (-1 != location.href.indexOf("User_talk:"));
   var isPageNS = (-1 != location.href.indexOf("Page:"));
   var isAuthorNS = (-1 !== location.href.indexOf("Author:")); /*why double ==?*/
   var isAgrippa = (-1 != location.href.indexOf("Page:[\w\_]+Occult_Philosophy")); /*not sure if I'll use this but it's a long project*/
   var isIndianBio = (-1 != location.href.indexOf("Page:The_Indian_Biographical_Dictionary.djvu/"));/*model only*/
   var isIrishBio = (-1 != location.href.indexOf("Page:A_Compendium_of_Irish_Biography"));/*model only*/
   var isBiography = (-1 != location.href.indexOf("Page:[\w\_]+Biography"));
   var isDNB00 = (-1 != location.href.indexOf("DNB00"));
   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):/));

// == SIDEBAR SCRIPTS /*I prefer the cleanness of a portlet like en:User:Jsimlo/shortcuts, I wonder if that's possible*/ ==
/**
 * 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
	*********/
	// === CALLS FOR USE IN ALL NAMESPACES ===
	pathoschild.TemplateScript.add([
		{
			name: 'clean up',
			script: cleanup
		}
	]);

	// === CALLS FOR USE IN THE PAGESPACE ===
	if(isPageNS) {
		pathoschild.TemplateScript.add([
			{
				name: 'Indent',
				script: function(editor) {
					editor.forField('#wpHeaderTextbox')
						.replace(/(<div class="pagetext">)/g, '<div class="pagetext" style="text-indent:1em;">');
				}
			},
			{
				name: 'IndianBio',
				enabled: isIndianBio,
				script: function(editor) {
					// Page:The Indian Biographical Dictionary.djvu tools
					// cleanup for Page:The Indian Biographical Dictionary.djv

					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');
				}
			},
			{
				name: 'Anchor',
				enabled: isBiography,
				script: function(editor) {
					// applies {{anchor+}}
					// Page:A Compendium of Irish Biography tools
					editor.replaceSelection(function(pre) {
						return '{{anchor+|'+pre+'}}';
					});
				}
			},
			{
				name: 'internal nslink',
				enabled: isBiography,
				script: function(editor) {
					//set to internal namespace link, set specifically for subpage xref
					editor.replaceSelection(function(pre) {
						return '{{namespace link|'+pre+'|'+pre+'|..}}';
					});
				}
			},
			{
				name: 'djvu pagelink',
				enabled: isBiography,
				script: function(editor) {
					//adds template 'DJVU page link'
					editor.replaceSelection(function(pre) {
						var work = isIrishBio ? 24 : '';
						return '{{DJVU page link|'+pre+'|'+work+'}}';
					});
				}
			},
			{
				name: 'Header',
				script: function(editor) {
					// PAGE NAMESPACE  headerbox or footerbox
					// puts {{RunningHeader}} in to headerbox
					editor.forField('#wpHeaderTextbox')
						.append('{{RunningHeader|||}}');
				}
			},
			{
				name: 'Footer',
				script: function(editor) {
					//*Footerbox app; for archaic books where the first word on the next page is placed at bottom
					editor.forField('#wpFooterTextbox')
						.replace(/(<\/div>)/, '{{RunningHeader|||}}$1');
				}
			},
			{
				name: 'nop',
				script: function(editor) {
					// nop, though it puts it at the bottom of editbox, so only useful in Page:
					editor.append('\n{{nop}}');
				}
			},
			{
				name: 's to ſ',
				script: function(editor) {
					editor.replace(/s(?![\Wf])/g, 'ſ');
				}
			},
			{
				name: 'ſ to {{ls}}',
				script: function(editor) {
					editor.replace(/ſ/g, '{{ls}}');
				}
			},
			{
				name: 'quoteleft',
				script: function(editor) {
					editor.replace(/([\s—'])(\")(?=[\w\[])/g, '$1“');
				}
			},
			{
				name: 'quoteright',
				script: function(editor) {
					editor.replace(/((\")(?=[\s—']))/g, '”');
				}
			},
			{
				name: 'squoteright',
				script: function(editor) {
					editor.replace(/(\w)(\')(\s)/g, '$1’$3');
				}
			},
			{
				name: 'squoteleft',
				script: function(editor) {
					editor.replace(/([\s—])(\')(?=\w)/g, '$1‘');
				}
			},
			{
				name: 'apostrophe',
				script: function(editor) {
					editor.replace(/(\w)(\')([tsTS][—\s\.\!\?])/g, '$1’$3');
				}
			}
		]);
	}

	// === CALLS FOR USE IN THE MAIN NAMESPACE ===
	if(isMainspace) {
		pathoschild.TemplateScript.add([
			{
				name: 'DefSort Subpage',
				script: function(editor) {
					editor.append('{{DEFAULTSORT:common.js}}');
				}
			},
			{
				name: 'DNBset',
				script: function(editor) {
					// replaces {{header}} with {{DNBset}}
					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
			},
			//{
			//	name: 'override transl',
			//	script: otranslator // I don't see any script for this to call
			//},
			{
				name: 'remove TQ',
				script: function(editor) {
					// removes {{TextQuality}}
					editor.replace(/\{\{TextQuality\|\d{2,3}%\}\}/, '');
				}
			},
			{
				name: '{{similar}}',
				script: function(editor) {
					// adds {{similar}} above header
					editor
						.replace(/\{\{header/g, '{{similar|}}\n{{header')
						.appendEditSummary('{{similar}}');
				}
			},
			{
				name: 'Ch.prev & next',
				script: function(editor) {
					// from {{header}} section, wikis Chapter -1/+1 to Prev/Next
					var newtitle = parseInt(mw.config.get('wgTitle').replace(/^.*\/Chapter (\d+)$/,'$1'))-1;
					var headerprevious = '[[../Chapter '+newtitle+'/]]';
					var newtitle = parseInt(mw.config.get('wgTitle').replace(/^.*\/Chapter (\d+)$/,'$1'))+1;
					var headernext = '[[../Chapter '+newtitle+'/]]';
					editor.replace(/\| previous   \= \n \| next       \= /, '| previous   \= ' + headerprevious +'\n \| next       \= ' + headernext);
				}
			},
			{
				name: 'Pages',
				script: function(editor) {
					// subst: #tag and some fields
					// applies <pages> using #tag and  index {{BASEPAGENAME}} fromsection tosection take {{SUBPAGENAME}}
					editor.replaceSelection('{{subst:#tag:pages||index="{{subst:BASEPAGENAME}}.djvu"|from=|to=|fromsection="{{subst:SUBPAGENAME}}"|tosection="{{subst:SUBPAGENAME}}"}}');
				}
			}
		]);
	}

	// === CALLS FOR USE IN THE USER NAMESPACE ===
	if(isUserNS) {
		pathoschild.TemplateScript.add([
			{
				name: 'welcome',
				script: function(editor) {
					// applies {{welcome}}
					editor
						.append('{{welcome}} ~~' + '~~')
						.appendEditSummary('welcome');
				}
			},
			{
				name: 'welcomeip & test',
				script: function(editor) {
					// subst {{welcomeip}} applies {{test}}
					editor
						.append('{{subst:' + 'welcomeip}} ~~' + '~~\n{{test}}')
						.appendEditSummary('welcome & test');
				}
			},
			{
				name: 'welcomeip',
				script: function(editor) {
					// subst {{welcomeip}}
					editor
						.append('{{subst:' + 'welcomeip}} ~~' + '~~')
						.appendEditSummary('welcome');
				}
			},
			{
				name: 'toggle in Page:',
				script: function(editor) {
					//subst: User:Billinghurst/HeaderToggle which tells how to toggle header in Page:
					editor.append('{{subst:' + 'User:Billinghurst/HeaderToggle}}');
				}
			}
		]);
	}

	/*********
	** Define scripts
	*********/
	//ALL NAMESPACE USE
	function cleanup(editor) {
		// all sorts of text cleaning from OCR
		editor
			// Digitized by Google (kill)
			.replace(/Digitized[\s\n]+by[\s\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 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');

		// 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.
				// but not if there's a tag, template or table syntax either side of the line break
				.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');
	}
});
// </nowiki>

// == CUSTOM TABS ==
// === Adds a tab for soft redirects below "move" ===
 /* Soft redirect */
    /* See [[WS:SCRIPTS]]; adds "soft redirect" tab in Firefox. */
       mw.loader.load('//en.wikisource.org/w/index.php?title=User:Bookofjude/softredirect.js&action=raw&ctype=text/javascript');

// == CUSTOM BUTTONS ==
/*Import from subpage*/
importScript('User:Doug/custombuttons.js');