User:NKohli (WMF)/LST-test.js
Jump to navigation
Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
Documentation for this script can be added at User:NKohli (WMF)/LST-test. |
/**
* 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 + '<section begin="' + title + '" />' + content + '<section end="' + title + '" />\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 = /<section\sbegin=[\s]*(.*?)[\s]*\/>/;
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]*?)<section\send=(.*?)\/>(\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 );