User:PC-XT/JSubst
Description | Allows virtual templates to be substituted without using Template space |
---|---|
Author(s) | PC-XT |
Status | experimental draft |
Updated | January 10, 2014 |
Source | User:PC-XT/JSubst.js |
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
[edit]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
[edit]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:
importScript("User:PC-XT/JSubst.js");/*linkback: [[User:PC-XT/JSubst]]*/
importScript("User:PC-XT/JSubst/fb.js");/*linkback: [[User:PC-XT/JSubst/fb]]*/
JSubst.auto({list:JSubst.fb});
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
[edit]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:
importScript("User:PC-XT/JSubst.js");/*linkback: [[User:PC-XT/JSubst]]*/
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
[edit]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
[edit]Options are set using JSubst.set()
Prefix
[edit]To not override existing templates, consider using a different prefix, such as JSubst:
JSubst.set({prefix:"[Jj][Ss][Uu][Bb][Ss][Tt]",requirePrefix:true});
or perhaps a smaller prefix, like %:
JSubst.set({prefix:"%",requirePrefix:true});
Keep in mind, some symbols will need escaping, like *: (use regular expression rules, but double the backslash)
JSubst.set({prefix:"\\*",requirePrefix:true});
You can even use just the letter S:
JSubst.set({prefix:"[Ss]",requirePrefix:true});
or maybe you want to be able to use any of these, or "J:", but not "subst:"
JSubst.set({prefix:"(%|\\*|[Jj]|[Ss]|[Jj][Ss][Uu][Bb][Ss][Tt])",requirePrefix:true});
List
[edit]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
[edit]You can also set all options in one line:
JSubst.set({prefix:"[Ff][Bb]",requirePrefix:true,list:"fb"});
Making it go
[edit]To always parse text before you submit it, add this line:
JSubst.auto();
Instead, you may prefer to place an item in your toolbox to manually apply the substitution, like so:
addPortletLink('p-tb', 'javascript:JSubst.subst()', 'JSubstlink','JSubstlink');
auto() and subst() also accept the same parameters as set, so you can save a setup line:
JSubst.auto({prefix:"[Ff][Bb]",requirePrefix:true,list:"fb"});
or create different tool buttons with different parameters:
addPortletLink('p-tb', "javascript:JSubst.subst({prefix:'[Ff][Bb]',requirePrefix:true,list:JSubst.fb})", 'JSubstFb','JSubstFb');
addPortletLink('p-tb', "javascript:JSubst.subst({prefix:'%',requirePrefix:true,list:JSubst.talk})", 'JSubstTalk','JSubstTalk');
Error messages
[edit]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, <nowiki></nowiki> tags are ignored by this routine. I may add an option for that, later, if needed.
Does it need to be this complicated?
[edit]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:
document.editform.onsubmit=function(){document.editform.wpTextbox1.value=document.editform.wpTextbox1.value
.replace(/\{\{(subst:|)WLeague NUJ}\}/g,"[[Newcastle Jets FC W-League|Newcastle Jets]]")
.replace(/\{\{(subst:|)WLeague AU}\}/g,"[[Adelaide United FC W-League|Adelaide United]]")
.replace(/\{\{(subst:|)WLeague BR}\}/g,"[[Brisbane Roar FC W-League|Brisbane Roar]]")
.replace(/\{\{(subst:|)WLeague CCM}\}/g,"[[Central Coast Mariners FC W-League|Central Coast Mariners]]")
.replace(/\{\{(subst:|)WLeague CU}\}/g,"[[Canberra United FC|Canberra United]]")
.replace(/\{\{(subst:|)WLeague MV}\}/g,"[[Melbourne Victory FC W-League|Melbourne Victory]]")
.replace(/\{\{(subst:|)WLeague PG}\}/g,"[[Perth Glory FC W-League|Perth Glory]]")
.replace(/\{\{(subst:|)WLeague QR}\}/g,"[[Brisbane Roar FC W-League|Queensland Roar]]")
.replace(/\{\{(subst:|)WLeague SFC}\}/g,"[[Sydney FC W-League|Sydney FC]]")
.replace(/\{\{(subst:|)WLeague WSW}\}/g,"[[Western Sydney Wanderers FC W-League|Western Sydney Wanderers]]")
.replace(/\{\{(subst:|)[Ff]c(\|([^|]*))?}\}/g,"[[$3 F.C.|$3]]")
.replace(/\{\{(subst:|)[Aa]fc(\|([^|]*))?}\}/g,"[[$3 A.F.C.|$3]]")
.replace(/\{\{(subst:|)[Aa]fc2(\|([^|]*))?}\}/g,"[[A.F.C. $3|$3]]")
.replace(/\{\{(subst:|)[Ll]fc(\|([^|]*))?}\}/g,"[[$3 L.F.C.|$3]]")
.replace(/\{\{(subst:|)[Ee]fs(\|([^|]*))?}\}/g,"[[$3 in English football|$3]]")
.replace(/\{\{(subst:|)[Nn]ftu(\|([^|]*))?(\|([^|]*))?}\}/g,"[[$5 national under-$3 football team|$5 U-$3]]")
;};
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.