User talk:Gerbrant/edit/autoReplace.js

Gerbrant.edit.autoReplace
This module allows you to make a standard set of regular expression based replacements each time you start editing a page. It uses the interface of Gerbrant.edit.regexReplace. Its engine is Gerbrant.edit.multiReplace, which you can use in your own scripts as well.

Usage edit

When installed, everytime you edit a non-talk page, the script will search for a standard set of regular expressions, allowing you to selectively replace them using a standard set of substitutions.

How the interface works is described in detail in the Gerbrant.edit.regexReplace documentation.

Installation edit

This script will only work in conjunction with Gerbrant.fw (documentation). If you're already using it, just add "Gerbrant.edit.autoReplace" to the start of the list of modules to load, otherwise paste the following in your monobook.js:

Gerbrant = {fw: {load: [

"Gerbrant.edit.autoReplace"

]}}

mw.loader.load("http://en.wikipedia.org/w/index.php?title=User:Gerbrant/fw.js&action=raw&ctype=text/javascript");

Exports edit

show()
Activate this module. Gets called everytime a non-talk page is edited.
caption
String, equal to "AutoReplace".
diag()
A diagnostic function to check whether all required libraries have finished loading correctly. Only useful for debugging purposes.

Settings edit

noDefault
If defined and true, disables the default set of replacements.
lib
An optional array of identifiers of modules to be loaded, containing extra sets of replacements. They should export an array of definitions, the format of which is explained below.
defs
An optional array of extra replacements. Each replacement is an object with two members:
re
The regular expression to search for as a string. This means that every \ in the regular expression should be escaped: \d becomes \\d.
hf
A function that is called to determine the replacement text. The first parameter is the full match, the rest are the submatches, if any.

Example edit

The following is a small example. I hope I didn't make any typos. The ellipses (...) indicate the possible presence of more settings (of other modules). Usually this will include the settings for Gerbrant.fw, as shown in the section titled "Installation".

Gerbrant = { ...
edit: { ...
autoReplace:
{
    noDefault: true,
    lib:
    [
        // This loads User:Gerbrant/edit/autoReplace/default.js.
        // Same effect can be achieved by not specifying the noDefault setting.
        "Gerbrant.edit.autoReplace.default",
        // Your own set of replacements.
        "ExampleUser.scriptname"
    ],
    defs:
    [
        {   // This example will replace things like A with A.
            re: "\\[\\[(.*?)\\|\\1\\]\\]",
            hf: function(a,b){return "" + b + "";}
        }
    ]
}
... } ... }