User:Rochefoucauld/common.js

/* hide-vector-sidebar.js: Adds a button to toggle visibility of the Vector sidebar. Written by PleaseStand. Public domain; all copyright claims waived as described in http://en.wikipedia.org/wiki/Template:PD-self */

( function ( mw, $ ) {	var sidebarSwitch;

function sidebarHide { document.getElementById( 'mw-panel' ).style.visibility = 'hidden'; document.getElementById( 'mw-head-base' ).style.marginLeft = '0'; document.getElementById( 'content' ).style.marginLeft = '0'; document.getElementById( 'left-navigation' ).style.marginLeft = '0'; document.getElementById( 'footer' ).style.marginLeft = '0'; if ( sidebarSwitch ) { sidebarSwitch.parentNode.removeChild(sidebarSwitch); }		sidebarSwitch = mw.util.addPortletLink( 'p-cactions', '#', 'Show sidebar', 'ca-sidebar', 'Show the navigation links', 'a' ); $( sidebarSwitch ).click( function ( e ) {			e.preventDefault;			sidebarShow;		} ); }	function sidebarShow { document.getElementById( 'mw-panel' ).style.visibility = ''; document.getElementById( 'mw-head-base' ).style.marginLeft = ''; document.getElementById( 'content' ).style.marginLeft = ''; document.getElementById( 'left-navigation' ).style.marginLeft = ''; document.getElementById( 'footer' ).style.marginLeft = ''; if ( sidebarSwitch ) { sidebarSwitch.parentNode.removeChild(sidebarSwitch); }		sidebarSwitch = mw.util.addPortletLink( 'p-cactions', '#', 'Hide sidebar', 'ca-sidebar', 'Hide the navigation links', 'a' ); $( sidebarSwitch ).click( function ( e ) {			e.preventDefault;			sidebarHide;		} ); }	// Only activate on Vector skin if ( mw.config.get( 'skin' ) === 'vector' ) { $.when($.ready,mw.loader.using('mediawiki.util')).then(function{			// Change this if you want to show the sidebar by default			sidebarHide;		} ); } }( mediaWiki, jQuery ) ); if ($(".mw-changeslist-legend .mw-collapsible-content").is(":visible")) {       $(".mw-changeslist-legend .mw-collapsible-toggle a").click; } // == IMPORT REGEX MENU FRAMEWORK == // Regex menu framework // by m:user:Pathoschild  // Adds a sidebar menu of user-defined scripts. mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/Regex_menu_framework.js&action=raw&ctype=text/javascript'); // == CONVENIENCE FUNCTIONS == // === FOR EXTRACTING INFORMATION ABOUT PAGE === //convenience function - pulls djvu page number out of url function page {   var m = /\.djvu\/([0-9]+)&action=edit/g.exec(location.href);   if (m==null) return null;   return parseInt(m[1]); } var pagenum=page; //convenience function - identifies namespace function get_namespace {   var m = /index.php\?title=Page:/g.exec(location.href); if (m!=null) return "Page"; return "other"; } var namespace=get_namespace; //convenience function - pulls work title out of url. // reversal of url encoding is a dirty hack function work { var m = /title=Page:([^\.]+\.djvu)/g.exec(location.href); if (m==null) return null; t = m[1].replace(/_/g, " "); return t; } var worktitle = work; function getpagename { var m = /title=([^&]+)&/g.exec(location.href); if (m==null) return null; t = m[1].replace(/_/g, " "); t = t.replace(/%3F/g, "?"); t = t.replace(/%27/g, "'"); t = t.replace(/%22/g, '"');  return t; } var pagename = getpagename; // find out page quality status function getQuality {    var q = document.getElementById('pagequality');    if (q == null) {        return "Nonexistent";    } else {        c = q.getAttribute("class");        switch(c) {            case "quality0": return "Without text";            case "quality1": return "Not proofread";            case "quality2": return "Problematic";            case "quality3": return "Proofread";            case "quality4": return "Validated";        }    }    return null; } // These variables can be used to customize operations depending on whether you are working with unproofed OCR or already proofed text. var quality = getQuality; var proofed = ((quality != "Nonexistent") && (quality != "Not proofread")); // === FOR CONVERSIONS === // Convenience function: converts some text into title case function titlecase(text) { // split text into individual words and examine them one by one var textArray = text.toLowerCase.split(" "); for (i in textArray) { switch(textArray[i]) {              case "a": case "an": case "and": case "as": case "at": case "but": case "by": case "etcetera": case "etc.": case "for": case "from": case "in": case "nor": case "of": case "o'": case "on": case "or": case "the": case "to": case "with": case "versus": case "vs.": case "v.": case "yet": break; // don't capitalise articles, "to" as part of an infinitive, prepositions or short conjunctions default: // capitalise everything else textArray[i] = textArray[i].substring(0,1).toUpperCase + textArray[i].substring(1,textArray[i].length) break; }       }        // capitalise first word regardless textArray[0] = textArray[0].substring(0,1).toUpperCase + textArray[0].substring(1,textArray[0].length) // capitalise last word regardless var last = textArray.length-1; textArray[last] = textArray[last].substring(0,1).toUpperCase + textArray[last].substring(1,textArray[last].length) // reconstruct title var titleCase="" for (i in textArray) { titleCase += textArray[i]; if (i < last) titleCase += " "; }       return titleCase; } // == SIDEBAR TOOLS == function reload_page_image{ var thumbElement = document.getElementById("ProofReadImage"); if (thumbElement == null) return; var match = thumbElement.src.match(/-([0-9]+)px/); if (match == null) return; thumbElement.src = thumbElement.src.replace(match[0], "-"+(parseInt(match[1])*9/10).toString+"px"); } function cleanup(poem) { var editbox = document.getElementsByName('wpTextbox1')[0]; var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; var footerbox = document.getElementsByName('wpFooterTextbox')[0]; // anything noincluded at the start or end of the edit box should be pushed into the header and footer respectively. if (/^\/.test(editbox.value)) { var e = editbox.value.indexOf(" ") // append to header a trimmed version of whatever is inside the leading noinclude headerbox.value = headerbox.value + "\n" + editbox.value.substr(11, e-11).replace(/^\s+|\s+$/g, ''); // remove leading noinclude from editbox editbox.value = editbox.value.substr(e+12); }       if (/\<\/noinclude\>$/.test(editbox.value)) { var s = editbox.value.lastIndexOf(" "); // prepend to footer a trimmed version of whatever is inside the trailing noinclude footerbox.value = editbox.value.substr(s+11, editbox.value.length-s-11-12).replace(/^\s+|\s+$/g, '') + "\n" + footerbox.value // remove trailing noinclude from editbox editbox.value = editbox.value.substr(0, s)       } // remove trailing spaces at the end of each line editbox.value = editbox.value.replace(/ +\n/g, '\n'); // remove trailing whitespace preceding a hard line break editbox.value = editbox.value.replace(/ +/g, ' '); // remove trailing whitespace at the end of page text editbox.value = editbox.value.replace(/\s+$/g, ''); // remove trailing spaces at the end of refs editbox.value = editbox.value.replace(/ +<\/ref>/g, ' '); // remove trailing spaces at the end of template calls editbox.value = editbox.value.replace(/ +}}/g, '}}'); // convert double-hyphen to mdash (avoiding breaking HTML comment syntax) editbox.value = editbox.value.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). editbox.value = editbox.value.replace(/ +— +/g, '—'); // join words that are hyphenated across a line break // (but leave "|-" table syntax alone) editbox.value = editbox.value.replace(/([^\|])-\n/g, '$1'); // stuff to do only if the page doesn't contain a tag: if (-1 == editbox.value.indexOf(" ")) { // lines that start with " should probably be new lines,               // if the previous line ends in punctuation,                // other than a comma or semicolon                // and let's get rid of trailing space while we're at it                editbox.value = editbox.value.replace(/([^\n\w,;])\n\" */g, '$1\n\n"');                // lines that end with " should probably precede a new line, // unless preceded by a comma, // or unless the new line starts with a lower-case letter; // and let's get rid of preceding space while we're at it               editbox.value = editbox.value.replace(/([^,])\ *\"\n([^a-z\n])/g, '$1"\n\n$2'); if (poem) { // for poems I want to preserve line breaks // by inserting hard breaks editbox.value = editbox.value.replace(/([^>}\|\n])\n([^<{\|\n])/g, '$1 \n$2')+" "; } else { // remove single line breaks; preserve multiple. // but not if there's a tag, template or table syntax either side of the line break editbox.value = editbox.value.replace(/([^>}\|\n])\n([^<{\|\n])/g, '$1 $2'); }               // collapse sequences of spaces into a single space editbox.value = editbox.value.replace(/ +/g, ' '); }       // dump spurious hard breaks at the end of paragraphs editbox.value = editbox.value.replace(/\n\n/g, '\n\n'); // remove unwanted spaces around punctuation marks editbox.value = editbox.value.replace(/ ([;:\?!,])/g, '$1'); // unicodify editbox.value = editbox.value.replace(/&mdash;/g, '—') editbox.value = editbox.value.replace(/–/g, '–') //OCR fixes // convert i9 to 19, etc.	editbox.value = editbox.value.replace(/[il]([0-9])/g, '1$1'); // "the", "them", "their", etcetera editbox.value = editbox.value.replace(/tlie/g, 'the'); // "U" -> "ll" when preceded by a lowercase letter. editbox.value = editbox.value.replace(/([a-z])U/g, '$1ll'); // replace "float center" with "block center"; original template name was misleading enough be warrant routinely fixing editbox.value = editbox.value.replace(/\{\{float center/g, '{{block center'); editbox.value = editbox.value.replace(/ \s*([.\n]*?)\s*<\/center>/g, '{{center|$1}}') // temporary fix just for Portrait if (-1 != location.href.indexOf('A_Portrait_of_the_Artist_as_a_Young_Man')) { editbox.value = editbox.value.replace(/\n\n\n/g, '\n\n'); } } //Make selected text into appropriately capitalised title link // e.g. "THE MAN FROM SNOWY RIVER" —> "THE MAN FROM SNOWY RIVER" function maketitle { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var text = editbox.value.substring(selStart, selEnd); var target = titlecase(text); // construct link text var linked; if (target==text) { linked = +text+; } else { linked = +text+; }       // replace text with link text editbox.value = editbox.value.substring(0, selStart) + linked + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+linked.length, selStart+linked.length); } //Make selected text into author link function author { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var name = editbox.value.substring(selStart, selEnd); var target; // if name contains a comma, switch around when linking var commaPos = name.indexOf(','); if (commaPos == -1) { target = name; } else { target = name.substr(commaPos+1).replace(/^\s+|\s+$/g, ) + ' ' + name.substr(0, commaPos).replace(/^\s+|\s+$/g, ); }       // If name is all in capitals, convert target to title case. if (target == target.toUpperCase) { target = titlecase(target) }       var linked = +name+; editbox.value = editbox.value.substring(0, selStart) + linked + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+linked.length, selStart+linked.length); } function surnameFirst { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var name = editbox.value.substring(selStart, selEnd); // If name is all in capitals, convert target to title case. var target = name; if (target == target.toUpperCase) { target = titlecase(target) }       // split text into individual words var nameArray = name.split(" "); // put last first, followed by a comma, then all the rest preceded by spaces name = nameArray[nameArray.length-1]+","; for (i=0; i<nameArray.length-1; i++) name = name+" "+nameArray[i] var linked = +name+; editbox.value = editbox.value.substring(0, selStart) + linked + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+linked.length, selStart+linked.length); } //Mark selected text up with small-caps function smallcaps { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var pre = editbox.value.substring(selStart, selEnd); // Applying small-caps to all-caps text is pointless...       // ... unless the all-caps is OCR of text that is actually small-caps. // Check if text is all-caps, and if it is, convert it to title case before applying small-caps. if (pre == pre.toUpperCase) { pre = titlecase(pre) }       var post = ''; editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function make_center { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; editbox.value = editbox.value.substring(0, selStart) + ''                    + editbox.value.substring(selStart, selEnd) + ''                     + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart, selStart+11); } function make_right { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; editbox.value = editbox.value.substring(0, selStart) + ''                    + editbox.value.substring(selStart, selEnd) + ''                     + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart, selStart+10); } function SIC { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; editbox.value = editbox.value.substring(0, selStart) + ''                    + editbox.value.substring(selStart, selEnd) + '['                    + editbox.value.substring(selStart, selEnd) + '] [sic]' + editbox.value.substring(selEnd); editbox.setSelectionRange(selEnd+8, selEnd+8); } //wrap first word in hwe template function hwe { var editbox = document.getElementsByName('wpTextbox1')[0]; var to = editbox.value.search(/\W/); if (to == -1) to = editbox.value.length; var pre = editbox.value.substring(0, to); var post = ''; editbox.value = post + editbox.value.substring(to); } //wrap last word in hws template function hws { var editbox = document.getElementsByName('wpTextbox1')[0]; var from = editbox.value.lastIndexOf(" "); if (from == -1) from = 0; else from = from + 1; var pre = editbox.value.substring(from); if (pre.slice(-1) == "-") pre = pre.slice(0, -1); var post = ''; editbox.value = editbox.value.substring(0, from) + post; } // As you work your way through the page, when you encounter a reference, just mark it with tags and continue. // Once you've got to the end of the page and proofed the references, simply highlight each reference in turn, // and use this function to move it to its proper position. function makeref { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var refStart = editbox.selectionStart; var refEnd = editbox.selectionEnd; var firstref = editbox.value.indexOf(' '); if (-1 != firstref) { editbox.value = editbox.value.slice(0,firstref+5) + editbox.value.slice(refStart, refEnd) + editbox.value.slice(firstref+5, refStart) + editbox.value.slice(refEnd); } } function ligature { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; if (selStart == selEnd && selEnd != 0) { selStart = selEnd-2; }       var pre = editbox.value.substring(selStart, selEnd); switch(pre) {       case 'ae': post = 'æ'; break; case 'AE': post = 'Æ'; break; case 'oe': post = 'œ'; break; case 'OE': post = 'Œ'; break; }       editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function diareses { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; if (selStart == selEnd && selEnd != 0) { selStart = selEnd-1; }       var pre = editbox.value.substring(selStart, selEnd); switch(pre) {       case 'a': post = 'ä'; break; case 'A': post = 'Ä'; break; case 'e': post = 'ë'; break; case 'E': post = 'Ë'; break; case 'i': post = 'ï'; break; case 'I': post = 'Ï'; break; case 'o': post = 'ö'; break; case 'O': post = 'Ö'; break; case 'u': post = 'ü'; break; case 'U': post = 'Ü'; break; case 'y': post = 'ÿ'; break; default: post = pre; break; }       editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function acute { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; if (selStart == selEnd && selEnd != 0) { selStart = selEnd-1; }       var pre = editbox.value.substring(selStart, selEnd); switch(pre) {       case 'a': post = 'á'; break; case 'A': post = 'Á'; break; case 'c': post = 'ć'; break; case 'C': post = 'Ć'; break; case 'e': post = 'é'; break; case 'E': post = 'É'; break; case 'g': post = 'ģ'; break; case 'i': post = 'í'; break; case 'I': post = 'Í'; break; case 'o': post = 'ó'; break; case 'O': post = 'Ó'; break; case 'l': post = 'ĺ'; break; case 'L': post = 'Ĺ'; break; case 'n': post = 'ń'; break; case 'N': post = 'Ń'; break; case 'r': post = 'ŕ'; break; case 'R': post = 'Ŕ'; break; case 's': post = 'ś'; break; case 'S': post = 'Ś'; break; case 'u': post = 'ú'; break; case 'U': post = 'Ú'; break; case 'y': post = 'ý'; break; case 'Y': post = 'Ý'; break; case 'z': post = 'ź'; break; case 'Z': post = 'Ź'; break; default: post = pre; break; }       editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function grave { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; if (selStart == selEnd && selEnd != 0) { selStart = selEnd-1; }       var pre = editbox.value.substring(selStart, selEnd); switch(pre) {       case 'a': post = 'à'; break; case 'A': post = 'À'; break; case 'e': post = 'è'; break; case 'E': post = 'È'; break; case 'i': post = 'ì'; break; case 'I': post = 'Ì'; break; case 'o': post = 'ò'; break; case 'O': post = 'Ò'; break; case 'u': post = 'ù'; break; case 'U': post = 'Ù'; break; default: post = pre; break; }       editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function circumflex { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; if (selStart == selEnd && selEnd != 0) { selStart = selEnd-1; }       var pre = editbox.value.substring(selStart, selEnd); switch(pre) {       case 'a': post = 'â'; break; case 'A': post = 'Â'; break; case 'c': post = 'ĉ'; break; case 'C': post = 'Ĉ'; break; case 'e': post = 'ê'; break; case 'E': post = 'Ê'; break; case 'g': post = 'ĝ'; break; case 'G': post = 'Ĝ'; break; case 'h': post = 'ĥ'; break; case 'H': post = 'Ĥ'; break; case 'i': post = 'î'; break; case 'I': post = 'Î'; break; case 'j': post = 'ĵ'; break; case 'J': post = 'Ĵ'; break; case 'o': post = 'ô'; break; case 'O': post = 'Ô'; break; case 's': post = 'ŝ'; break; case 'S': post = 'Ŝ'; break; case 'u': post = 'û'; break; case 'U': post = 'Û'; break; case 'w': post = 'ŵ'; break; case 'W': post = 'Ŵ'; break; case 'y': post = 'ŷ'; break; case 'Y': post = 'Ŷ'; break; default: post = pre; break; }       editbox.value = editbox.value.substring(0, selStart) + post + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart+post.length, selStart+post.length); } function uc { var editbox = document.getElementsByName('wpTextbox1')[0]; matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value); while (matches != null) { editbox.value = editbox.value.replace(matches[0], matches[1].toUpperCase); matches = /\{\{[Uu]c\s*\|\s*([^\}]+)\}\}/g.exec(editbox.value); } } function upper { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.focus; var selStart = editbox.selectionStart; var selEnd = editbox.selectionEnd; var text = editbox.value.substring(selStart, selEnd); var upperCaseText = text.toUpperCase editbox.value = editbox.value.substring(0, selStart) + upperCaseText + editbox.value.substring(selEnd); editbox.setSelectionRange(selStart, selStart+upperCaseText.length); } // == SHORTCUT SUPPORT == function deregisterShortcut(shortcut) { switch(shortcut) {               case "'": result=document.querySelector("[accesskey=\"'\"]"); break; case '"':                       result=document.querySelector('[accesskey=\'"\']'); break; default: //alert("DEREGISTER: "+document.querySelector("accesskey=\""+shortcut+"\"")); result=document.querySelector("[accesskey=\""+shortcut+"\"]"); break; }       if (result) { result.removeAttribute("accesskey"); } } function regexToolWithShortcut(name,functionname,shortcut) { var newline = document.createElement('li'); var newlink = document.createElement('button'); newlink.setAttribute('style', 'background:none!important; border:none; padding:0!important; text-align: left; '); newlink.setAttribute('onclick',functionname); newlink.setAttribute('title',name); newlink.setAttribute('class','sidebar-link'); if ((shortcut == null) || (shortcut=="")) { // do nothing } else { deregisterShortcut(shortcut); newlink.setAttribute('accesskey', shortcut); }	newlink.appendChild(document.createTextNode(name)); newline.appendChild(newlink); toollist.appendChild(newline); } // == REGISTER TOOLS IN SIDEBAR == function rmflinks { // stuff I routinely do at the start if (namespace == "Page") { regexToolWithShortcut('clean up','cleanup(false)',null); regexToolWithShortcut('reload', 'reload_page_image',null); regexToolWithShortcut('hws','hws',null); regexToolWithShortcut('hwe','hwe',null); regexToolWithShortcut('Custom regex','custom'); // a default tool which performs regex input in a dynamic form }      // stuff I only do via shortcuts; if I could create these shortcuts without adding them to the sidebar, I would. regexToolWithShortcut('small-caps [k]','smallcaps', 'k'); regexToolWithShortcut('center [c]','make_center', 'c'); regexToolWithShortcut('right [r]','make_right', 'r'); regexToolWithShortcut('SIC [?]','SIC', '/'); regexToolWithShortcut('ligature [-]','ligature', '-'); regexToolWithShortcut('diaresis [:]','diareses', ':'); regexToolWithShortcut('acute [\']','acute', '\''); regexToolWithShortcut('grave [`]','grave', '`'); regexToolWithShortcut('circumflex [^]','circumflex', '6'); // stuff I do at the end if (namespace == "Page") { regexToolWithShortcut('makeref', 'makeref',null); regexToolWithShortcut('clean up (poem)','cleanup(true)',null); regexToolWithShortcut('uc','uc',null); }       // stuff I can do any time regexToolWithShortcut('title [t]','maketitle', 't'); regexToolWithShortcut('author [a]','author','a'); regexToolWithShortcut('author (surname first) [,]','surnameFirst',','); regexToolWithShortcut('upper [u]','upper', 'u'); } //