MediaWiki:Bibtex2template.js
From Semantic Portal Wiki
Note - After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh); Konqueror: click Reload or press F5; Opera: clear the cache in Tools → Preferences; Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* customized hook */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
function fn_b2f_main(){
// constants
var allowedEntryTypes= new Array(
'article','book','booklet','confernce','inbook',
'incollection','inproceedings','manual',
'masterthesis','misc','phdthesis',
'proceedings','techreport','unpublished');
var txtBibBegin= new String('\n<!-- Bibtex commented by bibtex2template (begin) \n\n');
var txtBibEnd= new String('\n\n Bibtex commented by bibtex2template (end)-->');
var txtTpBegin= new String('');
var txtTpEnd= new String('<!--\n This template is generated by bibtex2template \n-->');
var txtTpName = new String('i.publication');
// get wiki text from editing area
var text=document.getElementById("wpTextbox1").value;
// Stop if the template is already in wiki text.
if (text.search(txtTpName)!=-1){
return false;
}
//try bibtex type, some of which are not yet supported
for (x in allowedEntryTypes)
{
// use a simple regular expression format for bibtex (not allow '{','}' inside title)
var pattern =new RegExp( "@" + allowedEntryTypes[x] + '[^{]*{(?:[^{}]|{[^{}]*})*}', 'i');
temp = text.match(pattern);
if (null != temp){
text = text.replace(pattern, txtBibBegin+temp+txtBibEnd);
temp = new String(temp);
// trim bibtype
temp = temp.substring(temp.indexOf('\{')+1, temp.length-1);
// add key field
temp = '\n key = ' + temp +'\n';
// TODO: correct wrong bibtex (missing ',' at the end of a bibtex entry)
// this can be detected by temp.match(/=[^=\n,]*\n[^=]*=/g,',');
document.getElementById("wpTextbox1").value += temp ;
var bib_data= new Array();
var counter = new Number(100); // avoid infinit loop
while(counter >0) {
counter = counter -1;
var entry= new Array();
// extract key
var index1 = new Number(0);
index1 = temp.search(/=/)+1;
if (index1==0)
break;
var key = new String();
key = temp.substring(0,index1-2);
key = key.trim();
entry.push( key );
// extract value
var index2 = new Number(0);
index2 = temp.search(/,[^,=]+=/)+1 ;
var value = new String();
if (index2==0){
index2 = temp.length;
}
value = temp.substring(index1,index2) ;
// trim borders
value= value.trim();
value= value.replace(/,$/ ,'');
value= value.replace(/\n/g ,'');
value= value.replace(/\s+/g ,' '); // should be careful
value= value.trim();
value= value.replace(/^{|}$|^"|"$|^'|'$/g ,'');
if (key == 'author')
value = value.replace (/\s*and/g,';');
entry.push( value );
// save key,value
bib_data.push( entry);
// update temp
temp = temp.substring(index2);
};
//render key value pairs to template
var tp = new String();
tp += '{{'+txtTpName+'.'+ allowedEntryTypes[x] ;
tp += '\n';
tp += ' |bibtype = ' + allowedEntryTypes[x] ;
tp += '\n';
for ( kv in bib_data){
tp += ' |'+ bib_data[kv][0] +' = ' +bib_data[kv][1] ;
tp += '\n';
}
tp += '}}';
//update wiki text in editing area
document.getElementById("wpTextbox1").value = txtTpBegin+ tp + txtTpEnd + text;
break; // if we found a match, we don't need to work anymore
}// if
}//for
return false;
}
function fn_b2f_add_button() {
// try to check if toolbar exists (it only show up in edit mode
var toolbar = document.getElementById('toolbar');
if (!toolbar) { return false; }
// create image button
var image = document.createElement("img");
image.width = 23;
image.height = 22;
image.className = "mw-toolbar-editbutton";
//image.src = "http://tw.rpi.edu/images/smwbp/icon_property.png";
image.border = 0;
image.alt = "b2t";
image.title = "convert bibtex to template (i.publication)";
image.style.cursor = "pointer";
image.onclick = fn_b2f_main;
// add image to toolbar
toolbar.appendChild(image);
}
addOnloadHook(fn_b2f_add_button);

