Module:TemplateBox: Difference between revisions
From Neodyland Wiki
More actions
m args |
m test |
||
| Line 7: | Line 7: | ||
return JSON:encode(args) | return JSON:encode(args) | ||
end | |||
function tobool(st) | |||
return st == 'true' | |||
end | |||
function p.templatebox(frame) | |||
local pargs = mw.clone( ( frame:getParent() or {} ).args or {} ) | |||
local tdata = { | |||
description = '', | |||
params = { | |||
}, | |||
sets = { | |||
} | |||
} | |||
local hasDesc, hasParams = false, false | |||
local JSON = require('Module:JSON') | |||
for k, v in pairs( pargs ) do | |||
if k:find('^%d$') then | |||
local paramNumber = tonumber(k) | |||
local param = pargs[k .. '-td'] or v | |||
local label = pargs[k .. '-label-td'] or pargs[k .. '-label'] | |||
local set = pargs[k .. '-set-td'] or pargs[k .. '-set'] | |||
local required = tobool(pargs[k .. '-required-td'] or pargs[k .. '-required'] or (pargs[k .. 'stat'] == 'required')) | |||
local description = pargs[k .. 'd-td'] or pargs[k .. 'd'] | |||
local deprecated = tobool(pargs[k .. '-deprecated-td'] or pargs[k .. '-deprecated'] or (pargs[k .. 'stat'] == 'deprecated')) | |||
local aliases = mw.text.split(pargs[k .. 'aliases-td'] or pargs[k .. 'aliases'], '/', true) | |||
local default = pargs[k .. 'def-td'] or pargs[k .. 'def'] | |||
local ptype = pargs[k .. 'type-td'] or pargs[k .. 'type'] | |||
tdata.params[param] = { | |||
label = label, | |||
required = required, | |||
description = description, | |||
deprecated = deprecated, | |||
aliases = aliases, | |||
default = default, | |||
type = ptype | |||
} | |||
if set then | |||
local found = false | |||
for i, s in ipairs( tdata.sets ) do | |||
if s.label == set then | |||
table.insert(s.params, param) | |||
found = true | |||
end | |||
end | |||
if not found then | |||
table.insert(tdata.sets, { | |||
label = set, | |||
params = { param } | |||
}) | |||
end | |||
end | |||
pargs[k] = nil | |||
pargs[k .. '_td'] = nil | |||
elseif k:find('^desc$') then | |||
tdata.description = pargs[k .. '-td'] or v | |||
end | |||
end | |||
return JSON:encode(tdata) | |||
end | end | ||
return p | return p | ||
Revision as of 10:33, 12 July 2013
This documentation is transcluded from Module:TemplateBox/doc.
local p = {}
-- =p.test({ args={a="b"} })
function p.test(frame)
local args = frame.args
local JSON = require('Module:JSON')
return JSON:encode(args)
end
function tobool(st)
return st == 'true'
end
function p.templatebox(frame)
local pargs = mw.clone( ( frame:getParent() or {} ).args or {} )
local tdata = {
description = '',
params = {
},
sets = {
}
}
local hasDesc, hasParams = false, false
local JSON = require('Module:JSON')
for k, v in pairs( pargs ) do
if k:find('^%d$') then
local paramNumber = tonumber(k)
local param = pargs[k .. '-td'] or v
local label = pargs[k .. '-label-td'] or pargs[k .. '-label']
local set = pargs[k .. '-set-td'] or pargs[k .. '-set']
local required = tobool(pargs[k .. '-required-td'] or pargs[k .. '-required'] or (pargs[k .. 'stat'] == 'required'))
local description = pargs[k .. 'd-td'] or pargs[k .. 'd']
local deprecated = tobool(pargs[k .. '-deprecated-td'] or pargs[k .. '-deprecated'] or (pargs[k .. 'stat'] == 'deprecated'))
local aliases = mw.text.split(pargs[k .. 'aliases-td'] or pargs[k .. 'aliases'], '/', true)
local default = pargs[k .. 'def-td'] or pargs[k .. 'def']
local ptype = pargs[k .. 'type-td'] or pargs[k .. 'type']
tdata.params[param] = {
label = label,
required = required,
description = description,
deprecated = deprecated,
aliases = aliases,
default = default,
type = ptype
}
if set then
local found = false
for i, s in ipairs( tdata.sets ) do
if s.label == set then
table.insert(s.params, param)
found = true
end
end
if not found then
table.insert(tdata.sets, {
label = set,
params = { param }
})
end
end
pargs[k] = nil
pargs[k .. '_td'] = nil
elseif k:find('^desc$') then
tdata.description = pargs[k .. '-td'] or v
end
end
return JSON:encode(tdata)
end
return p