User:Guccisamsclub/common.js

importScript( 'User:ערן/autocomplete.js' ); // Backlink: User:ערן/autocomplete.js

/* segregate-refs.js: A user script to simplify editing of articles using inline ref tags with the Cite.php extension to MediaWiki. Copyright (c) 2010, PleaseStand This software is licensed under these licenses: 1. Creative Commons Attribution-Share Alike 3.0 Unported License (see  for the text) 2. GNU Free Documentation License, any published version. (see  for the text) 3. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN		ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. You may select the license(s) of your choice if you wish to copy, modify, or distribute this software. If you modify the software and do not wish to	license your changes under one or more of the licenses, please remove the license(s) from the list above.

/*global window, addOnloadHook, SegregateRefsJsL10n, SegregateRefsJsAllowConversion, wikEdUseWikEd, WikEdUpdateTextarea, WikEdUpdateFrame*/

// Translate the right-hand side of these if necessary. // Put translations in a separate file, changing the first line to: // var SegregateRefsJsL10n = { var SegregateRefsJsMsgs = { version: 1.11, buttonText: "Segregate refs for editing", buttonStyle: "background: #dfd;", buttonConvertText: "Migrate article to LDR", buttonConvertStyle: "background: #fdd;", autoWord: "Auto", convertRefsWarning: "WARNING: You need consensus to migrate an article to list-defined references format (LDR) BEFORE you do so.\n\nClick Cancel now if consensus has not been established in favor of this migration. If there is consensus to make the conversion, click OK to do so.", groupPrompt: "Please enter the name of a group (as it appears in the wikitext, including any quotes). Leave this blank if unsure.", refsHeader: "Inline footnotes", convertHeader: "Generated refs list", refsCommentComplete: "\n\n", convertSummary: "Converted footnotes to LDR format (using segregate-refs)", convertFurther: "This script has done most of the work. However, you still need to do the following:\n\n* Insert the refs list in the new textbox into the proper place in the wikitext.\n* If converting a special group, optionally remove the group attributes.\n* Replace all autogenerated names with human-generated names.\n\nYou can do the above with the Find/Replace command in many text editors. (Always use the quoted form of the attributes.) Then, paste the text back into the edit form and save the page.", integrateWarning: "The refs listed below are missing from the text. If you continue, they will be permanently deleted. Are you sure?\n\nUnused refs: " };

( function ( $ ) {

var editForm, refsDiv, refsH2, mainTextbox, refsTextbox, randPrefix, messages, refsButton, convertButton, unloadHandlerRegistered = false;

function has( obj, key ) { return Object.prototype.hasOwnProperty.call( obj, key ); }

/** * Unquote a wikitext tag attribute. * * @param string quotedValue * @return string */ function htmlUnquote( quotedValue ) { var d = document.createElement( 'div' ); d.innerHTML = ' '; return d.firstChild.value; }

/** * Quote a wikitext tag attribute, choosing single quotes versus * double quotes depending on which is shorter. * * @param string value * @return string */ function htmlQuote( value ) { var sQ, dQ;

value = value.replace( /\&/g, '&amp;' ); sQ = "'" + value.replace( /'/g, '&#39;' ) + "'", dQ = '"' + value.replace( /"/g, '&quot;' ) + '"';

return sQ.length < dQ.length ? sQ : dQ; }

// Looks for ref tags in the text, skipping problematic extension tags. // For example, "references" may contain out-of-line refs, which should be skipped. function RefScanner( argWikiText ) { this.wikiText = argWikiText; this.refScanRegex = /(?:|<(nowiki|source|references|ref)(?:|\s(?:[^"']|"[^"]*"|'[^']*')*?)(?:\/>|(?:>[\s\S]*?<\/\1(?:|\s[^>]*)>)))/gi; }

// Get the next ref from the text. RefScanner.prototype.getRef = function { var results; do { results = this.refScanRegex.exec( this.wikiText ); if ( !results ) { return null; }		if ( results[1] === undefined ) { results = [0, 0]; }	} while ( results[1].toString.toLowerCase !== 'ref' ); return results[0]; };

// Extracts attributes from ref tags. function RefParser( argWikiText ) { // This is mostly a copy of refScanRegex, except that the whole string must be a ref, // and no more, and two parts are extracted: $1=attributes, $2=remaining portion of ref var refParseRegex = /^|(?:>[\s\S]*?<\/ref(?:|\s[^>]*)>))$/i;

this.wikiText = argWikiText; this.parsedRef = refParseRegex.exec( this.wikiText ); if ( !this.parsedRef ) { throw new Error( 'invalid ref' ); } }

// Get all attributes of the tag. RefParser.prototype.getAttributes = function { // In this regex, we need to extract a single name-value pair at a time. var attParseRegex = /\s([^\s=>]+)\s*=\s*("[^"]*"|'[^']*'|[^\s"']*)/g; if ( !this.parsedRef ) { return null; }	var attributes = {}, results; while ( ( results = attParseRegex.exec( this.parsedRef[1] ) ) ) { attributes[results[1].toLowerCase] = htmlUnquote( results[2] ); }	return attributes; };

/** * Segregate refs from content. * * @param string argWikiText The original wikitext * @param string group The name of the ref group to process (default group is '') * @param bool caseCues Mark the original ref code locations using capitalization? * @return Object */ function segregateRefs( argWikiText, group, caseCues ) { var prefixChars, randNo, randPrefix, refPreferred, scanner, ref, parser, attributes, refGroup, refName, refStored, refEmpty, refLong, unnamedRefs = 0, refNames = {}, refCodes = [], refShort, outWikiText = '', offset = 0;

// Create a random prefix for autogenerated ref names prefixChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; randNo = Math.floor( Math.random * ( prefixChars.length * prefixChars.length ) ); randPrefix = messages.autoWord + prefixChars.charAt( Math.floor( randNo / prefixChars.length ) ) + prefixChars.charAt(randNo % prefixChars.length) + '-';

// Create the beginning of the code for a preferred ref location refPreferred = caseCues ? '' || parser.parsedRef[2].slice( 0, 3 ) == '></'; refLong = ref;

} else { // We have to autogenerate one refName = randPrefix + ( ++unnamedRefs ).toString(10); refStored = false; refEmpty = false; refLong = ''; } else { refShort = refPreferred + 'name=' + htmlQuote( refName ) + ' group=' + htmlQuote( refGroup ) + '/>'; }

} else if ( !refEmpty && refNames[refName].empty ) { // Already found an empty ref under this name, yet this one is non-empty // Fill in the long code for the existing entry refCodes[refNames[refName].code] = refLong; refNames[refName].empty = false;

// Make a short code for the ref refShort = refPreferred + 'name=' + htmlQuote( refName ); if ( refGroup !== '' ) { refShort += ' group=' + htmlQuote( refGroup ) + '/>'; }			refShort += '/>';

} else { // Leave the ref as-is refShort = caseCues ? refLong.replace( /^<REF/, "<ref" ) : refLong; }

// Replace the long code with the short code outWikiText += argWikiText.slice( offset, scanner.refScanRegex.lastIndex - ref.length ); outWikiText += refShort; offset = scanner.refScanRegex.lastIndex; }

outWikiText += argWikiText.slice( offset );

return { wikiText: outWikiText, refCodes: refCodes, randPrefix: randPrefix }; }

/** * Insert ref contents back into the text. * * @param string argWikiText The wikitext without ref contents * @param string argRefText The ref contents * @param string randPrefix The randPrefix value returned by segregateRefs * @param string caseCues The caseCues argument passed to segregateRefs */ function integrateRefs(argWikiText, argRefText, randPrefix, caseCues) {

// Remove an autogenerated ref name if possible. function cleanRefLong(dirtyRef) { var cleanRegex = /^<(ref) name=(?:"[^"]*"|'[^']*'|[^\s"']*)/i; return dirtyRef.replace(cleanRegex, '<$1'); }

var scanner, ref, parser, attributes, refCodes = {}, usageFreq = {}, preferredRef = {}, refLong, outWikiText = '', offset = 0;

// First, we build an associative array of all the ref codes // that we might need to put back into the text. scanner = new RefScanner( argRefText ); while ( ( ref = scanner.getRef ) ) { parser = new RefParser( ref ); attributes = parser.getAttributes; if ( attributes.name !== undefined ) { // Only use the first ref having each name if ( !refCodes.hasOwnProperty( attributes.name ) ) { refCodes[attributes.name] = ref; }		}	}

// Next, we build an associative array that holds the usage frequency // of every ref name used in text, and whether there is a preferred ref, // if caseCues are enabled. scanner = new RefScanner( argWikiText ); while ( ( ref = scanner.getRef ) ) { parser = new RefParser( ref ); attributes = parser.getAttributes; if ( attributes.name !== undefined ) { if ( !has( usageFreq, attributes.name ) ) { usageFreq[attributes.name] = 1; } else { usageFreq[attributes.name]++; }			if ( caseCues && ref.slice( 0, 4 ) == '<REF' ) { preferredRef[attributes.name] = true; }		}	}

// Finally, we go through the text again and this time we insert the // ref codes where we need to, but only in the first place // a ref name appears (or the first preferred location). scanner = new RefScanner( argWikiText ); while ( ( ref = scanner.getRef ) ) { parser = new RefParser( ref ); attributes = parser.getAttributes; if ( has(attributes, 'name' ) ) { // Is this name on the replacement list? if ( has( refCodes, attributes.name ) ) {

// If we are using caseCues, and another location is // preferred, skip to the next ref. if ( caseCues && has( preferredRef, attributes.name ) &&					ref.slice( 0, 4 ) != ' 1 ) { // Multiple: the replacement code should be the same // as that stored in the ref textbox. refLong = refCodes[attributes.name]; } else { // Single: replacement code must not include the name, // at least not if the citation was untouched. // (We don't want to add unnecessary autonames) refLong = cleanRefLong( refCodes[attributes.name] ); }				} else { // No: the replacement code should be the same // as that stored in the ref textbox. // (We want to preserve all human-generated names) refLong = refCodes[attributes.name]; }				// Replace the short code with the long code outWikiText += argWikiText.slice( offset, scanner.refScanRegex.lastIndex - ref.length ); outWikiText += refLong; offset = scanner.refScanRegex.lastIndex;

// Delete the name from the replacement list delete refCodes[attributes.name]; }		}	}

outWikiText += argWikiText.slice( offset );

return { wikiText: outWikiText, unusedRefs: refCodes }; }

/** * Clear the undo history of a textarea by removing it from the document * and then inserting it again. * * @param HTMLTextareaElement ta The textarea element */ function clearUndoHistory(ta) { var pn = ta.parentNode, ns = ta.nextSibling; pn.insertBefore(pn.removeChild(ta), ns); }

function unloadHandler(evt) { // Local variables var result, refName, unusedRefNamesQuoted = []; // wikEd compatibility (frame -> textarea) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateTextarea; }	// Do the actual integration work result = integrateRefs(mainTextbox.value, refsTextbox.value, randPrefix, true);

// Find all unused ref names for(refName in result.unusedRefs) { if(has(result.unusedRefs, refName)) { unusedRefNamesQuoted.push(htmlQuote(refName)); }	}	// If any refs are unused, warn and allow the user to cancel; // we do not do this on unload because it is not really possible. if(evt.type == "submit" && unusedRefNamesQuoted.length) { if(!window.confirm(messages.integrateWarning + unusedRefNamesQuoted.join(", "))) { // Don't submit form evt.preventDefault; return false; }	}	// Otherwise, update the textbox. mainTextbox.value = result.wikiText; // wikEd compatibility (textarea -> frame) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateFrame; }	// Deactivate this event handler window.removeEventListener("submit", unloadHandler, false); window.removeEventListener("unload", unloadHandler, false); unloadHandlerRegistered = false; // We can delete the header and refs textbox now refsDiv.removeChild(refsH2); refsDiv.removeChild(refsTextbox); return true; }

function refsButtonHandler { // Called when script activated by button click // Both buttons should disappear if(convertButton.parentNode){ convertButton.parentNode.removeChild(convertButton); }	if(refsButton.parentNode) { refsButton.parentNode.removeChild(refsButton); }

// wikEd compatibility (frame -> textarea) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateTextarea; }	// Do the actual segregation work and save the random prefix var segFormat = segregateRefs(mainTextbox.value, "", true); if(!segFormat) { return false; }	randPrefix = segFormat.randPrefix; // Update the textbox mainTextbox.value = segFormat.wikiText; clearUndoHistory(mainTextbox); // wikEd compatibility (textarea -> frame) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateFrame; }	// Inline refs header refsH2 = document.createElement("h2"); refsH2.appendChild(document.createTextNode(messages.refsHeader)); refsH2.style.borderColor = "silver"; refsH2.style.borderStyle = "solid"; refsH2.style.borderWidth = "1px 0"; refsH2.style.fontSize = "100%"; refsH2.style.fontWeight = "100%"; refsH2.style.lineHeight = "normal"; refsH2.style.margin = "0"; refsH2.style.padding = "0.5em 0 0"; refsH2.style.textAlign = "center"; // Inline refs textbox refsTextbox = document.createElement("textarea"); refsTextbox.id = "PsRefsTextbox"; refsTextbox.value = segFormat.refCodes.join("\n\n"); refsTextbox.rows = Math.floor(mainTextbox.rows / 2); refsTextbox.cols = mainTextbox.cols; refsTextbox.style.border = "none"; refsTextbox.style.lineHeight = "1.5em"; refsTextbox.style.margin = "0"; refsTextbox.style.resize = "vertical"; refsTextbox.style.width = "100%";

// Add to document refsDiv.appendChild(refsH2); refsDiv.appendChild(refsTextbox); // Set up the submit handler (to integrate refs when done editing) window.addEventListener("submit", unloadHandler, false); window.addEventListener("unload", unloadHandler, false); unloadHandlerRegistered = true; // Don't submit form return false; }

function convertButtonHandler { // Called when script activated by button click // Display warning if(!window.confirm(messages.convertRefsWarning)) { return false; }	// Which group? var group = window.prompt(messages.groupPrompt, ""); if(group === null) { return false; }	group = htmlUnquote(group); // The first button should disappear if(refsButton.parentNode) { refsButton.parentNode.removeChild(refsButton); }	// Do the actual segregation work and save the random prefix var segFormat = segregateRefs(mainTextbox.value, group, false); if(!segFormat) { return false; }	randPrefix = segFormat.randPrefix; // wikEd compatibility (frame -> textarea) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateTextarea; }	// Update the textbox mainTextbox.value = segFormat.wikiText; clearUndoHistory(mainTextbox); // wikEd compatibility (textarea -> frame) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateFrame; }	// Inline refs header refsH2 = document.createElement("h2"); refsH2.appendChild(document.createTextNode(messages.convertHeader)); refsH2.style.borderColor = "silver"; refsH2.style.borderStyle = "solid"; refsH2.style.borderWidth = "1px 0"; refsH2.style.fontSize = "100%"; refsH2.style.fontWeight = "100%"; refsH2.style.lineHeight = "normal"; refsH2.style.margin = "0"; refsH2.style.padding = "0.5em 0 0"; refsH2.style.textAlign = "center"; // Inline refs textbox if(!refsTextbox) { // Does not exist; creating refsTextbox = document.createElement("textarea"); refsTextbox.id = "PsRefsTextbox"; refsTextbox.value = messages.refsCommentComplete + segFormat.refCodes.join("\n"); refsTextbox.rows = Math.floor(mainTextbox.rows / 2); refsTextbox.cols = mainTextbox.cols;

refsTextbox.style.border = "none"; refsTextbox.style.lineHeight = "1.5em"; refsTextbox.style.margin = "0"; refsTextbox.style.resize = "vertical"; refsTextbox.style.width = "100%"; // Add to document refsDiv.appendChild(refsH2); refsDiv.appendChild(refsTextbox); } else { // Already exists refsTextbox.value = messages.refsCommentComplete + segFormat.refCodes.join("\n"); }	// Set a default edit summary. document.getElementById("wpSummary").value = messages.convertSummary; // Show the further instructions. window.alert(messages.convertFurther); // Don't submit form return false; }

function getEditboxContents { // ajaxPreview compatibility if(unloadHandlerRegistered) { // wikEd compatibility (frame -> textarea) if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) { WikEdUpdateTextarea; }		return integrateRefs(mainTextbox.value, refsTextbox.value, randPrefix, true).wikiText; } else { return mainTextbox.value; } }

// Leave a global for ajaxPreview to use. window.getEditboxContents = getEditboxContents;

$( function {

try {

if( !window.addEventListener ) { return; }		// Handle message translations messages = (typeof SegregateRefsJsL10n == "object" &&					typeof SegregateRefsJsL10n.version != "undefined" &&					SegregateRefsJsL10n.version == 1.11 ? SegregateRefsJsL10n :					SegregateRefsJsMsgs); // Only activate on edit pages (that are not section edit pages) if(!document.getElementById("editform") ||		document.getElementById("editform").wpSection.value.length) { return; }		// Get the edit form editForm = document.getElementById("editform"); // Get the edit box mainTextbox = document.getElementById("wpTextbox1"); // Make the "segregate" button refsButton = document.createElement("input"); refsButton.type = "button"; refsButton.value = messages.buttonText; refsButton.setAttribute("style", messages.buttonStyle); refsButton.onclick = refsButtonHandler; // Make the "convert" button convertButton = document.createElement("input"); convertButton.type = "button"; convertButton.value = messages.buttonConvertText; convertButton.setAttribute("style", messages.buttonConvertStyle); convertButton.onclick = convertButtonHandler; if(typeof SegregateRefsJsAllowConversion == "undefined" ||		!SegregateRefsJsAllowConversion) { convertButton.setAttribute("style", "display: none;"); }		// Add the refs div refsDiv = document.createElement("div"); refsDiv.appendChild(refsButton); refsDiv.appendChild(convertButton); mainTextbox.parentNode.insertBefore(refsDiv, mainTextbox.nextSibling); } catch(e) { }

} );

})( jQuery );

// User:ais523/bracketmatch.js - Colour matching brackets in a copy of the edit box. // By User:ais523, on a suggestion by User:Absidy.

addOnloadHook(function{ var p=document.getElementById('wikiPreview');  if(p==null) p=document.getElementById('viewsourcetext');  if(p)    p.innerHTML+="Parse ";  try  {    var edh=document.getElementsByClassName('editHelp')[0];    if(bm_useparsebutton)      edh.innerHTML='  '+edh.innerHTML;  } catch(x) {} });

function bm_parsebrackets { var t=document.getElementById('wpTextbox1').value; var a=sajax_init_object; var p='action=expandtemplates&generatexml=1&format=json&callback=bm_callback&text='+encodeURIComponent(t); a.open('POST', mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/api.php'); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); a.setRequestHeader("Content-length", p.length); a.setRequestHeader("Connection", "close"); a.onreadystatechange=function{bm_apirespond(a)}; a.send(p); }

function bm_showparsed { bm_parsebrackets; return 0; }

function bm_apirespond(a) { try {   if(a.readyState==4) eval(a.responseText); } catch(e) {   document.getElementById('bm_parseres').innerHTML= "Could not parse due to a server error. Parse"; } }

var colang;

function bm_hexdigit(n) { var i=Math.floor(n); if(i<10) return i+''; if(i==10) return 'A'; if(i==11) return 'B'; if(i==12) return 'C'; if(i==13) return 'D'; if(i==14) return 'E'; if(i==15) return 'F'; }

function bm_tohex(n) { var i=Math.floor(n); return bm_hexdigit(i/16)+bm_hexdigit(i%16); }

function bm_gencol(ang) { var r=Math.sin(ang)+1; var g=Math.sin(ang+3.14159*2/3)+1; var b=Math.sin(ang-3.14159*2/3)+1; return bm_tohex(r*127.5)+bm_tohex(g*127.5)+bm_tohex(b*127.5); }

function bm_callback(o) { var subs={"tplarg":"", "template":"", "part":"|", "ext":"&"+"lt;", "/attr":"&"+"gt;", "attr/":"&"+"gt;"}; var h=o.parsetree["*"].split("<"); var i=h.length; var r; var n;  var sp=0; var st=new Array; var col; colang=0; while(--i) {   col=""; n=1; h[i]=h[i].split(">"); h[i][0]=h[i][0].split(" "); r=subs[h[i][0][0]]; if(r==undefined) r=""; if(r=='}}}'||r=='}}') {     st[sp]=bm_gencol(colang); col=st[sp]; sp++; colang+=2.4; //golden angle in radians, approx }   else if(r=='{{{'||r=='{{') {     sp--; col=st[sp]; n=2; }   if(col!="") r=""+r+" "; h[i][0]=r; h[i]=h[i].join(""); } document.getElementById('bm_parseres').innerHTML= " "+h.join("").split("\n").join(" ")+ " <a href='javascript:bm_parsebrackets;'>Parse</a> "; }

function bm_highlight(x,c) { document.getElementById('bm_f'+x+'1').style.backgroundColor=c; document.getElementById('bm_f'+x+'2').style.backgroundColor=c; if(c=="black") window.setTimeout("bm_highlight('"+x+"','transparent')",3000); } // //test: {{{a|b}}} {{a|b}} {{c|{{e|a=b|3=c}}|f}} //test: f g h //test: i {{{{{{{{j}}}}}}}} /* test: {{startofline}}