User:Pathoschild/Scripts/Regex menu framework

From Meta, a Wikimedia project coordination wiki

Regex menu framework

The regex menu framework has been merged into TemplateScript, which is much more powerful and compatible with the latest MediaWiki changes.

This script is no longer maintained and shouldn't be used; feel free to contact me if you'd like me to migrate your scripts to TemplateScript. Here's a quick comparison:

  Regex menu framework TemplateScript
regex editor ✓ improved with syntax highlighting
custom scripts ✓ improved with editor object to simplify common operations
custom sidebars "Scripts" sidebar ✓ any number of custom sidebars
supported views edit ✓ any (edit, block, protect, etc)
gadget support no-conflict mode (one gadget only) ✓ built-in support (any number of gadgets)
script framework ✘ causes conflicts if used in multiple scripts ✓ built-in support (any number of scripts)
compatibility ✘ unknown ✓ all skins, modernish browsers, MW extensions
keyboard shortcuts
save regex patterns
custom plugins
translatable

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[edit]

This script is no longer maintained; see TemplateScript for a complete replacement instead.

Usage[edit]

Dynamic input form[edit]

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[edit]

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[edit]

Standardization and cleanup[edit]

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 */
mw.loader.load( '//en.wikisource.org/w/index.php?title=User:Pathoschild/standardise.js&action=raw&ctype=text/javascript');

Creating a new tool[edit]

  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()[edit]

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()[edit]

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()[edit]

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()[edit]

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()[edit]

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[edit]

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[edit]

Update history[edit]

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[edit]

  • 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

FAQ[edit]

My saved regex patterns disappeared when Wikimedia switched to HTTPS![edit]

The regex editor works fine in HTTPS, but it saves your settings in your browser's local storage which isn't transferred between protocols. You'll need to recreate your settings in HTTPS mode. If you have complex patterns, that might be troublesome; you can save time by transferring the actual storage data.

  • In Chrome, you can do that like this:
    1. Install the HTML5 LocalStorage Manager extension.
    2. When visiting Wikipedia in HTTP mode, open the storage manager by clicking the icon in the URL bar.
    3. For each entry with a key that starts with "tsre-sessions", copy the key and value into a text file. (Make sure to copy the one just called "tsre-sessions" too!)
    4. Switch to Wikipedia in HTTPS mode and open the storage manager again.
    5. Add the entries you just copied.
  • In Safari, you can do it like this[1]:
    1. Enable the 'Developer' menu.
    2. When visiting Wikipedia in HTTP mode, go to Menu → Developer → Show Web Inspector.
    3. In the new window, choose the icon in the top-left that looks like 3 discs.
    4. For each entry under "Local Storage" with a key that starts with "tsre-sessions", copy the key and value into a text file. (Make sure to copy the one just called "tsre-sessions" too!)
    5. Switch to Wikipedia in HTTPS mode and open the "Local Storage" list again.
    6. Add the entries you just copied.

Your saved patterns should show up now.

See also[edit]