User:Czar/cite.js

/* to do - detect without |refs=, throw error when no reflist detected - detect when there are no changes and (1) alert, (2) ensure there are no spacing changes to article, (3) do not give an edit summary - pull current version of text box when on edit page - automate into preview when on view page - secondary functions: alphabetize LDR; detect dmy/mdy; strip back to bare URLs and re-run citoid */

console.log("imported"); var citation;

var typeMap = { blogPost:"Cite web", webpage:"Cite web", };

function printWikiParam(param, contents) { if (contents) { citation += " |" + param + "=" + contents; } }

// function just for me: if a work/journal has a specific title, add a specific Wikilink function addPipedWikilink(title, titleWikilink, json) { if ( (json.journal == title) || (json.work == title) ) { if (json.journal == title) json.journal = ""+title+""; if (json.work == title) json.work = ""+title+""; } }

//Input JSON, receive Wikipedia citation style (CS1 in braces) – SEE WRAPPER BELOW function citationGuts(json) { // Maybe in the future I'll build an object in a specific order and just have it print out, but why bother now // ["chapter", "title", "work", "journal", "volume", "issue", "pages", "date", "year", "url", "accessdate", "doi", "isbn", "issn"]; citation = ""; //	console.log(citation); return citation; }

function citationWrapper (json) { // SCRUBBING // replace double quotes in title with single quotes (since the quotes are embedded) json.title = json.title.replace(/[“”"]/g, '\); 	// convert websiteTitle to work	json.work = json.websiteTitle;	// remove API junk from URL	json.url = json.url.replace(/\?jsoncallback=\?/, );	// Build short and long ref names	var longestWordInTitle = json.title.split(' ').reduce(function (x,y) { return x.length > y.length ? x : y; }).replace(/[^A-Za-z0-9\s]/g,'');		shortref = ' ',		longref = ' ',		returnArray = [ longref, shortref ]	console.log(returnArray);	return returnArray; }

function czarcite { // Set this to whatever edit is being parsed var rawText = $('#wpTextbox1').text; // wiki editor textbox returns blank if empty var json; // this is on the outside so we can grab it from the console later var listDefinedRefs = new Array; var	cleanedText = rawText.replace(/ (http[^<]*)<\/ref>/g, // regex for converting bullet points: http://regexr.com/3dtk4			function citoid (match, plainURL) {				var activeCitation = new Array;				console.log(plainURL);				$.ajax({ url: "https://citoid.wikimedia.org/api?format=mediawiki&search=" + plainURL + "?jsoncallback=?", contentType: "application/json", async: false, complete: function (data) { json = data.responseJSON[0]; activeCitation = citationWrapper(json); console.log("activeCitation: "+activeCitation[1]); listDefinedRefs.push(activeCitation[0]); // push longref to list-defined refs array if (typeof activeCitation[1] == "string") { match = activeCitation[1]; // return shortref for in-text footnote } else { return match; }					}				});				return match;			}		); //console.log(cleanedText); //$('#wpTextbox1').html(cleanedText); listDefinedRefs = listDefinedRefs.sort;

//add LDR cleanedText = cleanedText.replace(/({{[Rr]eflist[^}]*\|\s*refs=)/, function alksdjfaslkhjdf (match, capture1) { //http://regexr.com/3dm33		var additions = '\n';		for (i=0; i<listDefinedRefs.length; i++) {		console.log(listDefinedRefs[i]);			additions += '\n' + listDefinedRefs[i] + '\n';		}		return capture1 + additions + '\n';	}); //console.log(cleanedText); $('#wpTextbox1').text(cleanedText); $('#wpSummary').val('bare URLs expanded with czar\'s jury-rigged citation expander'); }

function addPortletLink { // Adds "czarcite" portlet to WP interface sidebar $(mw.util.addPortletLink('p-tb', '#', 'czarcite', 'czarcite', 'Perform citation magic' )).click(function{		czarcite;		return false;	}); }

mw.loader.using('mediawiki.util', function{ // when page finishes loading, add a portlet to the WP interface sidebar	$(document).ready( mw.util.addPortletLink ); });