User:PC-XT/JSubst

JSubst is a virtual template substituter. I started it after several typing-aid substitution templates were deleted. During the discussions, I had said JavaScript would be better for such things. This is not exactly what I was thinking, at the time, because it accepts normal template syntax instead of something easier to parse with regular expressions. Substitution is always forced, but the prefix can be customized.

I named it JSubst as a short representation of JavaScript/JScript substitution. I could have named it USubst, after user (script) substitution, but I also have local JavaScript in mind.

Quick Setup
This is a draft userscript. Even established userscripts should be used with care. If something unexpected happens, see the error section, below.

For the deleted football templates
This will revive the deleted WLeague templates and provide other football templates up for deletion in an alternate form.

Copy this code and paste into your common.js page:

Now, just use the templates as you did, before. They should be automatically substituted while previewing or saving. (I recommend previewing to make sure my conversions of the templates work like you expect.)

Details
In more detail, here are the steps:

First, you have two options: You may copy and paste a line of code, or copy my entire source code, into your common.js page. (Create it if you don't already have one.) The line is shorter, and will allow automatic updates and bug fixes when I make them:

On the other hand, the raw code can be updated manually. You can examine it before you use it, to check for any errors I may have missed or changes you don't like. You also may customize it much further, if you know what you are doing.

Template packs
Template packs can be added to the JSubst object to be used in the list options, later. One template pack is User:PC-XT/JSubst/fb.

Options
Options are set using JSubst.set

Prefix
To not override existing templates, consider using a different prefix, such as JSubst:

or perhaps a smaller prefix, like %:

Keep in mind, some symbols will need escaping, like *: (use regular expression rules, but double the backslash)

You can even use just the letter S:

or maybe you want to be able to use any of these, or "J:", but not "subst:"

List
A list of virtual templates is required. You may use template packs, or create custom lists. Templates are parsed in the order listed. Templates earlier in the list may transclude other JSubst templates as long as they are placed later in the list, (or you can run the parser multiple times.)

Multiple options
You can also set all options in one line:

Making it go
To always parse text before you submit it, add this line:

Instead, you may prefer to place an item in your toolbox to manually apply the substitution, like so:

auto and subst also accept the same parameters as set, so you can save a setup line: or create different tool buttons with different parameters:

Error messages
Is it not working? Have you tried a cache bypass to no avail? To receive error alerts, set option {alert:true}. The following messages are possible:
 * A timeout error happens when the substitution is too complicated to happen in 5 seconds or less. I currently have no workaround for this other than to use fewer of these templates between previews/substitution commands.
 * If you forget to close a template, it will alert you. Substitution doesn't happen until the template is closed.
 * If it is not working right, but no error message appears, ask on the talk page.

Note that currently, &lt;nowiki&gt;&lt;/nowiki&gt; tags are ignored by this routine. I may add an option for that, later, if needed.

Does it need to be this complicated?
No. There are simpler JavaScripts to do similar things. This script is only to handle template syntax. A full script to force subst the deleted WLeague templates, and others, could be as simple as:

This wouldn't work with all template parameters that are named (unless they are in a specific order) or include double closing curly braces, such as other templates or parser functions/some other magic words, which is where JSubst comes in.