Module:TemplateBox: Difference between revisions
From Neodyland Wiki
More actions
m Protected Module:TemplateBox: Sensitive module used on Template:Navbox ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) |
m Documentation |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Helper function, not exposed | |||
function tobool(st) | function tobool(st) | ||
if type( st ) == 'string' then | if type( st ) == 'string' then | ||
| Line 9: | Line 10: | ||
end | end | ||
-- InterfaceText: | |||
-- A free-form string (no wikitext) in the content-language of the wiki, or, an object containing those strings keyed by language code. | |||
------------------------------------------------------ | |||
----------------- TEMPLATEDATA PART ------------------ | |||
------------------------------------------------------ | |||
-- A "hash" / table of everything TemplateData takes | |||
-- to ease maintenance. | |||
-- The type is automatically determined if t is omitted. | |||
-- If the type does not match or can't be converted, an error will be thrown! | |||
-- Available types (LUA-Types with exceptions): | |||
-- InterfaceText, boolean, number, selection, table, string | |||
-- selection*: - requires a selection-string of pipe-separated possibilities to be supplied | |||
-- InterfaceText*: A free-form string (no wikitext) in the content-language of the wiki, or, | |||
-- an object containing those strings keyed by language code. | |||
local paraminfoTemplate = { | local paraminfoTemplate = { | ||
description = { | description = { | ||
| Line 82: | Line 98: | ||
-- Initialize param info | -- Initialize param info | ||
-- Avoids having to add redundant information to the preceding tables | |||
function init( which ) | function init( which ) | ||
local setDefault = function(v) | local setDefault = function(v) | ||
| Line 98: | Line 115: | ||
init( paraminfoTLParams ) | init( paraminfoTLParams ) | ||
-- A real parser/transformer would look differently but it would likely be much more complex | |||
-- The TemplateData-portion for [[Template:TemplateBox]] | |||
function p.templatedata(frame) | function p.templatedata(frame) | ||
-- | -- Load the JSON-Module which will convert LUA tables into valid JSON | ||
local JSON = require('Module:JSON') | local JSON = require('Module:JSON') | ||
-- All kind of strange stuff with the arguments is done, so play safe and make a copy | |||
local pargs = mw.clone( ( frame:getParent() or {} ).args or {} ) | local pargs = mw.clone( ( frame:getParent() or {} ).args or {} ) | ||
-- Array-like table containing all parameter-numbers that were passed | |||
local templateArgs = {} | local templateArgs = {} | ||
-- Arguments that are localized (i.e. the user passed 1desc-en=English description of parameter one) | |||
local i18nTemplateArgs = {} | local i18nTemplateArgs = {} | ||
-- Required to determine in which languages the interface texts without langcode are | |||
local contentLangcode = mw.language.getContentLanguage():getCode() | |||
-- Ensure that tables end up as array/object (esp. when they are empty) | |||
JSON.strictTypes = true | JSON.strictTypes = true | ||
local tdata = JSON:decode('{"description":"","params":{},"sets":[]}') | local tdata = JSON:decode('{"description":"","params":{},"sets":[]}') | ||
local extractData = function( pi, number ) | local extractData = function( pi, number ) | ||
local prefix = number or '' | local prefix = number or '' | ||
| Line 217: | Line 241: | ||
if type( a ) == 'number' then | if type( a ) == 'number' then | ||
table.insert( templateArgs, a ) | table.insert( templateArgs, a ) | ||
else | else | ||
local argSplit = mw.text.split( a, '-', true ) | local argSplit = mw.text.split( a, '-', true ) | ||
Revision as of 15:27, 15 August 2013
This documentation is transcluded from Module:TemplateBox/doc.
local p = {}
-- Helper function, not exposed
function tobool(st)
if type( st ) == 'string' then
return st == 'true'
else
return not not st
end
end
------------------------------------------------------
----------------- TEMPLATEDATA PART ------------------
------------------------------------------------------
-- A "hash" / table of everything TemplateData takes
-- to ease maintenance.
-- The type is automatically determined if t is omitted.
-- If the type does not match or can't be converted, an error will be thrown!
-- Available types (LUA-Types with exceptions):
-- InterfaceText, boolean, number, selection, table, string
-- selection*: - requires a selection-string of pipe-separated possibilities to be supplied
-- InterfaceText*: A free-form string (no wikitext) in the content-language of the wiki, or,
-- an object containing those strings keyed by language code.
local paraminfoTemplate = {
description = {
required = true,
default = '',
t = 'InterfaceText',
alias = 'desc'
}
}
local paraminfoTLParams = {
label = {
required = false,
default = '',
t = 'InterfaceText'
},
required = {
required = true,
default = false,
extract = function(pargs, number, paramVal)
local req = (pargs[number .. 'stat'] == 'required')
return tobool( paramVal or req )
end
},
description = {
required = false,
default = '',
t = 'InterfaceText',
alias = 'd'
},
deprecated = {
required = true,
default = false,
extract = function(pargs, number, paramVal)
local depr = (pargs[number .. 'stat'] == 'deprecated')
return tobool( paramVal or depr )
end
},
aliases = {
required = false,
default = '',
t = 'table',
extract = function(pargs, number, paramVal)
local key = number .. 'aliases'
local tdkey = key .. '-td'
local aliases = pargs[tdkey] or pargs[key]
if aliases then
aliases = mw.text.split( aliases, '/', true )
end
return aliases
end
},
default = {
required = false,
default = '',
t = 'InterfaceText',
alias = 'def'
},
type = {
required = false,
default = 'unknown',
t = 'selection',
selection = 'unknown|number|string|string/wiki-user-name|string/wiki-page-name|string/line'
},
inherits = {
required = false,
default = nil,
t = 'string'
}
-- sets will be treated differently because we can only have a plain structure in wikitext
}
-- Initialize param info
-- Avoids having to add redundant information to the preceding tables
function init( which )
local setDefault = function(v)
if v.t == nil and v.default ~= nil then
v.t = type( v.default )
end
if v.selection then
v.selection = '|' .. v.selection .. '|'
end
end
for a, v in pairs( which ) do
setDefault(v)
end
end
init( paraminfoTemplate )
init( paraminfoTLParams )
-- A real parser/transformer would look differently but it would likely be much more complex
-- The TemplateData-portion for [[Template:TemplateBox]]
function p.templatedata(frame)
-- Load the JSON-Module which will convert LUA tables into valid JSON
local JSON = require('Module:JSON')
-- All kind of strange stuff with the arguments is done, so play safe and make a copy
local pargs = mw.clone( ( frame:getParent() or {} ).args or {} )
-- Array-like table containing all parameter-numbers that were passed
local templateArgs = {}
-- Arguments that are localized (i.e. the user passed 1desc-en=English description of parameter one)
local i18nTemplateArgs = {}
-- Required to determine in which languages the interface texts without langcode are
local contentLangcode = mw.language.getContentLanguage():getCode()
-- Ensure that tables end up as array/object (esp. when they are empty)
JSON.strictTypes = true
local tdata = JSON:decode('{"description":"","params":{},"sets":[]}')
local extractData = function( pi, number )
local prefix = number or ''
local key, key2, tdkey, tdkey2, ppv, paramVal
local paramKey, paramTable
if number then
paramKey = mw.text.trim( pargs[number] )
if '' == paramKey then
paramKey = tostring( number )
end
tdata.params[paramKey] = {}
paramTable = tdata.params[paramKey]
end
for p, info in pairs( pi ) do
key = prefix .. (info.alias or p)
key2 = prefix .. p
tdkey = key .. '-td'
tdkey2 = key2 .. '-td'
ppv = pargs[tdkey] or pargs[tdkey2] or pargs[key] or pargs[key2] or info.default
-- Do we have a multilingual item?
paramVal = i18nTemplateArgs[tdkey] or i18nTemplateArgs[tdkey2] or i18nTemplateArgs[key] or i18nTemplateArgs[key2]
if 'table' == type( paramVal ) then
if (nil == paramVal[contentLangcode]) then
paramVal[contentLangcode] = ppv
end
else
paramVal = ppv
end
if 'function' == type( info.extract ) then
if 'string' == type( paramVal ) then
paramVal = mw.text.trim( paramVal )
if '' == paramVal then
paramVal = nil
end
end
paramVal = info.extract( pargs, number, paramVal )
end
local insertValue = function()
if number then
paramTable[p] = paramVal
else
tdata[p] = paramVal
end
end
if info.selection then
if info.selection:find( paramVal, 1, true ) then
insertValue()
end
elseif 'InterfaceText' == info.t then
if ({ table=1, string=1 })[type( paramVal )] then
insertValue()
end
else
local paramType = type( paramVal )
if 'string' == info.t and 'string' == paramType then
paramVal = mw.text.trim( paramVal )
if '' ~= paramVal then
insertValue()
end
elseif 'boolean' == info.t then
paramVal = tobool(paramVal)
insertValue()
elseif 'number' == info.t then
paramVal = tonumber(paramVal)
insertValue()
elseif paramType == info.t then
insertValue()
elseif paramType == 'nil' then
-- Do nothing
else
error( p .. ': Is of type ' .. paramType .. ' but should be of type ' .. (info.t or 'unknown'), 1 )
end
end
end
-- Now, treat sets
key = prefix .. 'set'
tdkey = prefix .. 'set-td'
paramVal = pargs[tdkey] or pargs[key]
if paramVal then
local found = false
for i, s in ipairs( tdata.sets ) do
if s.label == paramVal then
table.insert( s.params, p )
found = true
end
end
if not found then
table.insert( tdata.sets, {
label = paramVal,
params = { p }
} )
end
end
end
if not pargs.useTemplateData then
local warning = "Warning: Module:TemplateBox - templatedata invoked but not requested by user (setting useTemplateData=1)."
mw.log(warning)
tdata.description = warning
return JSON:encode(tdata);
end
-- First, analyse the structure of the provided arguments
for a, v in pairs( pargs ) do
if type( a ) == 'number' then
table.insert( templateArgs, a )
else
local argSplit = mw.text.split( a, '-', true )
local argUnitl = {}
local argAfter = {}
local isTDArg = false
local containsTD = a:find( '-td', 1, true )
for i, part in ipairs( argSplit ) do
if isTDArg or (containsTD == nil and i > 1) then
-- This is likely a language version
table.insert( argAfter, part )
else
table.insert( argUnitl, part )
end
if part == 'td' then
isTDArg = true
end
end
if #argAfter > 0 then
argUnitl = table.concat( argUnitl, '-' )
argAfter = table.concat( argAfter, '-' )
i18nTemplateArgs[argUnitl] = i18nTemplateArgs[argUnitl] or {}
i18nTemplateArgs[argUnitl][argAfter] = v
end
end
end
-- Then, start building the actual template
extractData( paraminfoTemplate )
for i, number in pairs( templateArgs ) do
extractData( paraminfoTLParams, number )
end
-- And finally return the result
return JSON:encode(tdata);
end
return p