Module:Xpd

From MediaWiki.org
Jump to navigation Jump to search
Module documentation

This module is invoked from Template:Xpds, Template:Xpdp, Template:Xpdws and Template:Xpdwp (w = show wikitext produced, s = same line, p = new paragraph), expansion demo templates that show the call of a variable, parser function or template, optionally its resulting wikitext, and the latter's rendering. It is also invoked from Template:Xpdplains and Template:Xpdplainp, templates that show plain wikitext and its rendering. They are used for testing and documenting variables, parser functions, templates, and wikitext and html features. The technical notes below apply partly to all templates, partly to the first four only.

For parameters with explicit positive integer names the integer is reduced by 1 in the result (just like for implicit names, due to the position taken by the template name in the calls of the templates xpds and xpdp).

Expressions for parameter values are replaced by their results.

Without affecting the result the template call is written in a normalized form:

  • Values of named parameters are trimmed.[1]
  • Definitions of parameters with implicit or explicit positive integer names are ordered in ascending order.[1] Explicit names among them are removed. These parameters are put before the remaining ones.[1] Multiple definitions of the same parameter are replaced by the final one.[1]
  • In the parameter names and values (after the expansion mentioned above) any braces, pipes and/or equal signs are changed into {{(}}, {{)}}, {{!}}, and {{=}}, so that the parsing of the call is kept intact.[2]

Examples:


{{xpds|Foreach|SeeHook|abc|4= ParserBeforeStrip |5= ParserAfterStrip |delim= / }} gives:

abc/ParserBeforeStripTemplate:Enlink/list1/ParserAfterStripTemplate:Enlink/list1abc/ParserBeforeStripTemplate:Enlink/list1/ParserAfterStripTemplate:Enlink/list1


Wikitext on the left[edit]

To display on the left-hand side the wikitext and not the rendering of the parameter values, some adjustments are made at that side[3]:

  • & is replaced by &
  • square brackets are replaced by [ and ]
  • newlines are replaced by <br>
  • spaces are replaced by &nbsp;

Examples:


[[abc]]     [[abc]]     abc abc


{{2x|&#91;&#91;abc&#93;&#93;&nbsp;}} → [[abc]] [[abc]] 

[[abc]] [[abc]] abc abc


  • abc
    *abc
  • abc
  • abc

 a|b  a|b  → a|b a|b


 a=b  a=b  → a=b a=b


 a{{b  a{{b  → a{{b a{{b


 a}}b  a}}b  → a}}b a}}b


Limitation[edit]

Parameters with explicit or implicit positive integer names have to form a set {1, 2, .. k} without gaps. A template call with for example an undefined parameter 1 and a defined parameter 2 is not supported (where an undefined parameter should not be confused with a parameter which is assigned as value the empty string, these are supported)[4]:

{{xpds|2x|3=abc}} gives: abcabc → abcabc

See also[edit]

Notes[edit]

  1. 1.0 1.1 1.2 1.3 This is done by the system upon invocation of the module, so not programmed in the module or the templates. To circumvent it the module would have to require an input string with pipes between the parameters replaced by a token. This would make it more cumbersome to enter the wikitext for the invocation.
  2. There is no explicit way of telling the system that these characters should be considered plain text: if nowiki is used these tags are treated as part of the parameter name of value.
  3. nowiki does not work
  4. The reason it that at the left side of the output these names are all implicit. The module cannot determine which were written explicitly in the invocation, except when there is a space or newline at the start or end of the value, then the parameter name was implicit. Writing all explicitly would give wrong output, since the righthand side is based on parameters being implicit or explicit (they would be trimmed in the latter case), in accordance with what they were in the invocation of the module. Writing extra pipes would assign the empty string to the undefined parameters in the gaps, which it not the same as them being undefined.

-- Expansion demo, like [[Template:Xpd]]

local p = {}

function p.f(frame, showwikitext, sep)
    local pframe, w = frame:getParent(), { '{{' }
    
    for n,v in pairs( pframe.args ) do
        if type(n) == "string" then
            table.insert( w, table.concat({ '|', p.unexpand(n), '=', p.unexpand(v) }) )
        elseif n ~= 1 then
            table.insert( w, table.concat({ '|', p.unexpand(v) }) )
        else
            table.insert( w, 2, v )
        end
    end
    
    table.insert( w, '}}' )
    w = table.concat( w )
    
    local xpw = frame:preprocess( w )
    
    w = table.concat({ '<code>', p.swt(w), '</code> → ' })
    
    if showwikitext then
        w = table.concat({ w, sep, '<code>', p.swt( xpw ), '</code> → ' })
    end
    
    w = table.concat( { w, xpw }, sep )
    return w
end
 
function p.s(frame) return p.f(frame, false, '') end
function p.p(frame) return p.f(frame, false, '\n\n') end
function p.ws(frame) return p.f(frame, true, '') end
function p.wp(frame) return p.f(frame, true, '\n\n') end

function p.unexpand(w)
    local w1 = string.gsub( w, '[{}|=]', {
        ['{'] = '{{(}}',
        ['}'] = '{{)}}',
        ['|'] = '{{!}}',
        ['='] = '{{=}}',
    }):gsub( 'lbrace', '{{(}}' ):gsub( 'rbrace', '{{)}}' )
    return w1
end
  
function p.swt(w)
    local w1 = string.gsub( w, '[&<>%[%] \n\']', {
        ['&'] = '&amp;',
        ['<'] = '&lt;',
        ['>'] = '&gt;',
        ['['] = '&#91;',
        [']'] = '&#93;',
        ["'"] = '&#39;',
        [' '] = '&nbsp;',
        ['\n'] = '<br/>',
    })
    return w1
end

function p.plain(frame, sep)
    local w = frame:getParent().args[1]
    return table.concat({ '<code>', p.swt( p.unexpand( w ) ), '</code> → ', sep, w  })
end

function p.plains(frame) return p.plain(frame, '') end
function p.plainp(frame) return p.plain(frame, '\n\n') end

return p