User:Peteforsyth/Regexp toolbar.js

// /** * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. * @see https://meta.wikimedia.org/wiki/TemplateScript */ $.getScript('//tools.wmflabs.org/meta/scripts/pathoschild.templatescript.js', function {

if ($.inArray(mw.config.get('wgAction'), [ 'edit', 'submit' ]) > -1 ) { //we are editing if ([ 0 ].indexOf( mw.config.get( 'wgNamespaceNumber' ) ) > -1 ){ //main ns           pathoschild.TemplateScript.add([                { name:'Advance header',        script:function($target, TemplateScript){ advanceHeader;} },                 { name:'Convert pages',     script:function($target, TemplateScript){ converttopages;} },            ]); }       if ([ 104 ].indexOf( mw.config.get( 'wgNamespaceNumber' ) ) > -1 ){ //page ns            pathoschild.TemplateScript.add([                { name:'Clean up',              script:function($target, TemplateScript){ cleanup;} },                { name:'Clean up&lines',        script:function($target, TemplateScript){ cleanup_and_lines;} },                { name:'Leading ',       script:function($target, TemplateScript){ leading_nop;} },                { name:'Pline right',           script:function($target, TemplateScript){ pline_r;} },                { name:'Block center mid',      script:function($target, TemplateScript){ blockcentermid;} },                { name:'Strip gaps',            script:function($target, TemplateScript){ strip_gaps;} },                { name:'Sidenotes',             script:function($target, TemplateScript){ sidenotes;} },                { name:'Format header',         script:function($target, TemplateScript){ format_header;} }, { name:'Metadata',             script:function($target, TemplateScript){ add_metadata;} }, { name:'Rh',               script:function($target, TemplateScript){ RunningHeader;} }, { name:'Rh (lookup)',          script:function($target, TemplateScript){ set_running_header;} }, { name:'Scriptstyle',          script:function($target, TemplateScript){ scriptstyle;} }, { name:'Derelative',           script:function($target, TemplateScript){ derelative;} }, { name:'Single returns',       script:function($target, TemplateScript){ singleReturns;} }, { name:' to small',    script:function($target, TemplateScript){ smallcenter;} }, { name:'Convert to blk-c mid', script:function($target, TemplateScript){ blockcenterconvert;} }, { name:'Sidenotes',            script:function($target, TemplateScript){ sidenotes;} }, { name:'Colons',               script:function($target, TemplateScript){ colons;} }, { name:'Poem',                 script:function($target, TemplateScript){ poem;} }, { name:'Clear page',           script:function($target, TemplateScript){ clearpage;} }, { name:'Pipe links',       script:function($target, TemplateScript){ pipelinks;} }, ]);       }        //editing in any ns:        pathoschild.TemplateScript.add([ { name:' to {{c|',         script:function($target, TemplateScript){ convertcenters;} }, { name:'Quotes',                   script:function($target, TemplateScript){ quotes;} }, { name:'Convert redirect',         script:function($target, TemplateScript){ convert_redirect;} }, { name:'Convert ſ to ',      script:function($target, TemplateScript){ convert_long_s;} }, ]);   } }); function set_summary(value){ var summbox = document.getElementsByName('wpSummary')[0]; summbox.value = value } function set_minor(value){ var minor = document.getElementsByName('wpMinoredit')[0]; minor.checked = true } //escape all punctuation!!!!!! // = / < > function strip_gaps{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(//g, ""); set_minor set_summary( "Remove gaps used for paragraph indents, this is better done as a global style."); } function pline_r{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/([0-9]+) *\n/g, "\n"); } function convertcenters{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/ ([^<]*)<\/center>/g, "$1\n"); } function smallcenter { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/ ([^<]*)<\/center>\n/g, ''); } function sidenotes{ var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; headerbox.value = headerbox.value + '\n'; var footerbox = document.getElementsByName('wpFooterTextbox')[0]; footerbox.value = '\n' +footerbox.value; } function leading_nop { editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = "\n\n" + editbox.value } function format_header { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/{{ *[Hh]eader2/,     "{{header"); editbox.value = editbox.value.replace(/( *\| *title *= *)/,     " | title      = "); editbox.value = editbox.value.replace(/( *\| *author *= *)/,    " | author     = "); editbox.value = editbox.value.replace(/( *\| *translator *= *)/, " | translator = "); editbox.value = editbox.value.replace(/( *\| *section *= *)/,   " | section    = "); editbox.value = editbox.value.replace(/( *\| *previous *= *)/,  " | previous   = "); editbox.value = editbox.value.replace(/( *\| *next *= *)/,      " | next       = "); editbox.value = editbox.value.replace(/( *\| *year *= *)/,      " | year       = "); editbox.value = editbox.value.replace(/( *\| *notes *= *)/,     " | notes      = "); editbox.value = editbox.value.replace(/( *\| *portal *= *)/,    " | portal     = "); editbox.value = editbox.value.replace(/( *\| *wikipedia *= *)/, " | wikipedia  = "); editbox.value = editbox.value.replace(/( *\| *commonscat *= *)/, " | commonscat = "); } function convert_long_s { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/ſ/g, ''); } function scriptstyle { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/\(.*)\<\/math\>/g, '\\\scriptstyle\{$1\}\<\/math\>'); } function colons { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/::/g, ''); } function poem { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = ' ' + editbox.value + ' '; } function derelative { var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/\[\[\/(.*)\/\]\]/g, '\[\[$1\|$1\]\]'); } function singleReturns { var editbox = document.getElementsByName('wpTextbox1')[0]; // remove single line breaks; preserve multiple. editbox.value = editbox.value.replace(/([^>\n])\n([^<\n])/g, '$1 $2'); //linux editbox.value = editbox.value.replace(/([^>\r\n])\r\n([^<\r\n])/g, '$1 $2'); //windows } function quotes{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/[\u201C\u201D]/g, '\x22'); //double smart quotes editbox.value = editbox.value.replace(/[\u2018\u2019\x60]/g, '\x27'); //curly single quotes and backticks editbox.value = editbox.value.replace(/&quot;/g, '\x22'); //curly single quotes and backticks } function blockcenterconvert{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/{{([Ffloat|[Bb]lock) center\|([\s\S]*)}}/g, '$2'); //remove block centres in the main block blockcentermid //add split BC in the header/footer } function blockcentermid{ var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; headerbox.value = headerbox.value + '\n{{block center/s}}'; var footerbox = document.getElementsByName('wpFooterTextbox')[0]; footerbox.value = '{{block center/e}}\n' +footerbox.value; } function sidenotes{ var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; headerbox.value = headerbox.value + '\n'; var footerbox = document.getElementsByName('wpFooterTextbox')[0]; footerbox.value = '\n' +footerbox.value; } function cleanup { quotes var editbox = document.getElementsByName('wpTextbox1')[0]; // remove trailing whitespace at the end of each line editbox.value = editbox.value.replace(/ \n/g, '\n'); // remove trailing whitespace at the end of input editbox.value = editbox.value.replace(/\s+$/g, ''); // convert double-hyphen to mdash editbox.value = editbox.value.replace(/([^\!])--([^>])/g, '$1—$2'); // remove spacing around mdash, but only if it has spaces on both sides // (we don't want to remove the trailing space from "...as follows:— "   editbox.value = editbox.value.replace(/ +— +/g, '—');    // join words that are hyphenated across a line break    editbox.value = editbox.value.replace(/['-]\n/g, '');    // " word -> "word, only if preceded by a space.    editbox.value = editbox.value.replace(/ \x22 /g, ' \x22');    // remove unwanted spaces around punctuation marks    editbox.value = editbox.value.replace(/ ([;:\?!,])/g, '$1');    // : — to :—    editbox.value = editbox.value.replace(/: +—/g, ':—');    //OCR fixes    // convert i9 to 19, etc.    editbox.value = editbox.value.replace(/[il]([0-9])/g, '1$1');    // "the", "them", "their", "that", "than" etcetera    editbox.value = editbox.value.replace(/t[fl]i(a|e)/g, 'th$1');    // "liere", "liave"    editbox.value = editbox.value.replace(/(\s)li(ave|ere)(\s)/g, '$1h$2$3');    // wli -> wh    editbox.value = editbox.value.replace(/(\s)wli/g, '$1wh'); editbox.value = editbox.value.replace(/(\s)whicli/g, '$1which'); // "aud" to "and" editbox.value = editbox.value.replace(/(\s)aud(\s)/g, '$1and$2'); // "v/" to "w" editbox.value = editbox.value.replace(/v\//g, 'w'); // "ét" to "ct" (ligature causes this) editbox.value = editbox.value.replace(/ét/g, 'ct'); // "^V", "AV", "\\" to "W" editbox.value = editbox.value.replace(/\^V/g, 'W'); editbox.value = editbox.value.replace(/\AV[a-z]/g, 'W'); editbox.value = editbox.value.replace(/\\\\/g, 'W'); editbox.value = editbox.value.replace(/\[Il]\^/g, 'L'); // remove "�" editbox.value = editbox.value.replace(/�/g, ''); // "U" -> "ll" when preceded by a lowercase letter. editbox.value = editbox.value.replace(/([a-z])U/g, '$1ll'); // l)e -> be   editbox.value = editbox.value.replace(/(\s)l\)e(\s)/g, '$1be$2'); // Jn -> In   editbox.value = editbox.value.replace(/(\s)Jn(\s)/g, '$1In$2'); } function cleanup_and_lines{ var editbox = document.getElementsByName('wpTextbox1')[0]; cleanup // remove single line breaks; preserve multiple. editbox.value = editbox.value.replace(/([^>\n])\n([^<\n])/g, '$1 $2'); //linux editbox.value = editbox.value.replace(/([^>\r\n])\r\n([^<\r\n])/g, '$1 $2'); //windows // collapse sequences of spaces into a single space editbox.value = editbox.value.replace(/ +/g, ' '); } // RunningHeader - puts {{RunningHeader}} in to headerbox function RunningHeader { var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; headerbox.value = headerbox.value + '\n{{RunningHeader|||}}'; } function convert_redirect{ var editbox = document.getElementsByName('wpTextbox1')[0]; editbox.value = editbox.value.replace(/#REDIRECT\s*(\[\^\*\]\])/g, '{{substXXXXX:dated soft redirect|$1}}'); //prevent subst firing when saving this JS       editbox.value = editbox.value.replace('substXXXXX', 'subst'); var summbox = document.getElementsByName('wpSummary')[0]; summbox.value = 'Replaced hard subpage redirect with soft redirect.'; var mineditbox = document.getElementsByName('wpMinoredit')[0]; mineditbox.value = 0; } function advanceHeader{ var editbox = document.getElementsByName('wpTextbox1')[0]; var m = editbox.value.match(/ *\\| *section *= *(.*?)[, ]*((?:by .*)?)\n/) ; var prev = (m == null)? '': m[1]; var auth = (m == null)? '': m[2]; var m = editbox.value.match(/ *\\| *next *= *(.*)/) ; var section = (m == null)? '': m[1]; section = section.replace(/\[\[\.\.\/(.*)\/\]\]/, "$1");        //strip relative link var next = '..//'; editbox.value = editbox.value.replace(/( *\\| *section *= *)(.*)/, "$1" + section +', '+ auth); editbox.value = editbox.value.replace(/( *\\| *next *= *)(.*)/,  "$1" + next ); editbox.value = editbox.value.replace(/( *\\| *previous *= *)(.*)/, "$1../" + prev + "/"); //advance the pages tag to the next page var m = editbox.value.match(/\<pages *index="[^"]*" *from=([0-9]+) *to=([0-9]+)/) ;    if (m != null){        editbox.value = editbox.value.replace(/(\'); } function clearpage{ document.getElementsByName('wpTextbox1')[0].value = '' document.getElementsByName('wpFooterTextbox')[0].value = '' document.getElementsByName('wpHeaderTextbox')[0].value = '' $('.quality0 > input').prop('checked', true); //without text } //