مندرجات کا رخ کریں

ماڈیول:Unsubst

آزاد دائرۃ المعارف، ویکیپیڈیا سے

Helper module to facilitate a substituted template transform into a template transclusion.

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.

استعمال

[ترمیم]

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

{{ {{{|safesubst:}}}#invoke:Unsubst||$B=

[... existing template code... ]

}}

The wikitext to display when not substed must be given as "$B". A parameter "$N" may also be seen in some templates; this was required in an older version of the module, but is no longer necessary and may be removed. Such templates are automatically placed inCategory:Calls to Module:Unsubst that use $N.

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}}.

مثال

[ترمیم]

Consider a template Template:Example containing the following code:

{{ {{{|safesubst:}}}#invoke:Unsubst||foo=bar |date=__DATE__ |$B=

[... Template code goes here... ]

}}
اصل نتیجہ
{{subst:example}} {{Example|foo=bar|date=جولا‎ئی 2024}}
{{subst:example|foo=X}} {{Example|foo=X|date=جولا‎ئی 2024}}
{{subst:example|baz=X}} {{Example|foo=bar|baz=X|date=جولا‎ئی 2024}}
{{subst:example|date=January 2001}} {{Example|foo=bar|date=January 2001}}

localcheckType=require('libraryUtil').checkType

localp={}

localBODY_PARAM='$B'

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

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=mTemplateInvocation.name(frame:getParent():getTitle())

-- 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