User:Gerbrant/wikiedit.js

//Dit minimaliseert de kans op conflicten met andere scripts. var wikiedit = new Object; /*

H T M L   ->    W I K I

============================

wikiedit.Html2Wiki = function(node) {	return '\n\n\nLET OP! SLA JE RESULTATEN /N I E T/ OP! SAVE DOET HET NOG NIET.'; } /*

W I K I   ->    H T M L

============================

wikiedit.Wiki2Html = function(wiki) {	String.prototype.parse = function(sb, re, se, fi, fo) {		var a = wikiedit.AtomLevel++, i = 0, t = new Object, r = this, l = '', m;		while(m = r.match(re)) {			l += r.slice(0, m.index) + 'aToM[' + a + ':' + i + ']'; //		status = sb + m[1] + se; t[i++] = fi(m[1]); r = r.slice(m.index+m[0].length); }		r = fo(l + r); l = i;		for(i = 0; i < l; i++) r = r.replace('aToM[' + a + ':' + i + ']',				(sb + t[i] + se).replace('$', '$$')); wikiedit.AtomLevel = a;		return r;	} wikiedit.AtomLevel = 0; return wikiedit.sNoWiki(wiki); } /*function dumpmatch(s, re) {	var i, j, m = s.match(re), t = '[ ', mi; for(i = 0; i < m.length; i++) {		t += '"'; mi = m[i];		for(j = 0; j < mi.length; j++)			if(mi.charCodeAt(j) < 32)				t += '.' + mi.charCodeAt(j) + '.';			else				t += mi.charAt(j);		t += '" '; }	alert(t + ']'); }*/ wikiedit.sNoWiki = function(s) {	return s.parse(' &lt;nowiki&gt;',		/<\156owiki>((?:.|\n)*?)<\/nowiki>/, '&lt;/nowiki&gt; ',		wikiedit.sSanatize, wikiedit.sComment); } wikiedit.sSanatize = function(s) {	return s.replace(//g, '&gt;'); } wikiedit.sComment = function(s) {	return s.parse(' &lt;!--',		//, '--&gt; ',		wikiedit.sBlocks, wikiedit.sBlocks); } wikiedit.sBlocks = function(s) {	return wikiedit.sBlocksEx(s, false, true); } //NOOT: ik emuleer een bizarre bug in de wikisoftware -- ':' / ';' //NOOT: FirstTableOK geeft aan of tables etc. mogen op de eerste regel. wikiedit.sBlocksEx = function(r, NoSinglePara, FirstTableOK) {	var p, b, pBuf = , l = , m, ml, bp = wikiedit.sTemplate, fl = function(a, b)		{ if(b.length) return l + ' ' + bp(b) + ' '; else return l;		}, ab = function(a, b)		{ if(a == '') return b;			else return a + ' ' + b;		}, al = function(a, f)		{ var ha = , ep = , i;			for(i = 0; i < a.length; i++) {				switch(a.charAt(i)) {				case ';': ha += ''; ep += ';'; break; case ':': ha += ''; ep += ':'; break; case '*': ha += ''; ep += '*'; break; case '#': ha += ''; ep += '#'; break; default: return [ep, ha + f(a.slice(i))]; }			}			return [ep, ha]; },		prefix = '', o_prefix, n_prefix; //O_P en N_P deel van LELIJKE HACK if(!FirstTableOK) {		p = r.indexOf('\n'); if(p < 0) {			b = r;			r = ''; }		else {			b = r.slice(0, p); r = r.slice(++p); }		pBuf = b.match(/^\s*((?:\S(?:.*\S)?)?)\s*$/)[1]; }	while(true) {		p = r.indexOf('\n'); if(p < 0) if(r == '') break; else b = r;		else b = r.slice(0, p); b = b.match(/^((?:.*\S)?)\s*$/)[1]; //		status = 'BLOK: ' + b;		//LELIJKE HACK o_prefix = prefix.replace(/;/g, ':'); n_prefix = b.slice(0, prefix.length).replace(/;/g, ':'); ml = o_prefix.length; while(o_prefix != n_prefix) { //alert(prefix + '\t' + b+ '\n' +o_prefix +'\t'+ n_prefix); // v- moet prefix blijven switch(prefix.charAt(--ml)) {			case ';': l += ''; break; case ':': l += ''; break; case '*': l += ''; break; case '#': l += '</ol>'; }			o_prefix = o_prefix.slice(0, ml); n_prefix = n_prefix.slice(0, ml); }		prefix = b.slice(0, o_prefix.length); //GEEN ml DUS if(prefix == '') switch(b.charAt(0)) {			case '': l = fl(l, pBuf); pBuf = ''; break; case '=': m = b.match(/(=*)(.*)\1/); if(m) {					ml = m[1].length; l = fl(l, pBuf) + '<h' + ml + '>' + bp(m[2]) + '</h' + ml + '>'; pBuf = ''; }				else pBuf = ab(pBuf, b); break; case '-': if(b.match(/-{4,}/)) {					l = fl(l, pBuf) + ' '; pBuf = ''; }				else pBuf = ab(pBuf, b); break; case ';': //Doe nog speciaal werk voor de rare 1-regel syntaxis. ml = b.indexOf(' :'); if(ml > 0 && ml < p)				{ b = b.slice(0, ml); p = ml; }			case ':': case '*': case '#': b = al(b, bp); prefix += b[0]; l = fl(l, pBuf) + b[1]; pBuf = ''; break; case ' ': l = fl(l, pBuf) + ' ' + b.slice(1); pBuf = ''; if(p >= 0) while(r.charAt(p + 1) == ' ') {						r = r.slice(++p); p = r.indexOf('\n'); if(p < 0) if(r == '') break; else b = r;						else b = r.slice(0, p); l += '\n' + b.match(/^ ((?:.*\S)?)\s*$/)[1]; }				l += ' '; break; case '{': if(FirstTableOK && b.charAt(1) == '|') {					b = wikiedit.sTable(b.slice(2), r)					l = fl(l, pBuf) + b[0]; pBuf = ''; r = b[1]; p = 1; break; }			default: pBuf = ab(pBuf, b); }		else if(prefix != b) //Lege items zijn slecht! switch((b = b.slice(prefix.length))				.charAt(0)) {			case ';': //Doe nog speciaal werk voor de rare 1-regel syntaxis. ml = b.indexOf(' :'); if(ml > 0 && ml < p)				{ b = b.slice(0, ml); p = ml + prefix.length; }			case ':': case '*': case '#': b = al(b, bp); prefix += b[0]; l += b[1]; break; default: switch(o_prefix.charAt(n_prefix.length - 1)) {				case ';': m = '</dt>'; break; case ':': m = '</dd>'; break; default: l += '</li><li>' + bp(b); m = false; }				if(m) {					switch(prefix.charAt(n_prefix.length - 1)) {					case ';': m += '<dt>'; //Doe nog speciaal werk //voor de rare 1-regel syntaxis. ml = b.indexOf(' :'); if(ml > 0 && ml < p)						{ b = b.slice(0, ml); p = ml + prefix.length; }						break; case ':': m += '<dd>'; }					l += m + bp(b); }			}		if(p < 0) break; r = r.slice(++p); //		FirstTableOK = true; } //Bloklus if(l == '') if(NoSinglePara) return /*'BUF:' + */bp(pBuf); else return /*'PARBUF: '*/' ' + bp(pBuf) + ' '; else return /*'FLUSH:' + */fl(l, pBuf); } wikiedit.sTable = function(params, r) { var l = ' ', r]; //.slice(2) eruit gehackt default: h = false; switch(r.charAt(0)) {		case '!': h = true; case '|': if(!inRow) {				l += ' '; inRow = true; }			q = r.indexOf('|'); if(q < 0 || q > p)			{ if(h) l += ' '; else l += ' '; r = r.slice(1); }			else {				if(h) l += '<th '; else l += '<td '; l += r.slice(1, q) + '>'; r = r.slice(++q); }			q = wikiedit.sGetCell(r, h); p = q[1]; //alert(q[0]); l += wikiedit.sBlocksEx(q[0], true, false); //if(q[0].indexOf('aToM[0:12]')>=0) alert(r.slice(0,100)+'\n\n\n'+r.slice(p+1,p+101)); if(h) l += ' '; else l += ' '; }}		if(p < 0) break; r = r.slice(++p); } //Tabelinhoudlus if(inRow) l += ' '; return [l + ' ', '']; } wikiedit.sGetCell = function(r, h) { var p = r.indexOf(h ? '!!' : '||'), q = r.indexOf('\n'), n = 0; if(p < 0 || p > q) /* cel eindigt bij volgende regel die begint met eem ! of |: \n! \n|	\n|-	\n|+	\n|} maar geneste tabellen slaan we over: \n{|....\n|} if(q < 0) return [r, r.length]; else while(true) {				switch(r.charAt(q + 1)) {				case '!': case '|': if(n == 0) return [r.slice(0, q), q]; if(r.charAt(q + 2) == '}') n--; break; case '{': if(r.charAt(q + 2) == '|') n++; break; case '': return [r, r.length]; }				q = r.indexOf('\n', q + 1); }	else return [r.slice(0, p), p]; } wikiedit.sTemplate = function(s) {	return s.parse('<span wikiedit=template style=color:navy;\		background-color=#e0e0f0> ',		wikiedit.sTemplateBody, wikiedit.sLink); }	wikiedit.sTemplateBody = function(s) {		var p = s.indexOf('|'); if(p < 0) return s + '">' + s + '</a>';		else		{			var l = s.slice(0, p);			return l + '">' + l + '</a>|' + wikiedit.sLink(s.slice(p+1)); }	} wikiedit.sLink = function(s) {	return s.parse('<a href="/wiki/', /\[\[(.*?)\]\]/, '</a>',		wikiedit.sLinkBody, wikiedit.sBold); }	wikiedit.sLinkBody = function(s)	{		var p = s.indexOf('|');		if(p < 0)			return s + '">' + s;		else {			return s.slice(0, p) + '">' +				wikiedit.sBold(s.slice(p+1));		}	} wikiedit.sBold = function(s) {	return s.parse(, /((?:.|\n)*?)/, ,		wikiedit.sItalics, wikiedit.sItalics); } wikiedit.sItalics = function(s) {	return s.parse(', /((?:.|\n)*?)/, ',		wikiedit.sQuote, wikiedit.sQuote); } wikiedit.sQuote = function(s) {	return s.replace(/\r/g, 'CR')		.replace(/\n/g, 'LF '); } /*

-

G E B R U I K E R S I N T E R F A C E

=========================================

wikiedit.OK = function {	wikiPreview.style.display = 'none'; toolbar.style.display = ''; with(wikiedit.PlainTextBox) {		innerText = wikiedit.Html2Wiki(wikiPreview); style.display = ''; } } wikiedit.Cancel = function {	wikiPreview.style.display = 'none'; toolbar.style.display = ''; wikiedit.PlainTextBox.style.display = ''; } /*       --	WERKBALKEN ========== wikiedit.Bool2Dis = function(b){return b?'':'none';} wikiedit.OpenToolbar = function {	wikiedit.Toolbars[this.wikieditTB][0].style.display = wikiedit.Bool2Dis(true); this.style.display = wikiedit.Bool2Dis(false); } wikiedit.CloseToolbar = function {	wikiedit.Toolbars[this.wikieditTB][1].style.display = wikiedit.Bool2Dis(true); this.parentElement.style.display = wikiedit.Bool2Dis(false); } wikiedit.Toolbar = function(xname, bright, pastel, visible, buttons) {	var n = this.ToolbarCount++; var t = document.createElement('DIV'); var i, b;	with(t) {		with(style) {			display = this.Bool2Dis(visible); backgroundColor = pastel; borderWidth = '1px'; borderStyle = 'solid'; borderColor = bright; margin = '2px'; }	}	var c = document.createElement('BUTTON'); c.wikieditTB = n;	with(c) {		onclick = this.CloseToolbar; with(style) {			width = '64px'; height = '32px'; color = bright; marginRight = '32px'; }		appendChild(document.createTextNode(xname)); }	t.appendChild(c); for(i = 0; i < buttons.length; i+=2) {		b = document.createElement('BUTTON'); with(b) {			onclick = buttons[i+1]; with(style) {				width = '64px'; height = '32px'; color = bright; marginRight = '4px'; }			appendChild(document.createTextNode(buttons[i])); }		t.appendChild(b); }	var o = document.createElement('BUTTON'); o.wikieditTB = n;	with(o) {		onclick = this.OpenToolbar; with(style) {			display = this.Bool2Dis(!visible); width = '128px'; //pixels are bad!!!!! do st about this height = '32px'; color = bright; }		appendChild(document.createTextNode(xname)); }	this.Toolbars[n] = [t, o]; this.FoldToolbar.appendChild(o) return t; } wikiedit.CreateToolbars = function {	var ToolbarDiv = document.createElement('DIV'); this.FoldToolbar = document.createElement('DIV'); this.Toolbars = new Object; this.ToolbarCount = 0; with(ToolbarDiv) {		appendChild(this.Toolbar('Algemeen', 'maroon', '#f0e0e0', true, ['OK', this.OK, 'Annuleren', this.Cancel])); appendChild(this.Toolbar('Opmaak', 'green',  '#e0f0e0', true, []));		appendChild(this.Toolbar('Tabellen', 'navy',  '#e0e0f0', false, []));		appendChild(this.FoldToolbar); }	return ToolbarDiv; } wikiedit.CreateHtmlBox = function(boxheight, boxtext) {	var HtmlBox = document.createElement('DIV'); with(HtmlBox) {		with(style) {			pixelHeight = boxheight; width = '100%'; overflow = 'scroll'; }		contentEditable = true; innerHTML = wikiedit.Wiki2Html(boxtext); }	return HtmlBox; } wikiedit.init = function {	try{if(toolbar==undefined)return;}catch(e){return;} this.PlainTextBox = document.getElementsByName('wpTextbox1')[0]; with(this.PlainTextBox) {		this.HtmlBox = this.CreateHtmlBox(offsetHeight, value); style.display = 'none'; }	toolbar.style.display = 'none'; with(wikiPreview) {		appendChild(this.CreateToolbars); appendChild(this.HtmlBox); } } wikiedit.CheckReadyState = function {	try {		wikiedit.onreadystatechange; }	catch(e) {	}	if(document.readyState == 'complete') {		document.onreadystatechange = wikiedit.onreadystatechange; wikiedit.init; } } wikiedit.onreadystatechange = document.onreadystatechange; document.onreadystatechange = wikiedit.CheckReadyState;