Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:TemplateBox: Difference between revisions

From Neodyland Wiki
m tmp-auto-save
m This has to be tested.
Line 1: Line 1:
local p = {}
local p = {}
local paraminfo = {
    description = {
        required = true,
        default = '',
        t = 'InterfaceText',
        alias = 'desc'
    },
    params = {
        required = true,
        t = 'map',
        children = {
            label = {
                required = false,
                default = '',
                t = 'InterfaceText'
            },
            required = {
                required = true,
                default = false,
                extract = function()
                end
            },
            description = {
                required = false,
                default = '',
                t = 'InterfaceText',
                alias = 'd'
            },
            deprecated = {
                required = true,
                default = false,
                extract = function()
                end
            },
            aliases = {
                required = false,
                default = '',
                extract = function()
                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'
            },
            inherits = {
                required = false,
                default = nil,
                t = 'string'
            }
            -- sets will be treated differently because we can only have a plain structure in wikitext
        },
        key = {
            t = 'string',
            alias = '$d',
            childrenpattern = '^%d'
        }
    }
}


-- =p.test({ args={a="b"} })
-- =p.test({ args={a="b"} })
Line 81: Line 13:
end
end


function p.templatebox(frame)
-- 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 = '',
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'
},
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
function init()
for a, v in pairs( paraminfoTLParams ) do
if v.t ~= nil then
v.t = type( v.default )
end
if v.selection then
v.selection = '|' .. v.selection .. '|'
end
end
end
init()
 
function p.templatedata(frame)
    -- A real parser would look differently but it would likely be much more complex
     local pargs = mw.clone( ( frame:getParent() or {} ).args or {} )
     local pargs = mw.clone( ( frame:getParent() or {} ).args or {} )
     local tdata = {  
    local templateArgs = {}
local i18nTemplateArgs = {}
     local tdata = {
         description = '',
         description = '',
         params = {
         params = {
Line 90: Line 109:
         }
         }
     }
     }
     local hasDesc, hasParams = false, false
     local extractData = function( pi, number )
    local JSON = require('Module:JSON')
local prefix = number or ''
   
local key, key2, tdkey, tdkey2, paramVal
    for k, v in pairs( pargs ) do
        for p, info in pairs( pi ) do
        if type( k ) == 'number' then
key = prefix .. (info.alias or p)
            local param = pargs[k .. '-td'] or v
key2 = prefix .. p
             local label = pargs[k .. '-label-td'] or pargs[k .. '-label']
tdkey = key .. '-td'
            local set = pargs[k .. '-set-td'] or pargs[k .. '-set']
tdkey2 = key2 .. '-td'
            local required = tobool(pargs[k .. '-required-td'] or pargs[k .. '-required'] or (pargs[k .. 'stat'] == 'required'))
             paramVal = pargs[tdkey] or pargs[tdkey2] or pargs[key] or pargs[key2] or info.default
            local description = pargs[k .. 'd-td'] or pargs[k .. 'd']
-- Do we have a multilingual item?
            local deprecated = tobool(pargs[k .. '-deprecated-td'] or pargs[k .. '-deprecated'] or (pargs[k .. 'stat'] == 'deprecated'))
if i18nTemplateArgs[tdkey2] then
            local aliases = mw.text.split(pargs[k .. 'aliases-td'] or pargs[k .. 'aliases'] or '', '/', true)
paramVal = i18nTemplateArgs[tdkey2]
            local default = pargs[k .. 'def-td'] or pargs[k .. 'def']
end
            local ptype = pargs[k .. 'type-td'] or pargs[k .. 'type']
if i18nTemplateArgs[tdkey] then
           
paramVal = i18nTemplateArgs[tdkey]
            tdata.params[param] = {
end
                label = label,
if 'function' == type( info.extract ) then
                required = required,
paramVal = info.extract( pargs, number, paramVal )
                description = description,
end
                deprecated = deprecated,
                aliases = aliases,
local insertValue = function()
                default = default,
if number then
                type = ptype
tdata.params[p] = paramVal
            }
else
           
tdata[p] = paramVal
            if set then
end
                local found = false
end
                for i, s in ipairs( tdata.sets ) do
                    if s.label == set then
if info.selection then
                        table.insert(s.params, param)
if info.selection:find( paramVal, 1, true ) then
                        found = true
insertValue()
                    end
end
                end
elseif 'InterfaceText' == info.t then
                if not found then
if ({ table= 1, string=1 })[type( paramVal )] then
                    table.insert(tdata.sets, {
insertValue()
                        label = set,  
end
                        params = { param }
elseif type( paramVal ) == info.t then
                    })
insertValue()
                 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.templatedata then
mw.log("Warning: Module:TemplateBox - templatedata invoked but not requested by user.")
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
            v = mw.text.trim( v )
            if v ~= '' then
                 table.insert( templateArgs, a )
             end
             end
           
else
            pargs[k] = nil
local argSplit = mw.text.split( a, '-', true )
            pargs[k .. '_td'] = nil
local argUnitl = {}
        elseif k:find('^desc$') then
local argAfter = {}
            tdata.description = pargs[k .. '-td'] or v
local isTDArg = false
for i, part in ipairs( argSplit ) do
if isTDArg 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
argUnitl = table.concat( argUnitl, '-' )
argAfter = table.concat( argAfter, '-' )
i18nTemplateArgs[argUnitl] = i18nTemplateArgs[argUnitl] or {}
i18nTemplateArgs[argUnitl][argAfter] = v
         end
         end
     end
     end
      
     -- Then, start building the actual template
     return JSON:encode(tdata)
     extractData( paraminfoTemplate )
for i, number in pairs( templateArgs ) do
extractData( paraminfoTLParams, number )
end
-- And finally return the result
return JSON:encode( tdata );
end
end


return p
return p

Revision as of 17:15, 5 August 2013

Module documentation[ create · purge ]
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

-- 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 = '',
		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'
	},
	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
function init()
	for a, v in pairs( paraminfoTLParams ) do
		if v.t ~= nil then
			v.t = type( v.default )
		end
		if v.selection then
			v.selection = '|' .. v.selection .. '|'
		end
	end
end
init()

function p.templatedata(frame)
    -- A real parser would look differently but it would likely be much more complex
    local pargs = mw.clone( ( frame:getParent() or {} ).args or {} )
    local templateArgs = {}
	local i18nTemplateArgs = {}
    local tdata = {
        description = '',
        params = {
        },
        sets = {
        }
    }
    local extractData = function( pi, number )
		local prefix = number or ''
		local key, key2, tdkey, tdkey2, paramVal
        for p, info in pairs( pi ) do
			key = prefix .. (info.alias or p)
			key2 = prefix .. p
			tdkey = key .. '-td'
			tdkey2 = key2 .. '-td'
            paramVal = pargs[tdkey] or pargs[tdkey2] or pargs[key] or pargs[key2] or info.default
			-- Do we have a multilingual item?
			if i18nTemplateArgs[tdkey2] then
				paramVal = i18nTemplateArgs[tdkey2]
			end
			if i18nTemplateArgs[tdkey] then
				paramVal = i18nTemplateArgs[tdkey]
			end
			if 'function' == type( info.extract ) then
				paramVal = info.extract( pargs, number, paramVal )
			end
			
			local insertValue = function()
				if number then
					tdata.params[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
			elseif type( paramVal ) == info.t then
				insertValue()
			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.templatedata then
		mw.log("Warning: Module:TemplateBox - templatedata invoked but not requested by user.")
		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
            v = mw.text.trim( v )
            if v ~= '' then
                table.insert( templateArgs, a )
            end
		else
			local argSplit = mw.text.split( a, '-', true )
			local argUnitl = {}
			local argAfter = {}
			local isTDArg = false
			for i, part in ipairs( argSplit ) do
				if isTDArg 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
			argUnitl = table.concat( argUnitl, '-' )
			argAfter = table.concat( argAfter, '-' )
			i18nTemplateArgs[argUnitl] = i18nTemplateArgs[argUnitl] or {}
			i18nTemplateArgs[argUnitl][argAfter] = v
        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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.