User:Pathoschild/Scripts/Regex menu framework

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Mrjyn (talk | contribs) at 05:37, 30 August 2010 (added name for regex sidebar list). It may differ significantly from the current version.

The regex menu framework is a JavaScript tool that creates a sidebar menu of user-defined regex tools. Each regex tool is scripted using simplified functions that can perform regex search and replace, change the edit summary, set the edit options (such as watching the page), and perform an action like previewing the page. The default package below also contains a "Custom regex" script for single-use dynamic regex (screenshot).

Writing subscripts requires some basic knowledge of JavaScript, and using the "Custom regex" requires a basic knowledge of regex.

Installation

  1. Add your name to the user list below to get code updates. (If you're not on the list, the script might unexpectedly stop working for you one day if there's a major change.)
  2. Add the following code to User:your user name/common.js. To enable it for all users on a wiki, add it to MediaWiki:Common.js instead.
  3. Refresh your browser to reload the JavaScript. (In Chrome or Firefox, press [CTRL] and [R] at the same time.)
/*************
*** Regex menu framework
*** by [[m:user:Pathoschild]] <http://meta.wikimedia.org/wiki/User:Pathoschild/Scripts/Regex_menu_framework>
***	- adds a sidebar menu of user-defined scripts.
*************/
importScriptURI('http://meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/Regex_menu_framework.js&action=raw&ctype=text/javascript');

/* menu links */
// In the function below, add more lines like "regexTool('link text','function_name()')" to add
// links to the sidebar menu. The function name is the function defined in rfmscripts() below.
function rmflinks() {
	regexTool('Custom regex','custom()'); // a default tool which performs regex input in a dynamic form
}
	
/* scripts */
// Below, define the functions linked to from rmflinks() above. These functions can use any JavaScript,
// but there is a set of simplified tools documented at
// http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Regex_menu_framework .

Usage

Dynamic input form

Screenshot of the 'Custom regex' form'

The framework contains a default tool listed as "Custom regex". Clicking the link generates an input form above the edit box as shown at right, which allows the user to input any number of search & replace patterns.

All text in the search boxes is interpreted as JavaScript regex. Users can enter simple patterns (like "apple" or "colou?r"), or define modifiers using the JavaScript regex syntax (like "/apple/ig").

Text in the replacement boxes is interpreted as simple text, with the exception of referencing syntax ($1 through $9).

Adding existing tools

It is easy to add tools written for the framework (listed below). You must add two blocks of code for each script: the menu item, and the script itself.

  1. Under "/* menu links */", add the "regexTool()" code (instructions are in that section).
  2. Under "/* scripts */", add the script function. This may be code like "function something()", or like "document.write".

Wikisource-en

Standardization and cleanup

The standardization script performs a wide variety of standardization and cleanup tasks using the default edit summary "standardization, updates, and cleanup with regex".

  • main namespace
    • normalize and update {{template:header}};
    • normalize location and order of license templates, categories, and interwiki links;
  • author namespace
    • normalize {{template:author}} (experimental);
    • normalize dates in lists of works (experimental);
  • update license templates;
  • normalize template and link syntax;
  • fix newline artifacts (experimental).

Menu link: regexTool('standardization and cleanup','standardize()');
Script:

/* Standardize page */
document.write('<script type="text/javascript" src="'
  + 'http://en.wikisource.org/wiki/User:Pathoschild/standardise.js'
  + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');

Creating a new tool

  1. Add the tool to the menu under the header "add tools to menu", with the syntax regexTool('tool name on the menu','function_name()');. The function_name() is the function that will be executed when the tool is clicked on the menu.
  2. Under the header "define tool scripts", add the function_name() with the following syntax: function function_name() {};

The function will be executed directly when it is called by the framework, so that you may use any JavaScript. However, the framework provides a number of simplified tools explained below.

regsearch()

regsearch(/search/);

Performs a regex search, and returns the first value found (or null if nothing found).

  • search should be a regex pattern object.

regex()

regex(/search/,'replace',repeat);

Performs a search and replace.

  • search should be a regex pattern object.
  • replace should be a replacement string.
  • repeat (optional) should be a number. It defines how many times to recursively perform the pattern. (Use the global modifier "/g" to perform on all non-recursive matches.)

setreason()

setreason('reason','mode');

Changes the edit summary.

  • reason is the text to add.
  • mode (optional) changes how the reason is added (by default, it overwrites the current edit summary).
    • "append" will add the reason at the end of the edit summary ("old summary" → "old summary, new summary").
    • "appendonce" is equivalent to "append", but will only add each reason once.

setoptions()

setoptions(minor='toggle',watch='toggle');

Checks or unchecks edit options.

  • for parameter minor: string used should be "true" or "false" (toggles "minor edit" checkbox).
  • for parameter watch: string used should be "true" or "false" (toggles "watch this page" checkbox).

The toggle must be a string, not a boolean value.

doaction()

doaction('action');

Performs an action. Note that any script after this will be ignored, so this should be used at the very end of the tool script.

  • action should be "preview", "diff", or "save".

Example

This is an example of a very basic script that replaces every "foo" in the edit box with "bar", adds an edit summary, and shows the changes.

/****************
*** add tools to menu
****************/
regexTool('change foo to bar','foo2bar()');

/****************
*** define tool scripts
****************/
/* Change foo to bar */
function foo2bar() {
	regex(/foo/g,'bar');
	setreason('Replaced "foo" with "bar"');
	doaction('diff');
}

Metadata

Update history

In the table below, major changes are bold, fixes and minor changes are normal, and transparent changes are gray.

release changes
1.0 (2007-05-22) public release.
1.1 (2007-08-11)
  • added dynamic regex input form tool;
  • fixed bug with checkbox checking;
  • moved styles to document.write'd <style> tag.
1.2 (2007-08-12) modularized for automatic updates.

To-do in future versions

  • Check compatibility with ProofreadPage extension pages;
I found it; wpTextBox1 is destroyed and re-created by the extension, so the editbox variable in your code points to a dead object. ThomasV 12:31, 14 August 2007 (UTC)
  • switch custom() textareas to one-line input boxes?
did it too. for that one you have to replace \\n with \n in the regexp strings before calling regexp. ThomasV 12:34, 14 August 2007 (UTC)
  • Look to the placement of regex_tools when in edit mode (current placement at bottom in monobook is limiting useful number of scripts), also when in Page: namespace at WS, I would happily see it swallow my the whole left hand margin (not used when editing in that namespace) billinghurst sDrewth 06:44, 21 February 2010 (UTC)
  • Match naming conventions of current skin (see discussion). billinghurst sDrewth
  • Ability to save repetitive regex, even for the duration of a session (detail to talk page) billinghurst sDrewth
  • Ability to locate regex fields as appropriate (as discussed with relation to enWS Page: namespace billinghurst sDrewth

See also