Module:Infobox/sandbox
From Progressbar95 Wiki
Documentation for this module may be created at Module:Infobox/sandbox/doc
local p = {}
local args = {}
local origArgs = {}
local root
--[[ UTILS ]]--
-- Returns the union of the values of two tables, as a sequence.
local function union(t1, t2)
local vals = {}
for k, v in pairs(t1) do
vals[v] = true
end
for k, v in pairs(t2) do
vals[v] = true
end
local ret = {}
for k, v in pairs(vals) do
table.insert(ret, k)
end
return ret
end
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local function getArgNums(prefix)
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
--[[ RENDERING ]]--
local function renderTitle()
if not args.title then return end
local yesno = require('Module:Yesno')
if yesno(args.titlecaption or false) then
root
:tag('caption')
:addClass('infobox-title')
:addClass(args.titleclass)
:wikitext(args.title)
else
root
:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('infobox-title')
:addClass(args.titleclass)
:wikitext(args.title)
end
end
local function addImageRow(imageArgs)
if imageArgs.data then
local row = root:tag('tr')
row:addClass(imageArgs.rowclass)
local dataCell = row:tag('td')
dataCell
:attr('colspan', '2')
:addClass('infobox-image')
:addClass(imageArgs.class)
:wikitext(imageArgs.data)
end
end
local function renderImages()
if args.image then
args.image1 = args.image
end
if args.caption then
args.caption1 = args.caption
end
local imagenums = getArgNums('image')
for k, num in ipairs(imagenums) do
local caption = args['caption' .. tostring(num)]
local data = mw.html.create():wikitext(args['image' .. tostring(num)])
if caption then
data
:tag('div')
:addClass('infobox-caption')
:wikitext(caption)
end
addImageRow({
data = tostring(data),
class = args.imageclass,
rowclass = args['imagerowclass' .. tostring(num)]
})
end
end
-- Adds a row to the infobox, with either a header cell
-- or a label/data cell combination.
local function addRow(rowArgs)
if rowArgs.header and rowArgs.header ~= '_BLANK_' then
root
:tag('tr')
:addClass(rowArgs.rowclass)
:tag('th')
:attr('colspan', '2')
:addClass('infobox-header')
:addClass(rowArgs.class)
:addClass(args.headerclass)
:wikitext(rowArgs.header)
elseif rowArgs.data then
local row = root:tag('tr')
row:addClass(rowArgs.rowclass)
if rowArgs.label then
row
:tag('th')
:attr('scope', 'row')
:addClass('infobox-label')
:wikitext(rowArgs.label)
:done()
end
local dataCell = row:tag('td')
dataCell
:attr('colspan', not rowArgs.label and '2' or nil)
:addClass(not rowArgs.label and 'infobox-full-data' or 'infobox-data')
:addClass(rowArgs.class)
:wikitext(rowArgs.data)
end
end
-- Gets the union of the header and data argument numbers,
-- and renders them all in order
local function renderRows()
local rownums = union(getArgNums('header'), getArgNums('data'))
table.sort(rownums)
for k, num in ipairs(rownums) do
addRow({
header = args['header' .. tostring(num)],
label = args['label' .. tostring(num)],
data = args['data' .. tostring(num)],
class = args['class' .. tostring(num)],
rowclass = args['rowclass' .. tostring(num)],
})
end
end
local function renderFooter()
if not args.footer then return end
root
:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('infobox-footer')
:addClass(args.footerclass)
:wikitext(args.footer)
end
-- Render tracking categories. args.nocat == turns off tracking categories.
local function renderTrackingCategories()
if args.nocat == 'yes' then return end
if #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then
root:wikitext('[[Category:Articles using infobox templates with no data rows]]')
end
end
local function loadTemplateStyles()
local frame = mw.getCurrentFrame()
local base_templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Infobox/styles.css' }
}
local templatestyles = ''
if args['templatestyles'] then
templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = args['templatestyles'] }
}
end
return table.concat({
base_templatestyles,
templatestyles,
})
end
--[[ ARGUMENTS PROCESSING ]]--
-- If the argument exists, add it to the argument table.
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
local function preprocessSingleArg(argName, altName)
if origArgs[argName] then
args[argName] = origArgs[argName]
elseif origArgs[altName] then
args[argName] = origArgs[altName]
end
end
-- Assign the parameters with the given prefixes to the args table, in order, in
-- batches of the step size specified. This is to prevent references etc. from
-- appearing in the wrong order. The prefixTable should be an array containing
-- tables, each of which has two possible fields, a "prefix" string and a
-- "depend" table. The function always parses parameters containing the "prefix"
-- string, but only parses parameters in the "depend" table if the prefix
-- parameter is present and non-blank.
local function preprocessArgs(prefixTable, step)
if type(prefixTable) ~= 'table' then
error("Non-table value detected for the prefix table", 2)
end
if type(step) ~= 'number' then
error("Invalid step value detected", 2)
end
-- Get arguments without a number suffix, and check for bad input.
for i,v in ipairs(prefixTable) do
if type(v) ~= 'table' or type(v.prefix) ~= "string" or
(v.depend and type(v.depend) ~= 'table') then
error('Invalid input detected to preprocessArgs prefix table', 2)
end
preprocessSingleArg(v.prefix)
-- Only parse the depend parameter if the prefix parameter is present
-- and not blank.
if args[v.prefix] and v.depend then
for j, dependValue in ipairs(v.depend) do
if type(dependValue) ~= 'string' then
error('Invalid "depend" parameter value detected in preprocessArgs')
end
preprocessSingleArg(dependValue)
end
end
end
-- Get arguments with number suffixes.
local a = 1 -- Counter variable.
local moreArgumentsExist = true
while moreArgumentsExist == true do
moreArgumentsExist = false
for i = a, a + step - 1 do
for j,v in ipairs(prefixTable) do
local prefixArgName = v.prefix .. tostring(i)
if origArgs[prefixArgName] then
-- Do another loop if any arguments are found, even blank ones.
moreArgumentsExist = true
preprocessSingleArg(prefixArgName)
end
-- Process the depend table if the prefix argument is present
-- and not blank, or we are processing "prefix1" and "prefix" is
-- present and not blank, and if the depend table is present.
if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then
for j,dependValue in ipairs(v.depend) do
local dependArgName = dependValue .. tostring(i)
preprocessSingleArg(dependArgName)
end
end
end
end
a = a + step
end
end
local function parseDataParameters()
preprocessSingleArg('bodyclass')
preprocessSingleArg('title')
preprocessSingleArg('titleclass')
preprocessSingleArg('titlecaption')
preprocessArgs({
{prefix = 'image', depend = {'caption', 'imagerowclass'}}
}, 10)
preprocessSingleArg('imageclass')
preprocessArgs({
{prefix = 'header'},
{prefix = 'data', depend = {'label'}},
{prefix = 'rowclass'},
{prefix = 'class'}
}, 50)
preprocessSingleArg('headerclass')
preprocessSingleArg('footer', 'below')
preprocessSingleArg('footerclass', 'belowclass')
preprocessSingleArg('name')
preprocessSingleArg('nocat')
preprocessSingleArg('templatestyles')
end
--[[ MAIN ]]--
function p.infobox(frame)
origArgs = require('Module:ProcessArgs').merge( true )
parseDataParameters()
root = mw.html.create('table')
root
:addClass('infobox')
:addClass(args.bodyclass)
:cssText(args.bodystyle)
renderTitle()
renderImages()
renderRows()
renderFooter()
renderTrackingCategories()
root = tostring(root)
return loadTemplateStyles() .. root
end
return p