Module:Border

From Wikisource
Jump to navigation Jump to search

require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local make_style_string = require('Module:Optional style').make_style_string
local CSS_unit = require('Module:CSS unit')._CSS_unit

function p._border_start(args)
	local trackingCats = {}
	
	args['maxwidth'] = args['max-width'] or args.maxwidth
	
	if CSS_unit({args.maxwidth}) == 'px' then
		table.insert(trackingCats, '[[Category:' .. 'Pages using pixel widths (border)' .. ']]')
	end
	for k, v in pairs({'maxwidth', 'bstyle', 'bthickness', 'color', 'bgcolor', 'align', 'padding', 'style'}) do
		if args[v] then
			table.insert(trackingCats, '[[Category:' .. 'Borders using ' .. v .. ' parameter' .. ']]')
		end
	end
	for k, v in pairs(args) do
		if tonumber(k) and tonumber(k) ~= 1 then
			table.insert(trackingCats, '[[Category:' .. 'Borders with numerical arguments' .. ']]')
			break
		end
	end
	
	local styleArgs = {
		['max-width'] = args.maxwidth,
		['display'] = args.display,
		['border-style'] = args.bstyle,
		['border-width'] = args.bthickness,
		['border-color'] = args.color,
		['background-color'] = args.bgcolor,
		['text-align'] = args.align,
		['padding'] = args.padding,
		['style'] = args.style
	}
	if styleArgs['background-color'] then
		styleArgs['color'] = 'inherit'
	end
	local styleParam = make_style_string(styleArgs)
	
	if args.position ~= 'left' and args.position ~= 'right' then
		args.position = 'center'
	end
	local position_class = 'wst-border-' .. args.position
	
	args.compact = yesno(args.compact) or false
	local compact_class = 'wst-border-fullwidth'
	if args.compact then
		compact_class = 'wst-border-compact'
	end
	
	local border_classes = {'wst-border', position_class, compact_class}
	if args.class then
		table.insert(border_classes, args.class)
	end
	
	local divAttrs = {
		'class=\"' .. table.concat(border_classes, ' ') .. '\"',
		styleParam
	}
	if args.attr then
		table.insert(divAttrs, args.attr)
	end
	local divOpen = '<div ' .. table.concat(divAttrs, ' ') .. '>'
	
	local stylesheet = mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Template:Border/styles.css'})
	
	return stylesheet .. divOpen .. table.concat(trackingCats)
end

local function border_end()
	return '</div>'
end

function p._border(args)
	return p.border_start(args) .. (args[1] or '') .. border_end()
end

function p.border_start(frame)
	return p._border_start(getArgs(frame))
end

function p.border(frame)
	return p._border(getArgs(frame))
end

return p