Module:Infobox/sandbox: Difference between revisions

From Progressbar95 Wiki
Content added Content deleted
m (render title as old above for styling purposes)
m (accept "below" instead of "footer")
Line 210: Line 210:
-- If the argument exists, add it to the argument table.
-- If the argument exists, add it to the argument table.
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
local function preprocessSingleArg(argName)
local function preprocessSingleArg(argName, altName)
if origArgs[argName] then
if origArgs[argName] then
args[argName] = origArgs[argName]
args[argName] = origArgs[argName]
elseif origArgs[altName] then
args[argName] = origArgs[altName]
end
end
end
end
Line 301: Line 303:
preprocessSingleArg('labelstyle')
preprocessSingleArg('labelstyle')
preprocessSingleArg('datastyle')
preprocessSingleArg('datastyle')
preprocessSingleArg('footer')
preprocessSingleArg('footer', 'below')
preprocessSingleArg('footerclass')
preprocessSingleArg('footerclass', 'belowclass')
preprocessSingleArg('footerstyle')
preprocessSingleArg('footerstyle', 'belowstyle')
preprocessSingleArg('name')
preprocessSingleArg('name')
preprocessSingleArg('nocat')
preprocessSingleArg('nocat')

Revision as of 17:43, 26 April 2024

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()
	local titleObject = mw.title.getCurrentTitle()
	local title = args.title or titleObject.baseText
	
	root
		:tag('tr')
			:tag('th')
				:attr('colspan', '2')
				:addClass('pbwiki-header')
				:addClass('infobox-title')
				:addClass(args.titleclass)
				:cssText(args.titlestyle)
				:wikitext(title)
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)
			:cssText(imageArgs.datastyle)
			: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')
					:cssText(args.captionstyle)
					:wikitext(caption)
		end
		addImageRow({
			data = tostring(data),
			datastyle = args.imagestyle,
			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)
				:cssText(rowArgs.rowstyle)
				:tag('th')
					:attr('colspan', '2')
					:addClass('infobox-header')
					:addClass(rowArgs.class)
					:addClass(args.headerclass)
					:cssText(args.headerstyle)
					:cssText(rowArgs.rowcellstyle)
					:wikitext(rowArgs.header)
	elseif rowArgs.data then
		local row = root:tag('tr')
		row:addClass(rowArgs.rowclass)
		row:cssText(rowArgs.rowstyle)
		if rowArgs.label then
			row
				:tag('th')
					:attr('scope', 'row')
					:addClass('infobox-label')
					:cssText(args.labelstyle)
					:cssText(rowArgs.rowcellstyle)
					: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)
			:cssText(rowArgs.datastyle)
			:cssText(rowArgs.rowcellstyle)
			: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)],
			datastyle = args.datastyle,
			class = args['class' .. tostring(num)],
			rowclass = args['rowclass' .. tostring(num)],
			rowstyle = args['rowstyle' .. 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)
				:cssText(args.footerstyle)
				: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

--[=[
Loads the templatestyles for the infobox.

TODO: FINISH loading base templatestyles here rather than in
MediaWiki:Common.css.

]=]
local function loadTemplateStyles()
	local frame = mw.getCurrentFrame()
	
	-- See function description
	local base_templatestyles = frame:extensionTag{
		name = 'templatestyles', args = { src = 'Module:Infobox/sandbox/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('bodystyle')
	preprocessSingleArg('title')
	preprocessSingleArg('titleclass')
	preprocessSingleArg('titlestyle')
	preprocessArgs({
		{prefix = 'image', depend = {'caption'}}
	}, 10)
	preprocessSingleArg('captionstyle')
	preprocessSingleArg('imagestyle')
	preprocessSingleArg('imageclass')
	preprocessArgs({
		{prefix = 'header'},
		{prefix = 'data', depend = {'label'}},
		{prefix = 'rowclass'},
		{prefix = 'rowstyle'},
		{prefix = 'class'}
	}, 50)
	preprocessSingleArg('headerclass')
	preprocessSingleArg('headerstyle')
	preprocessSingleArg('labelstyle')
	preprocessSingleArg('datastyle')
	preprocessSingleArg('footer', 'below')
	preprocessSingleArg('footerclass', 'belowclass')
	preprocessSingleArg('footerstyle', 'belowstyle')
	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