Jump to content

Module:Table style

Permanently protected module
From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

local styles = mw.loadData('Module:Table style/styles')
local aliases = mw.loadData('Module:Table style/aliases')

-- for template
function p._main(args)
	local style_strings = {}
	for k, arg in pairs(args) do
		if type(k) == 'number' then
			local style = styles[arg] or styles[aliases[arg]] or arg
			style = string.gsub(style, ';$', '')
			table.insert(style_strings, style)
		end
	end
	
	-- Only add attribute wrapper if any valid style codes were given
	if #style_strings > 0 and yesno(args.nowrapper) then
		return table.concat(style_strings, ';') .. ';'
	elseif #style_strings > 0 then
		return 'style="' .. table.concat(style_strings, ';') .. ';"'
	end
	return nil
end

function p.main(frame)
	return p._main(getArgs(frame))
end

-- for documentation
function p.supported_codes()
	local TableTools = require('Module:TableTools') -- lazy loading
	
	local t = mw.html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
	t:tag('tr')
		:tag('th'):wikitext('Code')
		:tag('th'):wikitext('Aliases')
		:tag('th'):wikitext('Output CSS Style')
	
	for arg, style in TableTools.sortedPairs(styles, function(a, b) return a < b end) do
		local row = t:tag('tr')
		
		-- code
		row:tag('td'):tag('code'):wikitext(arg)
		
		-- aliases
		local aliasList = {}
		for k, v in pairs(aliases) do
			if v == arg then
				table.insert(aliasList, k)
			end
		end
		if #aliasList > 0 then
			table.sort(aliasList)
			local list_td = row:tag('td'):tag('ul')
			for i, alias in ipairs(aliasList) do
				list_td:tag('li'):tag('code'):wikitext(alias)
			end
		else
			row:tag('td')
		end
		
		-- style
		row:tag('td'):tag('code'):wikitext(style)
	end
	return t
end

return p