Module:template parser/documentation

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Documentation for Module:template parser. [edit]
This page contains usage information, categories, interwiki links and other content describing the module.

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:
  1. The template name.
  2. The template arguments.
  3. The full template invocation as it appears in the original text.
  4. 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:

  1. They are preprocessed, which means that any templates ({{ }}) and parameters ({{{ }}}) they contain will be resolved.
  2. 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 <noinclude>, <includeonly> and <onlyinclude> tags. By default, text is treated as though it has been transcluded, which means that text between <noinclude> tags will be ignored, and <onlyinclude> 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 <includeonly> 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.