Changes

Jump to navigation Jump to search

Module:Unsubst

1,904 bytes added, 08:31, 7 June 2017
add parameter list, aliases, flags; use Module:Template invocation
local checkType = require('libraryUtil').checkType
 
local p = {}
 
local BODY_PARAM = '$B'
local specialParams = {
['$params'] = 'parameter list', ['$aliases'] = 'parameter aliases', ['$flags'] = 'flags', ['$B'] = 'template content',
}
function p[''] = function .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. if not frame:getParentmw.isSubsting() then if body ~= nil then return body elseif frame.args[BODY_PARAM] ~= nil then return frame.args[BODY_PARAM] else error( string.format( "no template content specified (use parameter '%s'{{from #invoke:Unsubst|}} makes no sense without a parent frame' )", BODY_PARAM ), 2) end
end
  -- Sanity check for the frame object. if not type(frame) ~= 'table' or type(frame.args[getParent) ~= '$Bfunction'] or not frame:getParent() then error( "argument #1 to '{{#invoke:Unsubst|}} requires parameter $B (template content)main' must be a frame object with a parent " .. "frame available", 2 )
end
  -- Find the invocation name. if mwlocal mTemplateInvocation = require('Module:Template invocation') local name = mTemplateInvocation.isSubstingname(frame:getParent() then:getTitle()) ---- substing -- Combine passed args with passed defaults local args = {} if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*override%s*,' ) then for k, v in pairs( frame:getParent().args ) do args[k] = v end for k, v in pairs( frame.args ) do if not specialParams[k] then if v == '__DATE__' then v = mw.getContentLanguage():formatDate( 'F Y' ) end args[k] = v end end else
for k, v in pairs( frame.args ) do
if not specialParams[k] then
args[k] = v
end
end
-- Build an equivalent template invocationTrim parameters, if not specified otherwise if not string.find( ','..(frame.args['$flags'] or '')..',', ',%s*keep%-- Firstwhitespace%s*, find the title to use' ) then local titleobj for k, v in pairs( args ) do args[k] = mw.titleustring.newmatch(frame:getParent():getTitlev, '%s*(.*)%s*')or '' end end local title if titleobj.namespace == 10 then -- NS_TEMPLATERemove empty parameters, if specified title = titleobj if string.find( ','..text elseif titleobj(frame.namespace == 0 then -- NS_MAIN title = args['$flags'] or ':' ).. titleobj.text',', ',%s*remove%-empty%s*,' ) then elsefor k, v in pairs( args ) do title if v == '' then args[k] = titleobj.prefixedTextnil end
end
end
-- Build the invocation body with numbered Pull information from parameter aliases local aliases = {} if frame.args first, ['$aliases'] then named local ret list = mw.text.split( frame.args['$aliases'], '{{%s*,%s*' .. title) for k, v in ipairs( args list ) do if stringlocal tmp = mw.text.findsplit( v, '=%s*>%s*') aliases[tonumber(mw.ustring.match(tmp[1], '^[1, true ) then -9][0- likely something like 9]*$')) or tmp[1]] =foo=bar((tonumber(mw.ustring.match(tmp[2], we need to do it as a named arg'^[1-9][0-9]*$'))) or tmp[2]) break end end ret for k, v in pairs( aliases ) do if args[k] and ( not args[v] or args[v] == ret .. '|' .. v) then args[v] = args[k] = nil
end
args[k] = nil end  -- Order parameters if frame.args['$params'] then local params, tmp = mw.text.split( frame.args['$params'], '%s*,%s*' ), {} for k, v in pairsipairs( args params) do ret v = ret tonumber(mw.ustring. match(v, '|' .. k .. ^[1-9][0-9]*$')) or v if args[v] then tmp[v], args[v] =' .. args[v], nil end
end
for k, v in pairs(args) do tmp[k], args[k] = args[k], nil end return ret .. '}}' else ---- Not substing -- Just return the "body" return frame.args['$B']= tmp
end
 
return mTemplateInvocation.invocation(name, args)
end
 
p[''] = p.main -- For backwards compatibility
return p
Anonymous user

Navigation menu