Jump to content

Module:Vertical header/sandbox

From Wikisource
require('strict')

local p = {}

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

local function _vertical_header_cell(args)
	-- text
	local text = args[1] or args.text
	
	-- width
	local width = 0
	local maxWidth = args.maxWidth or args.mw
	if maxWidth then
		width = maxWidth
	elseif text then
		local rows = 1
		for eachMatch in text:gmatch('<[bB][rR] */? *>') do
			rows = rows + 1
		end
		width = rows .. 'em'
	end
	if width == '1em' then
		width = nil
	end
	
	-- classes
	local vertAlign = args.vertAlign or args.va
	vertAlign = vertAlign and 'is-valign-' .. vertAlign
	
	local noBold = yesno(args.noBold or args.nb) and 'is-normal'
	
	local class = table.concat({'nowrap', 'ts-vertical-header', vertAlign or '', noBold or '', args.class or ''}, ' ')
	class = mw.ustring.gsub(mw.text.trim(class), ' [ ]+', ' ')
	
	-- styles
	local stylesheet = mw.getCurrentFrame():extensionTag{
		name = 'templatestyles',
		args = {src = 'Module:Vertical header/styles.css'}
	}
	
	-- cell
	local th = mw.html.create('th')
		:addClass(class)
		:css({
			['width'] = width,
			['max-width'] = width
		})
		:cssText(args.cellstyle)
		:attr('rowspan', args.rowspan)
		:attr('colspan', args.colspan)
		:tag('div')
			:css({['style'] = args.style})
			:wikitext(text)
			:done()
		:done()
	
	return stylesheet .. tostring(th)
end

function p.cell(frame)
	return _vertical_header_cell(getArgs(frame))
end

return p