Jump to content
Wikipedia The Free Encyclopedia

Module:Unsubst

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 13,700,000 pages, or roughly 21% of all pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
This module depends on the following other modules:

Maintenance templates, such as {{Citation needed }} or {{Refimprove }}, should never be substituted. A trick to avoid that is to make a template substitute to its transcluded form. This module implements that trick.

Infoboxes should use Module:Unsubst-infobox, as should any other templates with parameters listed in block format by default.

Usage

To turn a template into a self-substituting template, wrap the existing template code with:

{{safesubst:<noinclude />#invoke: Unsubst||$B=
 [ ... existing template code ... ]
}}

The wikitext to display when not substed must be given as "$B". All other parameters passed to the #invoke will be copied to the generated template invocation as default values. If the value of any of these default parameters is __DATE__, that value in the generated template invocation will be the current month and year.

Some templates have a <noinclude> but no matching </noinclude> at the end of the template. In such cases the missing </noinclude> must be added before the ending }}.

Advanced

{{safesubst:<noinclude />#invoke: Unsubst||$params=[ parameters ]|$aliases=[ aliases ]|$flags=[ flags ]|$B=
 [ ... existing template code ... ]
}}

Due to Lua limitations, parameters are normally ordered randomly when the template is substituted. |$params= can be used in #invoke:Unsubst to list template parameters in order, comma-separated (e.g. egg,bacon,sausage,cheese,spam). Numbered parameters should be before others in the list. Any remaining parameters are tacked onto the end of the generated invocation.

Parameter aliases can be listed in |$aliases= (and shouldn't be listed in |$params=), and will be replaced automatically. Each alias and its replacement should be formatted as alias>replacement, and each of those pairs should be comma-separated (e.g. œuf>egg,melt>cheese). Note that this parameter can function with or without |$params=.

Parameter |$flags= can be used to modify other facets of the module's behaviour; entries are comma-separated. Valid flags are override (allows parameters in the #invoke: to take precedence over parameters in the original template invocation); keep-whitespace (prevents whitespace from being trimmed from unnamed parameters); and remove-empty (removes empty parameters).

These parameters can be manipulated using parser functions to provide more complicated options (note that in the parameters any parser function, or template or module invocation, should also have SAFESUBST:<noinclude />).

Parameter |$template-name= will override the subst'd template's name with the template name assigned to this parameter.

Example

Consider a template Template:Example containing the following code:

{{safesubst:<noinclude />#invoke: Unsubst||foo=bar |date=__DATE__ |$B=
 [ ... Template code goes here ... ]
}}
Original Result
{{subst:example}} {{Example|date=October 2025|foo=bar}}
{{subst:example|foo=X}} {{Example|date=October 2025|foo=X}}
{{subst:example|baz=X}} {{Example|baz=X|date=October 2025|foo=bar}}
{{subst:example|date=January 2001}} {{Example|date=January 2001|foo=bar}}
The above documentation is transcluded from Module:Unsubst/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

 localcheckType=require('libraryUtil').checkType

 localp={}

 localBODY_PARAM='$B'

 localspecialParams={
 ['$params']='parameter list',
 ['$aliases']='parameter aliases',
 ['$flags']='flags',
 ['$B']='template content',
 ['$template-name']='template invocation name override',
 }

 functionp.main(frame,body)
 -- If we are substing, this function returns a template invocation, and if
 -- not, it returns the template body. The template body can be specified in
 -- the body parameter, or in the template parameter defined in the
 -- BODY_PARAM variable. This function can be called from Lua or from
 -- #invoke.

 -- Return the template body if we aren't substing.
 ifnotmw.isSubsting()then
 ifbody~=nilthen
 returnbody
 elseifframe.args[BODY_PARAM]~=nilthen
 returnframe.args[BODY_PARAM]
 else
 error(string.format(
 "no template content specified (use parameter '%s' from #invoke)",
 BODY_PARAM
 ),2)
 end
 end

 -- Sanity check for the frame object.
 iftype(frame)~='table'
 ortype(frame.getParent)~='function'
 ornotframe:getParent()
 then
 error(
 "argument #1 to 'main' must be a frame object with a parent "..
 "frame available",
 2
 )
 end

 -- Find the invocation name.
 localmTemplateInvocation=require('Module:Template invocation')
 localname

 ifframe.args['$template-name']and''~=frame.args['$template-name']then
 name=frame.args['$template-name']-- override whatever the template name is with this name
 else
 name=mTemplateInvocation.name(frame:getParent():getTitle())
 end

 -- Combine passed args with passed defaults
 localargs={}
 ifstring.find(','..(frame.args['$flags']or'')..',',',%s*override%s*,')then
 fork,vinpairs(frame:getParent().args)do
 args[k]=v
 end
 fork,vinpairs(frame.args)do
 ifnotspecialParams[k]then
 ifv=='__DATE__'then
 v=mw.getContentLanguage():formatDate('F Y')
 end
 args[k]=v
 end
 end
 else
 fork,vinpairs(frame.args)do
 ifnotspecialParams[k]then
 ifv=='__DATE__'then
 v=mw.getContentLanguage():formatDate('F Y')
 end
 args[k]=v
 end
 end
 fork,vinpairs(frame:getParent().args)do
 args[k]=v
 end
 end

 -- Trim parameters, if not specified otherwise
 ifnotstring.find(','..(frame.args['$flags']or'')..',',',%s*keep%-whitespace%s*,')then
 fork,vinpairs(args)doargs[k]=mw.ustring.match(v,'^%s*(.*)%s*$')or''end
 end

 -- Pull information from parameter aliases
 localaliases={}
 ifframe.args['$aliases']then
 locallist=mw.text.split(frame.args['$aliases'],'%s*,%s*')
 fork,vinipairs(list)do
 localtmp=mw.text.split(v,'%s*>%s*')
 aliases[tonumber(mw.ustring.match(tmp[1],'^[1-9][0-9]*$'))ortmp[1]]=((tonumber(mw.ustring.match(tmp[2],'^[1-9][0-9]*$')))ortmp[2])
 end
 end
 fork,vinpairs(aliases)do
 ifargs[k]and(notargs[v]orargs[v]=='')then
 args[v]=args[k]
 end
 args[k]=nil
 end

 -- Remove empty parameters, if specified
 ifstring.find(','..(frame.args['$flags']or'')..',',',%s*remove%-empty%s*,')then
 localtmp=0
 fork,vinipairs(args)do
 ifv~=''or(args[k+1]andargs[k+1]~='')or(args[k+2]andargs[k+2]~='')then
 tmp=k
 else
 break
 end
 end
 fork,vinpairs(args)do
 ifv==''then
 ifnot(type(k)=='number'andk<tmp)thenargs[k]=nilend
 end
 end
 end

 -- Order parameters
 ifframe.args['$params']then
 localparams,tmp=mw.text.split(frame.args['$params'],'%s*,%s*'),{}
 fork,vinipairs(params)do
 v=tonumber(mw.ustring.match(v,'^[1-9][0-9]*$'))orv
 ifargs[v]thentmp[v],args[v]=args[v],nilend
 end
 fork,vinpairs(args)dotmp[k],args[k]=args[k],nilend
 args=tmp
 end

 returnmTemplateInvocation.invocation(name,args)
 end

 p['']=p.main-- For backwards compatibility

 returnp

AltStyle によって変換されたページ (->オリジナル) /