Module:template parser/doc
This is the documentation page for Module:template parser
This module provides functions for parsing and finding template invocations found in wikitext.
parseTemplate(text, not_transcluded)
- Parses text as a template invocation and returns a pair of values, the template name and the arguments (containing anonymous, numbered and named arguments). If the text could not be parsed as a template invocation, the function returns nil. The parser will correctly parse any wikitext given as template arguments (such as subtemplates, arguments, tables etc), but if the string does not form a valid template in markup, then it will return
nil
. findTemplates(text, not_transcluded)
- Finds all template invocations in the text. This is designed to be used as an iterator in for statements, and returns four values for each invocation:
- The template name.
- The template arguments.
- The full template invocation as it appears in the original text.
- The index the template appears at within the given text; as with Lua in general, the beginning of the text is index 1.
For convenience, template names will be normalized in two ways:
- They are preprocessed, which means that any templates (
{{ }}
) and parameters (Ta̱mpi̱let:param) they contain will be resolved. - Any redirects will be converted to their canonical equivalents (e.g.
{{l}}
is treated as{{link}}
).
Note that any templates with invalid names (after preprocessing) will be skipped over. For performance reasons, preprocessing is only applied to the keys in a template's table of arguments, so it should be applied (selectively) to the values by the calling module when needed.
Note that the parser will respect Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value)., Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value). and Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value). tags. By default, text is treated as though it has been transcluded, which means that text between Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value). tags will be ignored, and Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value). tags will be respected if present. If the parameter not_transcluded is set to true
, then text will be treated as though it has not been transcluded, which means text between Lua error in Module:template_parser at line 86: attempt to call upvalue 'pattern_escape' (a nil value). tags will be ignored instead.
Although the parser is very accurate, some discrepancies may still exist between it and the native parser in certain cases.