Help:Recursive conversion of wikitext
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/- |
- See also Automatic conversion of wikitext
With certain coding, subst: does not automatically convert when it is saved. An editor can create wikitext that is only converted when saved again.
Normally wikitext like {{subst:a}}
is not saved as {{subst:a}}
if {{template:a}}
exists, because subst: pastes the coding of template:a in the saved page.
Contents
Delaying Template:subst
"Subst" can be delayed with one save by creating and using the Template:subst.
template used | template coding | Paste or type | Save | Result |
---|---|---|---|---|
Template:subst | {{subst:
|
{{subst:subst}}a|b}}
|
save page | {{subst:a|b}}
|
On the next save (with or without edits elsewhere on the page) results in substitution of template "a".
Other more complex examples
However, in {{subst:subst}}a|b}}
the visible braces are not balanced - there are two {{
and four }}}}
.
So if {{subst:subst}}a|b}}
is inside another pair of double braces, the final closing braces }} of the inner code are taken as closing braces of the outer pair.
template used | template coding | Paste or type | Save | Result | ||
---|---|---|---|---|---|---|
Template:t1 | start-{{{1}}}-end
|
{{subst:t1|{{subst:subst}}a|b}}|c}}
|
save page | start-{{subst:a-end|c}}
| ||
Braces can be made balanced with Template:Rightbraces (Template:rb) | ||||||
Template:Rightbraces (Template:rb) | }}
|
{{subst:subst}}a|b{{subst:rb}}
|
save page | {{subst:a|b}}
|
But the first pipe in the inner code:
{{subst:subst}}a
|b{{subst:rb}}
Is not inside one of the pairs of braces in the inner code, so it is taken as parameter separator of the outer template or parser function. Therefore we also need Template:!:
template used | template coding | Paste or type | Save | Result |
---|---|---|---|---|
Template:! | |
|
{{subst:t1|{{subst:subst}}a{{subst:!}}b{{subst:rb}}|c}}
|
Save page | start-{{subst:a|b}}-end
|
Making a recursively self-converting page
The resulting page can have the same property of making a saved page containing subst, if we use a template "a" specially designed for this purpose.
To make this template static, every subst:
is replaced by {{{|subst:}}}
, or, alternatvely, <includeonly></includeonly>
is put before (or around) every subst:
A basic form is that of Template:tsubst containing "a{{{{{|subst:}}}subst}}tsubst|b{{{{{|subst:}}}rb}}c
" where b represents the parameters, a the output before the template call, and c the output after the template call. These can all depend on the parameters. Starting with the wikitext {{subst:tsubst|..}}, on every save "a" is added before the braces, the parameter values are replaced, and "c" is added after the braces.
The wikitexts "a", "b" and "c" can contain {{{{{|subst:}}}..}}
, in which case the wikitext produced is the result of expansion of the variable, parser function, or template at the time of saving. Also, ~{{{|~~}}}
gives the signature of the user.
Thus "b" represents the latest situation, and "a" and "c" the history, where chronological order is inward, toward the template call; in other words, the history grows from the inside.
More generally a page can contain multiple template calls, each generating multiple template calls.
If it is so desired, it may be set up so that the template stops reproducing under a certain condition (e.g. by substituting an empty page), only leaving the old versions of "a" and "c" behind, and, if any, the result of the last substitution in between.
Tailoring rendered output
<span style="display:none">..</span> can be used to hide the template call. Proper nesting inside or around pairs of double or triple braces is not needed, during template expansion these tags are treated as plain text. Thus e.g. the opening tag can be in "b" and the closing tag in "c".
If we have a non-empty "c", for hiding the closing braces "c" needs to start with "</span>". Opening and closing span tags have to be balanced to avoid that the template call and/or its output affect the style after that. This requires an opening span tag in "c" to match the closing tag in the "c" that was previously put. It can come either immediately after the closing tag at the start of "c" (optionally specifying a style overriding the external style), or at the end of "c" (the external style applies also for "c"). If we have a non-empty "a", for hiding the opening braces "a" needs to end with "<span style="display:none">". Thus "a" has to start with a closing span tag to match the opening span tag in the previous "a" (the external style applies for "a", unless it contains an extra pair of span tags). Finally, to match the closing span tag at the start of the leftmost (oldest) "a" an opening span tag is needed to the left of that, while to match the opening span tag in the rightmost (oldest) "c" a closing span tag is needed to the right of that. If "a" is empty this opening span tag is also put; similarly, if "c" is empty this closing span tag is also put.
Thus the whole template call, including "b", is hidden, unless inside "b" we have "</span>" and "<span style="display:none">"; in that case the part of "b" in between has the external style, unless it contains an extra pair of span tags.
Dummy span tag pairs (either with nothing in between or without style specification) could be cleaned up manually from time to time.
Documentation
In the case of automatic conversion of wikitext the original wikitext is not automatically documented. The edit summary can be useful for that. In addition, in the case of automatic conversion by just pressing edit and save, both the wikitext before and after the conversion are in the edit history. A code could be put in the edit summary to indicate "no manual change in the wikitext". Thus only the wikitext entered for the first step is undocumented, unless it is put in the edit summary (whose capacity may be too small), or is put in pre- or nowiki-tags in the page itself or the talk page.
Examples
Live example (automatically changed every time "Save page" is pressed):
a{{subst:tsubst|b}}c
Examples:
- Template:Last edit - "a" and "c" are empty, "b" contains edit info
- Template:Edit counter A - a clean edit counter, displaying |n|
- Template:Page history - "a" is empty, "b" contains a header, "c" contains edit info
- Template:Square root - parameter 1 is a number, parameter 2 an approximation of its square root; on every edit/save an iteration step is carried out to improve the approximation.
- Template:Square root with defaults
- Template:Square root with defaults A
- Template:Eraser-left A - "dressing up" a substitution in a parser function
Examples of templates designed for use with the pre-2008 preprocessor, with which they provided an arbitrary number of levels of substitution:
- Template:Square root A - calculates, with Newton's method, the square root of parameter 1, with initial guess being parameter 2, with absolute error stop criterion (terminating when two successive estimates are within parameter 3), in two saves (or, with a parser function, in one save), printing all successive estimates and the differences between consecutive ones
- Template:Square root B the same as {{square root A}}, except that only the final outcome is printed
- Template:Sqr - ditto, with fixed stop criterion of relative error <1e-11 (based on use of a server with 12-digit results); gives a single value, a call can be put in an expression
- Template:Multiple substs - a subst generates multiple substs on each level, in two saves (or, called with a parser function, in one save)
Preview
Note that the preview before/without making manual changes to the wikitext is not the same as the rendering of the current page.
See also
Category:Recursive conversion templates
Links to other help pages
- Help contents
- Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · mw: · b: · s: · mw:Manual · Google
- Versions of this help page (for other languages see further)
- Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary
- What links here on Meta or from Meta · Wikipedia · MediaWiki
- Reading
- Go · Search · Stop words · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
- Tracking changes
- Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
- Logging in and preferences
- Logging in · Preferences · User style
- Editing
- Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
- Referencing
- Links · URLs · Piped links · Interwiki linking · Footnotes
- Style and formatting
- Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
- Fixing mistakes
- Show preview · Testing · Reverting edits
- Advanced functioning
- Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Embed page
- Others
- Special characters · Renaming (moving) a page · Talk page · Signatures · Sandbox · Legal issues for editors