User:M~enwiki/dereference.js

/* importScript('User:M/jquery.js'); importScript('User:M/dereference.js');

/*! selectText jQuery plugin by Steve Clay http://code.google.com/p/mrclay/source/browse/trunk */ /* * Simple plugin to select a range of text within a text box/textarea */ (function{function selectText(el,begin,end){var len=el.value.length;end=end||len;if(begin==null) el.select;else if(el.setSelectionRange) el.setSelectionRange(begin,end);else if(el.createTextRange){var tr=el.createTextRange,c="character";tr.moveStart(c,begin);tr.moveEnd(c,end-len);tr.select;} else el.select;el.focus;} jQuery.fn.selectText=function(begin,end){this.length&&selectText(this.get(0),begin,end);return this;};});

// Adds a link below the page heading which replaces references of the following form: // //  // with this form: // [1][2] //  [3] // The refs are re-inserted upon submit (unless these numbers are deleted). // // TODO: // * Dump the refs into a list, where clicking each opens a textarea with its removed ref data. // * Timer to check for new number-refs, add them to list, allow creation. Check duplicates [12][12], highlight removed for re-insertion. // * Handle slashes in: ref name="aa/bb". // * Entry form for template refs instead of textarea, make ref list look nicer. // * More efficient, if problems arise. // // "...that removes all refs into an a div below the edit textarea, replacing them with numbers[439]. // If you mess with the number, the ref-down-there turns red. Click the button to inject another ref // below using reftools, and a number inline. Parsing/replacing the refs wouldn't be very difficult, // and then add a timer that makes sure the numbers are all still there in the text. Offer little 'add // this ref' for any new numbers (so right now I'd see '439: [click to add ref]' somewhere below)." // $(window).load( addRefTab ); function addRefTab { if (window.location.href.indexOf("action=") == -1) return; $("  ").insertAfter($("#wpTextbox1")); $("References ").insertAfter($("#wpTextbox1")); $("#refbutton").click(function {		$("#refbox").slideDown( 500 );		$(this).fadeTo(100,.01).slideUp(100);		hideRefs; 	}).mouseenter(function{$(this).css("color","#900").css("background","#fafafa");	}).mouseleave(function{$(this).css("color","black").css("background","#fafafa");	}); $("#wpTextbox1").width($("#refbutton").width); } var refMap = new Array; function hideRefs {

var t1 = new Date.getTime if (window.location.href.indexOf("action=") == -1) return; var text = $("#wpTextbox1").val; //var regex = /)>]*(\/>|>(?:(?! y4bo4gche5h)[\s\S])*?<\/ref>)/; // also absorbs comments to the sides of the ref var regex = /[\s\S])*?-->[ ]*)*)>]*(\/>|>(?:(?! y4bo4gche5h)[\s\S])*?<\/ref>)([ ]*)[\s\S])*?-->)*/ var curRefNum = 0; var num=0;

$("#refbox").append(" References: "); $("#refbox").append($("  "));

var t2 = new Date.getTime

for { curRefNum++; var match = text.match(regex); if (match==null) break; var startR = match.index; var endR = match[0].length + startR; var rText = text.substring(startR,endR); while(text.indexOf("["+curRefNum+"]")>=0) { curRefNum++; }		text = text.substring(0,startR) + "["+curRefNum+"]" + text.substring(endR,text.length); refMap[""+curRefNum] = rText;

$("#reflist").append(		$("")			.text(curRefNum+", ")			.mouseenter(function{ $(this).css("background","red"); var refnum = $(this).attr("id").substr(4); $("#refcontent").val(refMap[refnum]); var loc = $("#wpTextbox1").val.indexOf("["+refnum+"]"); $("#wpTextbox1").selectText(loc,loc+2+refnum.length); }).mouseleave(function{ $(this).css("background","none"); $("#refcontent").val(""); })		);

num++; }	var t3 = new Date.getTime alert(t3-t2); $("#wpTextbox1").text(text);

$("#refclean").text("DeReferenced "+num+" references. They will be added back upon save/preview/changes, unless [2][56] etc. are removed."); $("#editform").submit(function {		$("#refclean").text("Rereferencing...");		var text2 = $("#wpTextbox1").val;		for (var key in refMap) {			//TODO: notify missing refs.			text2 = text2.split("["+key+"]").join(refMap[key]);		}		$("#wpTextbox1").text(text2);		$("#refclean").text("Rereferenced");		return true;	}); }

// Adds a link below the page heading which replaces references of the following form: // //  // with this form: // [1][2] //  [3] // The refs are re-inserted upon submit (unless these numbers are deleted). // //                                                       //                                                        //