Jump to content

Module:Template invocation

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module provides functions for making MediaWiki template invocations.localcheckType=require('libraryUtil').checkTypelocalp={}-------------------------------------------------------------------------- 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 object for the-- template (string or mw.title object)-- Returns: String------------------------------------------------------------------------functionp.name(title)iftype(title)=='string'thentitle=mw.title.new(title)ifnottitleor#title.prefixedText==0or#title.interwiki>0thenerror("invalid title in parameter #1 of function 'name'",2)endelseiftype(title)~='table'ortype(title.getContent)~='function'thenerror("parameter #1 of function 'name' must be a string or a mw.title object",2)endiftitle.namespace==10thenlocaltext=title.textlocalcheck=mw.title.new(text,10)-- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo".returncheckandmw.title.equals(title,check)andtextortitle.prefixedTextelseiftitle.namespace==0thenreturn':'..title.prefixedTextelsereturntitle.prefixedTextendend-------------------------------------------------------------------------- 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 named parameters will be-- alphabetically sorted, 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------------------------------------------------------------------------functionp.invocation(name,args,format)checkType('invocation',1,name,'string')checkType('invocation',2,args,'table')checkType('invocation',3,format,'string',true)-- Validate the args table and make a copy to work from. We need to-- make a copy of the table rather than just using the original, as-- some of the values may be erased when building the invocation.localinvArgs={}fork,vinpairs(args)dolocaltypek=type(k)localtypev=type(v)iftypek~='string'andtypek~='number'ortypev~='string'andtypev~='number'thenerror("invalid arguments table in parameter #2 of ".."'invocation' (keys and values must be strings or numbers)",2)endinvArgs[k]=vend-- Get the separators to use.localseps={openb='{{',closeb='}}',pipe='|',equals='='}ifformat=='nowiki'thenfork,vinpairs(seps)doseps[k]=mw.text.nowiki(v)endend-- Build the invocation body with numbered args first, then named.localret={}ret[#ret+1]=seps.openbret[#ret+1]=namefork,vinipairs(invArgs)doiftype(v)=='string'andv:find('=',1,true)then-- Likely something like 1=foo=bar which needs to be displayed as a named arg.elseret[#ret+1]=seps.piperet[#ret+1]=vinvArgs[k]=nil-- Erase the key so that we don't add the value twiceendendlocalkeys={}-- sort parameter list; better than arbitrary orderfork,_inpairs(invArgs)dokeys[#keys+1]=kendtable.sort(keys,function(a,b)-- Sort with keys of type number first, then string.iftype(a)==type(b)thenreturna<belseiftype(a)=='number'thenreturntrueendend)localmaybeSpace=''-- First named parameter should not be separated by a spacefor_,vinipairs(keys)do-- Add named args based on sorted parameter listret[#ret+1]=maybeSpace..seps.piperet[#ret+1]=tostring(v)ret[#ret+1]=seps.equalsret[#ret+1]=invArgs[v]maybeSpace=' 'endret[#ret+1]=seps.closebreturntable.concat(ret)endreturnp
close