Difference between revisions of "Module:Unsubst/sandbox"
Jump to navigation
Jump to search
blackwiki>Jackmcbarn (track usage of $N) |
blackwiki>Mr. Stradivarius (split this up into three different functions, to allow other Lua modules to use the invocation-building algorithm and the full unsubst functionality without having to play around with frame objects) |
||
Line 1: | Line 1: | ||
+ | local checkType = require('libraryUtil').checkType | ||
+ | |||
local p = {} | local p = {} | ||
− | local | + | local BODY_PARAM = '$B' |
− | [' | + | |
− | [' | + | function p.invocation(name, args) |
− | + | -- This function makes a template invocation from the name and the arguments | |
+ | -- given. The arguments are not altered. | ||
+ | checkType('p.invocation', 1, name, 'string') | ||
+ | checkType('p.invocation', 2, args, 'table') | ||
+ | |||
+ | -- Copy the invocation args and convert magic words. | ||
+ | -- We need to make a copy of the table rather than just using the original, | ||
+ | -- as some of the values may be erased when building the invocation. | ||
+ | local invArgs = {} | ||
+ | for k, v in pairs(args) do | ||
+ | invArgs[k] = v | ||
+ | end | ||
+ | |||
+ | -- Build the invocation body with numbered args first, then named. | ||
+ | local ret = {} | ||
+ | ret[#ret + 1] = '{{' | ||
+ | ret[#ret + 1] = name | ||
+ | for k, v in ipairs(invArgs) do | ||
+ | if string.find(v, '=', 1, true) then | ||
+ | -- Likely something like 1=foo=bar, we need to do it as a named arg | ||
+ | break | ||
+ | end | ||
+ | ret[#ret + 1] = '|' | ||
+ | ret[#ret + 1] = v | ||
+ | invArgs[k] = nil -- Erase the key so that we don't add the value twice | ||
+ | end | ||
+ | for k, v in pairs(invArgs) do | ||
+ | ret[#ret + 1] = '|' | ||
+ | ret[#ret + 1] = k | ||
+ | ret[#ret + 1] = '=' | ||
+ | ret[#ret + 1] = v | ||
+ | end | ||
+ | ret[#ret + 1] = '}}' | ||
+ | |||
+ | return table.concat(ret) | ||
+ | end | ||
+ | |||
+ | function p._main(frame, body) | ||
+ | -- If we are substing, this function returns a template invocation. The | ||
+ | -- invocation substitutes the magic word "__DATE__" for the current month | ||
+ | -- and year in "Month YYYY" format. If we are not substing, the function | ||
+ | -- returns the body parameter, without any type checking. This function | ||
+ | -- is intended to be called from Lua modules. | ||
+ | -- | ||
+ | -- Params: | ||
+ | -- @frame - the current frame object. Requires a parent frame to be available. | ||
+ | -- @body - the template body, returned as-is if not substing. | ||
+ | |||
+ | if not mw.isSubsting() then | ||
+ | return body | ||
+ | end | ||
− | + | -- Sanity check for the frame object. | |
− | + | if type(frame) ~= 'table' | |
− | error( ' | + | or type(frame.getParent) ~= 'function' |
+ | or not frame:getParent() | ||
+ | then | ||
+ | error( | ||
+ | "argument #1 to '_main' must be a frame object with a parent " .. | ||
+ | "frame available", 2 | ||
+ | ) | ||
end | end | ||
− | if | + | |
− | + | -- Find the invocation name. | |
+ | local titleobj = mw.title.new(frame:getParent():getTitle()) | ||
+ | local name | ||
+ | if titleobj.namespace == 10 then -- NS_TEMPLATE | ||
+ | name = titleobj.text | ||
+ | elseif titleobj.namespace == 0 then -- NS_MAIN | ||
+ | name = ':' .. titleobj.text | ||
+ | else | ||
+ | name = titleobj.prefixedText | ||
end | end | ||
− | + | ||
− | + | -- Combine passed args with passed defaults, and substitute magic words. | |
− | + | local args = {} | |
− | + | for k, v in pairs(frame.args) do | |
− | + | if v == '__DATE__' then | |
− | + | v = mw.getContentLanguage():formatDate('F Y') | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | args[k] = v | ||
+ | end | ||
+ | for k, v in pairs(frame:getParent().args) do | ||
+ | args[k] = v | ||
+ | end | ||
− | + | return p.invocation(name, args) | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | function p.main(frame) | |
− | + | -- This function is called from #invoke. It uses the parameter defined in | |
− | + | -- BODY_PARAM for the template content. | |
− | + | local body = frame.args[BODY_PARAM] | |
− | + | if not body then | |
− | + | error(string.format( | |
− | + | "no template content provided (use parameter '%s')", | |
− | + | BODY_PARAM | |
− | + | ), 2) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | frame.args[BODY_PARAM] = nil | ||
+ | return p._main(frame, body) | ||
end | end | ||
+ | |||
+ | p[''] = p.main -- Alias for p.main, for backwards compatibility. | ||
return p | return p |
Revision as of 06:44, 24 September 2014
Documentation for this module may be created at Module:Unsubst/sandbox/doc
local checkType = require('libraryUtil').checkType
local p = {}
local BODY_PARAM = '$B'
function p.invocation(name, args)
-- This function makes a template invocation from the name and the arguments
-- given. The arguments are not altered.
checkType('p.invocation', 1, name, 'string')
checkType('p.invocation', 2, args, 'table')
-- Copy the invocation args and convert magic words.
-- We need to make a copy of the table rather than just using the original,
-- as some of the values may be erased when building the invocation.
local invArgs = {}
for k, v in pairs(args) do
invArgs[k] = v
end
-- Build the invocation body with numbered args first, then named.
local ret = {}
ret[#ret + 1] = '{{'
ret[#ret + 1] = name
for k, v in ipairs(invArgs) do
if string.find(v, '=', 1, true) then
-- Likely something like 1=foo=bar, we need to do it as a named arg
break
end
ret[#ret + 1] = '|'
ret[#ret + 1] = v
invArgs[k] = nil -- Erase the key so that we don't add the value twice
end
for k, v in pairs(invArgs) do
ret[#ret + 1] = '|'
ret[#ret + 1] = k
ret[#ret + 1] = '='
ret[#ret + 1] = v
end
ret[#ret + 1] = '}}'
return table.concat(ret)
end
function p._main(frame, body)
-- If we are substing, this function returns a template invocation. The
-- invocation substitutes the magic word "__DATE__" for the current month
-- and year in "Month YYYY" format. If we are not substing, the function
-- returns the body parameter, without any type checking. This function
-- is intended to be called from Lua modules.
--
-- Params:
-- @frame - the current frame object. Requires a parent frame to be available.
-- @body - the template body, returned as-is if not substing.
if not mw.isSubsting() then
return body
end
-- Sanity check for the frame object.
if type(frame) ~= 'table'
or type(frame.getParent) ~= 'function'
or not frame:getParent()
then
error(
"argument #1 to '_main' must be a frame object with a parent " ..
"frame available", 2
)
end
-- Find the invocation name.
local titleobj = mw.title.new(frame:getParent():getTitle())
local name
if titleobj.namespace == 10 then -- NS_TEMPLATE
name = titleobj.text
elseif titleobj.namespace == 0 then -- NS_MAIN
name = ':' .. titleobj.text
else
name = titleobj.prefixedText
end
-- Combine passed args with passed defaults, and substitute magic words.
local args = {}
for k, v in pairs(frame.args) do
if v == '__DATE__' then
v = mw.getContentLanguage():formatDate('F Y')
end
args[k] = v
end
for k, v in pairs(frame:getParent().args) do
args[k] = v
end
return p.invocation(name, args)
end
function p.main(frame)
-- This function is called from #invoke. It uses the parameter defined in
-- BODY_PARAM for the template content.
local body = frame.args[BODY_PARAM]
if not body then
error(string.format(
"no template content provided (use parameter '%s')",
BODY_PARAM
), 2)
end
frame.args[BODY_PARAM] = nil
return p._main(frame, body)
end
p[''] = p.main -- Alias for p.main, for backwards compatibility.
return p