Title: Module:Unsubst - freem URL Source: https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst Markdown Content: _Documentation for this module may be created at [Module:Unsubst/doc](https://freemwiki.com/index.php?title=Module:Unsubst/doc&action=edit&redlink=1 "Module:Unsubst/doc (page does not exist)")_ [](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-1)local checkType = require('libraryUtil').checkType[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-2)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-3)local p = {}[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-4)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-5)local BODY_PARAM = '$B'[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-6)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-7)local specialParams = {[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-8)['$params'] = 'parameter list',[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-9)['$aliases'] = 'parameter aliases',[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-10)['$flags'] = 'flags',[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-11)['$B'] = 'template content',[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-12)['$template-name'] = 'template invocation name override',[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-13)}[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-14)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-15)function p.main(frame, body)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-16)-- If we are substing, this function returns a template invocation, and if[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-17)-- not, it returns the template body. The template body can be specified in[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-18)-- the body parameter, or in the template parameter defined in the[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-19)-- BODY_PARAM variable. This function can be called from Lua or from[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-20)-- #invoke.[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-21)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-22)-- Return the template body if we aren't substing.[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-23)if not mw.isSubsting() then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-24)if body ~= nil then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-25)return body[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-26)elseif frame.args[BODY_PARAM] ~= nil then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-27)return frame.args[BODY_PARAM][](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-28)else[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-29)error(string.format([](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-30)"no template content specified (use parameter '%s' from #invoke)",[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-31)BODY_PARAM[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-32)), 2)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-33)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-34)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-35)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-36)-- Sanity check for the frame object.[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-37)if type(frame) ~= 'table'[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-38)or type(frame.getParent) ~= 'function'[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-39)or not frame:getParent()[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-40)then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-41)error([](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-42)"argument #1 to 'main' must be a frame object with a parent " ..[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-43)"frame available",[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-44)2[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-45))[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-46)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-47)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-48)-- Find the invocation name.[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-49)local mTemplateInvocation = require(['Module:Template invocation'](https://freemwiki.com/wiki/Module:Template_invocation))[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-50)local name[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-51)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-52)if frame.args['$template-name'] and '' ~= frame.args['$template-name'] then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-53)name = frame.args['$template-name']-- override whatever the template name is with this name[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-54)else[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-55)name = mTemplateInvocation.name(frame:getParent():getTitle())[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-56)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-57)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-58)-- Combine passed args with passed defaults[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-59)local args = {}[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-60)if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*override%s*,' ) then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-61)for k, v in pairs( frame:getParent().args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-62)args[k] = v[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-63)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-64)for k, v in pairs( frame.args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-65)if not specialParams[k] then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-66)if v == '__DATE__' then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-67)v = mw.getContentLanguage():formatDate( 'F Y' )[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-68)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-69)args[k] = v[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-70)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-71)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-72)else[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-73)for k, v in pairs( frame.args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-74)if not specialParams[k] then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-75)if v == '__DATE__' then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-76)v = mw.getContentLanguage():formatDate( 'F Y' )[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-77)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-78)args[k] = v[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-79)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-80)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-81)for k, v in pairs( frame:getParent().args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-82)args[k] = v[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-83)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-84)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-85)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-86)-- Trim parameters, if not specified otherwise[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-87)if not string.find( ','..(frame.args['$flags'] or '')..',', ',%s*keep%-whitespace%s*,' ) then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-88)for k, v in pairs( args ) do args[k] = mw.ustring.match(v, '^%s*(.*)%s*$') or '' end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-89)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-90)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-91)-- Pull information from parameter aliases[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-92)local aliases = {}[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-93)if frame.args['$aliases'] then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-94)local list = mw.text.split( frame.args['$aliases'], '%s*,%s*' )[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-95)for k, v in ipairs( list ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-96)local tmp = mw.text.split( v, '%s*>%s*' )[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-97)aliases[tonumber(mw.ustring.match(tmp[1], '^[1-9][0-9]*$')) or tmp[1]] = ((tonumber(mw.ustring.match(tmp[2], '^[1-9][0-9]*$'))) or tmp[2])[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-98)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-99)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-100)for k, v in pairs( aliases ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-101)if args[k] and ( not args[v] or args[v] == '' ) then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-102)args[v] = args[k][](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-103)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-104)args[k] = nil[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-105)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-106)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-107)-- Remove empty parameters, if specified[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-108)if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*remove%-empty%s*,' ) then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-109)local tmp = 0[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-110)for k, v in ipairs( args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-111)if v ~= '' or ( args[k+1] and args[k+1] ~= '' ) or ( args[k+2] and args[k+2] ~= '' ) then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-112)tmp = k[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-113)else[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-114)break[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-115)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-116)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-117)for k, v in pairs( args ) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-118)if v == '' then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-119)if not (type(k) == 'number' and k < tmp) then args[k] = nil end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-120)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-121)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-122)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-123)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-124)-- Order parameters[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-125)if frame.args['$params'] then[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-126)local params, tmp = mw.text.split( frame.args['$params'], '%s*,%s*' ), {}[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-127)for k, v in ipairs(params) do[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-128)v = tonumber(mw.ustring.match(v, '^[1-9][0-9]*$')) or v[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-129)if args[v] then tmp[v], args[v] = args[v], nil end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-130)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-131)for k, v in pairs(args) do tmp[k], args[k] = args[k], nil end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-132)args = tmp[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-133)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-134)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-135)return mTemplateInvocation.invocation(name, args)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-136)end[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-137)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-138)p[''] = p.main -- For backwards compatibility[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-139)[](https://freemwiki.com/index.php?oldid=580038&title=Module%3AUnsubst#L-140)return p