Module:Unsubst: Difference between revisions

338 bytes added ,  10 years ago
Deprecate the $N parameter; the template name is now detected automatically. The $N check is still present until all transclusions have been updated. Code courtesy of User:Anomie, User:Jackmcbarn and myself.
(Created page with "local p = {} local specialParams = { ['$N'] = 'template name', ['$B'] = 'template content', } p[''] = function ( frame ) if not frame:getParent() then error( '{{#invo...")
(Deprecate the $N parameter; the template name is now detected automatically. The $N check is still present until all transclusions have been updated. Code courtesy of User:Anomie, User:Jackmcbarn and myself.)
Line 1:
local p = {}
 
local specialParams = {
['$N'] = 'template name', -- Deprecated, but keeping until it is removed from transcluding templates
['$B'] = 'template content',
}
 
p[''] = function ( frame )
if not frame:getParent() then
error( '{{#invoke:Unsubst|unsubst}} makes no sense without a parent frame' )
end
if not frame.args[k'$B'] then
for k, v in pairs( specialParams ) do
error( '{{#invoke:Unsubst|unsubst}} requires parameter ' .. k .. '$B ('template .. v .. 'content)' )
if not frame.args[k] then
error( '{{#invoke:Unsubst|unsubst}} requires parameter ' .. k .. ' (' .. v .. ')' )
end
end
if mw.isSubsting() then
---- substing
Line 31 ⟶ 29:
args[k] = v
end
 
-- Now, buildBuild an equivalent template invocation
-- First, numberedfind args,the title thento nameduse
local rettitleobj = '{{' mw.title. new(frame.args['$N']:getParent():getTitle())
local title
if titleobj.namespace == 10 then -- NS_TEMPLATE
title = titleobj.text
elseif titleobj.namespace == 0 then -- NS_MAIN
title = ':' .. titleobj.text
else
title = titleobj.prefixedText
end
 
-- Build the invocation body with numbered args first, then named
local ret = '{{' .. title
for k, v in ipairs( args ) do
if string.find( v, '=', 1, true ) then
Line 46 ⟶ 55:
ret = ret .. '|' .. k .. '=' .. v
end
return ret .. '}}'
else
Line 54 ⟶ 63:
end
end
 
return p
Anonymous user