User:Dave Braunschweig/cleanup.js

var customizeToolbar = function { $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {       'sections': {            'cleanup': {                'type': 'toolbar',                'label': 'Cleanup'            }        }    } );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {       'section': 'cleanup',        'groups': {            'options': {                'label': 'Options'            }        }    } );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {       'section': 'cleanup',        'group': 'options',        'tools': {            'format': {                label: 'Format',                type: 'button',                icon: '//upload.wikimedia.org/wikipedia/commons/9/97/Button_LINKT1.png',                action: {                    type: 'callback',                    execute: function (context) {                        formatTemplates;                    }                }            },

'find': { label: 'Find', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/b/b8/Button_number_0.png', action: { type: 'callback', execute: function (context) { findCoauthors; }               }            },

'fix1': { label: 'Fix 1', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png', action: { type: 'callback', execute: function (context) { processFix1; }               }            },

'fix2': { label: 'Fix 2', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/b/bd/Button_number_2.png', action: { type: 'callback', execute: function (context) { processFix2; }               }            }

// 'fix3': { //    label: 'Fix 3', //    type: 'button', //    icon: '//upload.wikimedia.org/wikipedia/commons/8/80/Button_number_3.png', //    action: { //        type: 'callback', //        execute: function (context) { //            processFix3; //        }            //     }            // }        }    } ); };

var formatTemplates = function { try { var element = document.editform.wpTextbox1; var start = element.selectionStart; var end = element.selectionEnd; var selection = element.value.substring(start, end); var pattern = /\{\{.+?\}\}/g; while(true) { var regex = pattern.exec(selection); if (!regex) { break; }

var template = regex[0]; var fix = template; var index = 0; while(true) { index = fix.indexOf("|", index + 1); if (index < 0) { break; }               while (fix.substr(index - 2, 1) == " ") { fix = fix.substr(0, index - 1) + fix.substr(index); index -= 1; }               if (fix.substr(index - 1, 1) != "\n") { fix = fix.substr(0, index) + "\n" + fix.substr(index); index += 1; }           }

index = fix.indexOf("}"); if (fix.substr(index - 1, 1) != "\n") { fix = fix.substr(0, index) + "\n" + fix.substr(index); }           if (template != fix) { selection = selection.replace(template, fix); }       }        if (element.value.substring(start, end) != selection) { element.value = element.value.substr(0, start) + selection + element.value.substr(end); element.selectionStart = start; element.selectionEnd = start + selection.length; }   }    catch (exception) { console.log(exception); } };

var findCoauthors = function { try { var element = document.editform.wpTextbox1; var text = element.value; var start = element.selectionStart; var end = element.selectionEnd; var pattern = /\| *coauthors *=.*?(\||\}|\n)/; // var pattern2 = /,.*?,|;| and | & /; var search = text.substring(end); while(true) { // find name or break var regex = pattern.exec(search); if (!regex) { document.editform.wpSummary.value = "Category:Pages with citations using unsupported parameters"; document.editform.wpSummary.focus; break; }

// find multiple names or continue var name = regex[0]; var index = end + regex.index; var length = regex[0].length - 1; // var regex2 = pattern2.exec(name); // if (!regex2) { // 	end = index + length; // 	search = text.substring(end); // 	continue; // }

// found scrollTextarea(element, index); element.setSelectionRange(index, index + length); element.focus; break; }   }    catch (exception) { console.log(exception); } };

function scrollTextarea(textarea, offset) { const text = textarea.value; if (offset >= text.length || offset < 0) return; textarea.scrollTop = 0; textarea.value = text.substring(0, offset); const height = textarea.scrollHeight; textarea.value = text; textarea.scrollTop = height - 60; }

var processFix1 = function { try { var element = document.editform.wpTextbox1; var text = element.value; var start = element.selectionStart; var end = element.selectionEnd; var coauthors = text.substring(start, end); var fix = coauthors; fix = fix.replace("coauthors", "author2"); fix = fix.replace(/ and /g, ","); fix = fix.replace(/ & /g, ","); fix = fix.replace(/, *,/g, ","); var count = 2; var index = 0; while (true) { index = fix.indexOf(","); if (index < 0) { break; }           count++; fix = fix.substr(0, index).trim + "\n|author" + count + "=" + fix.substr(index + 1).trim; }

element.value = text.replace(coauthors, fix); element.selectionStart = start; element.selectionEnd = start + fix.length; }   catch (exception) { console.log(exception); } };

var processFix2 = function { try { var element = document.editform.wpTextbox1; var text = element.value; var start = element.selectionStart; var end = element.selectionEnd; var coauthors = text.substring(start, end); var fix = coauthors; fix = fix.replace("coauthors", "author2"); fix = fix.replace(/ and /g, ";"); fix = fix.replace(/ & /g, ";"); fix = fix.replace(/; *;/g, ";"); var count = 2; while (true) { var index = fix.indexOf(";"); if (index < 0) { break; }           count++; fix = fix.substr(0, index).trim + "\n|author" + count + "=" + fix.substr(index + 1).trim; }       element.value = text.replace(coauthors, fix); element.selectionStart = start; element.selectionEnd = start + fix.length; }   catch (exception) { console.log(exception); } };

var processFix3 = function { try { var element = document.editform.wpTextbox1; var text = element.value; var start = element.selectionStart; var end = element.selectionEnd; var author = text.substring(start, end); var fix = author; var count = 1; while (true) { var index = fix.indexOf(";"); if (index < 0) { break; }           count++; fix = fix.substr(0, index).trim + "\n|author" + count + "=" + fix.substr(index + 1).trim; }       element.value = text.replace(author, fix + "\n"); element.selectionStart = start; element.selectionEnd = start + fix.length; }   catch (exception) { console.log(exception); } };

/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */ if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) { mw.loader.using( 'user.options' ).then( function {		// This can be the string "0" if the user disabled the preference (T54542)		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {			$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready ).then( customizeToolbar );		}	} ); }