/* to do
- detect {{reflist}} 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 = "[[" titleWikilink "|" title "]]";
if (json.work == title) json.work = "[[" titleWikilink "|" 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 = "{{" typeMap[json.itemType];
//Authors
if (json.author) {
for (var i=0; i<json.author.length; i ) {
//var currentCreator = json.author[i];
//var currentCreatorType = ZU.getLocalizedCreatorType(currentCreator.creatorType);
if (json.author[i].length == 2) { //If the array item has two parts, meaning a first and last name
citation = ' |last=' json.author[i][1] ' |first=' json.author[i][0];
} else {
citation = ' |author=' json.author[i][0];
}
}
}
// My special processing for adding wikilinks
if (json.work) {
var linkAnyOfThese = ["Booklist","Library Journal","Publishers Weekly","Kirkus Reviews",
"The New York Times","The Washington Post","The Huffington Post","Financial Times",
"National Review","Moyers & Company","American Historical Review",
"The Times Higher Education Supplement",
"Journal of the History of Ideas","New Formations","Commonweal",
"The Times Literary Supplement","Public Administration Review","London Review of Books",
"Journal of American History","Boston Review","The Hedgehog Review","History News Network",
"The New Republic","Bookforum","Contemporary Sociology","Ars Technica","IGN","PC Gamer",
"Digital Spy","Rock, Paper, Shotgun","Metacritic","GameSpot","GameZone","VG247","IGN",
"Kill Screen","Game Informer","GamesRadar","USgamer","Eurogamer","Yahoo Finance",
"The Verge","Popular Science","Bloomberg.com","Refinery29","Gamasutra","VideoGamer.com",
"Engadget","Digital Trends","Vice Motherboard","Destructoid","Hardcore Gamer"];
for (var i=0; i<linkAnyOfThese.length; i ) {
if (json.work == linkAnyOfThese[i]) json.work = "[[" linkAnyOfThese[i] "]]";
}
// also for me, see function above
addPipedWikilink("Dissent", "Dissent (American magazine)", json);
addPipedWikilink("Wired", "Wired (magazine)", json);
addPipedWikilink("Polygon", "Polygon (website)", json);
}
printWikiParam("title",json.title);
printWikiParam("work",json.work);
printWikiParam("date",json.date);
printWikiParam("url",json.url);
printWikiParam("accessdate",json.accessDate);
//Finalize
citation = " |df=mdy-all }}";
// 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 = '<ref name=\"' json.work ': ' longestWordInTitle '\"\/>',
longref = '<ref name=\"' json.work ': ' longestWordInTitle '\">' citationGuts(json) '</ref>',
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(/<ref>(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 [[user:czar|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()
);
});