User:Frietjes/listhelper.js

jQuery(document).ready(function($) { if(mw.config.get('wgNamespaceNumber') != -1) { mw.loader.using(['mediawiki.util']).done( function { var portletlink = mw.util.addPortletLink('p-tb','#','List helper','t-listhelp'); $(portletlink).click(function(e) { e.preventDefault;  listhelper;  }); }); } // // function listhelper {  var myContent = document.getElementById('wpTextbox1').value;

// Iterate over potentially fake lists myContent = myContent.replace(/(=[ ]*\[\^\[\*\]\][ ]*)<(br[^<>]*>[ ]*\/gi, '$1<$2'); var sections = myContent.split(/(===*)/gm);  for( var k=0; k < sections.length; k++) {  	if (sections[k].search(/<[\/ ]*br[\/ ]*>/gi) >= 0) {  	var segments = sections[k].split(/(=(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:•|\{\{.\}\}|<[\/ ]*br[\/ ]*>)(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:\||\}\}))/gim);	  for( var i=0; i < segments.length; i++) {	  	var txt = segments[i];	  	if( txt.search(/^=(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*<[\/ ]*br[\/ ]*>(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:\||\}\})$/im) >= 0) {  			txt = txt.replace(/^(=)((?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*<[\/ ]*br[\/ ]*>(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*)((?:\||\}\}))$/im, '$1{' + '{ubl|$2}' + '}$3'); txt = txt.replace(/<[\/ ]*br[\/ ]*>/gim, '|'); txt = txt.replace(/(\{\{ubl\|)([\t \r\n]+)/gm, '$2$1'); txt = txt.replace(/(\{\{ubl\|(?:\{\{[^{}]*\}\}|[^{}])*)([\t\r\n ][\t\r\n ]*)(\}\})/gm, '$1$3$2'); txt = txt.replace(/(\{\{ubl\|(?:\{\{[^{}]*\}\}|[^{}])*)([\t\r\n ][\t\r\n ]*)(\}\})/gm, '$1$3$2'); var pretext = segments[i-1].replace(/(?:.|[\r\n])*\|/gm, '|'); var res1 = prompt('Replace: ' + pretext + segments[i] + '\nWith: ' + pretext + txt, 'y'); if( res1 == 'y') { segments[i] = txt; }	 	}	  	txt = segments[i]; if( txt.search(/^=(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:•|\{\{.\}\})(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:\||\}\})$/im) >= 0) { txt = txt.replace(/^(=)((?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*(?:•|\{\{.\}\})(?:[^\[\]\{\}\|]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\}|[\r\n])*)((?:\||\}\}))$/im, '$1{' + '{hlist|$2}' + '}$3'); txt = txt.replace(/(?:•|\{\{.\}\})/gim, '|'); txt = txt.replace(/(\{\{hlist\|)([\t \r\n]+)/gm, '$2$1'); txt = txt.replace(/(\{\{hlist\|(?:\{\{[^{}]*\}\}|[^{}])*)([\t\r\n ][\t\r\n ]*)(\}\})/gm, '$1$3$2'); txt = txt.replace(/(\{\{hlist\|(?:\{\{[^{}]*\}\}|[^{}])*)([\t\r\n ][\t\r\n ]*)(\}\})/gm, '$1$3$2'); var pretext2 = segments[i-1].replace(/(?:.|[\r\n])*\|/gm, '|'); var res2 = prompt('Replace: ' + pretext2 + segments[i] + '\nWith: ' + pretext2 + txt, 'y'); if( res2 == 'y') { segments[i] = txt; }	 	}	  }	  sections[k] = segments.join(""); } }	  myContent = sections.join(""); myContent = myContent.replace(/</g, '<'); myContent = myContent.replace(/(\{\{ubl[^{}\r\n]*)([\t\r\n ]+)(\}\})/g, '$1$3$2'); if(document.getElementById('wpTextbox1').value != myContent) { document.getElementById('wpTextbox1').value = myContent; var editsummary = document.getElementById('wpSummary').value + '; List markup per MOS:NOBR'; editsummary = editsummary.replace(/^; /g, ''); document.getElementById('wpSummary').value = editsummary; } } // // });