User:NKohli (WMF)/LST-test.js

/** * Easy Labeled Section Transclusion */

function restore_lst{ var editbox = document.getElementById('wpTextbox1'); var search = /##[\s]*(.*?)[\s]*##[\s]*\n/; var a = editbox.value.split(search); var s = a[0]; var m = parseInt(a.length/2); for( var i = 0 ; i < m ; i++ ) { var title = a[i*2+1]; // Ensure we'll don't get twice quote. title = title.replace(/^"(.*)"$/, "$1"); title = title.replace(/^'(.*)'$/, "$1"); var content = a[i*2+2]; if(title && content.substring(0,2)=='{|' ) content = '\n' + content; if(title) s = s + '' + content + '\n'; else s = s + content; /* if( i < m-1 ) s = s + "\n"; */ }	editbox.value = s; }

/** * easy lst : hide section markers */ function easy_lst { var editbox = document.getElementById('wpTextbox1'); if(editbox) {

var search = //; var a = editbox.value.split(search); var s = a[0]; var ok = true; for( var i = 0 ; i < parseInt(a.length/2) ; i++ ) { var title = a[i*2+1]; var content = a[i*2+2]; var r2 = /^([\s\S]*?)(\n|)[\s]*([\s\S]*?)$/; var m2 = content.match(r2); if( m2 ) { title = title.replace(/^"(.*)"$/, "$1"); title = title.replace(/^'(.*)'$/, "$1"); if(s && s.charAt(s.length-1)!='\n' && s.charAt(s.length-1)!='|' ) s = s+'\n'; s = s + "## " + title + " ##\n"+ m2[1] ; if( m2[4] ) { if( m2[4]!='\n' ) { if(s && s.charAt(s.length-1)!='\n' ) s = s+'\n'; s = s + "####\n" + m2[4] ; }				}			} else { ok = false;	/* alert("error"+title); */ }		}		if(ok) { editbox.value = s;		} var saveButton = document.getElementById("wpSave"); var previewButton = document.getElementById("wpPreview"); var diffButton = document.getElementById("wpDiff"); if(saveButton){ saveButton.onclick = restore_lst; previewButton.onclick = restore_lst; diffButton.onclick = restore_lst; }	} }

function easy_lst_setup { if (self.proofreadpage_raw_lst || mw.config.get( 'wgCanonicalNamespace' ) !== 'Page') return;

if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) { mw.loader.using('ext.proofreadpage.page', function {		// mimic code in the extension, there is a conditionnal deps on ext.wikiEditor.toolbar.		if (mw.user.options.get('usebetatoolbar') && $.inArray('ext.wikiEditor', mw.loader.getModuleNames) > -1) {		mw.loader.using('ext.wikiEditor', function { easy_lst; });			} else {				easy_lst;			}		}); } } $( easy_lst_setup );