Module:Template invocation: Difference between revisions

    (escape the equals sign as well)
    (add a p.name function to find the template name, use checkType with p.invocation, and add some function description comments)
    Line 1: Line 1:
    -- This module makes a template invocation from a template name and a table
    -- This module makes a template invocation from a template name and a table
    -- of arguments.
    -- of arguments.
    local checkType = require('libraryUtil').checkType


    local p = {}
    local p = {}
    ------------------------------------------------------------------------
    --        Name:  p.name
    --      Purpose:  Find a template invocation name from a page name or a
    --                mw.title object.
    --  Description:  This function detects whether a string or a mw.title
    --                object has been passed in, and uses that to find a
    --                template name as it is used in template invocations.
    --  Parameters:  title - full page name or mw.title obejct for the
    --                template (string or mw.title object)
    --      Returns:  String
    ------------------------------------------------------------------------
    function p.name(title)
    if type(title) == 'string' then
    title = mw.title.new(title)
    if not title then
    error("invalid title in parameter #1 of function 'name'", 2)
    end
    elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then
    error("parameter #1 of function 'name' must be a string or a mw.title object", 2)
    end
    if title.namespace == 10 then
    return title.text
    elseif title.namespace == 0 then
    return ':' .. title.prefixedText
    else
    return title.prefixedText
    end
    end
    ------------------------------------------------------------------------
    --        Name:  p.invocation
    --      Purpose:  Construct a MediaWiki template invocation.
    --  Description:  This function makes a template invocation from the
    --                name and the arguments given. Note that it isn't
    --                perfect: we have no way of knowing what whitespace was
    --                in the original invocation, the order of the parameters
    --                may be changed, and any parameters with duplicate keys
    --                will be removed.
    --  Parameters:  name - the template name, formatted as it will appear
    --                    in the invocation. (string)
    --                args - a table of template arguments. (table)
    --                format - formatting options. (string, optional)
    --                    Set to "nowiki" to escape, curly braces, pipes and
    --                    equals signs with their HTML entities. The default
    --                    is unescaped.
    --      Returns:  String
    ------------------------------------------------------------------------


    function p.invocation(name, args, format)
    function p.invocation(name, args, format)
    checkType('invocation', 1, name, 'string')
    checkType('invocation', 2, args, 'table')
    checkType('invocation', 3, format, 'string', true)
    -- Copy the invocation args and convert magic words.
    -- Copy the invocation args and convert magic words.
    -- We need to make a copy of the table rather than just using the original,
    -- We need to make a copy of the table rather than just using the original,