Module:ULS

From Wikisource
Jump to navigation Jump to search

--[=[
Implements [[Template:ULS]]
]=]

require('strict')

local p = {} --p stands for package

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

function p._ULS(args)
	local inline = yesno(args.inline or true)
	
	local tag
	if inline then
		tag = mw.html.create('span')
	else
		tag = mw.html.create('div')
	end
	
	-- lang
	if args.lang then
		tag:attr('lang', args.lang)
	end
	
	-- font family
	local fonts = {}
	local font_orders = {}
	for k, v in pairs(args) do
		if string.sub(k, 1, 4) == 'font' and tonumber(string.sub(k, 5)) and v then
			table.insert(fonts, v)
			font_orders[v] = tonumber(string.sub(k, 5))
		end
	end
	table.sort(fonts, function(a, b) return font_orders[a] < font_orders[b] end)
	local font_family
	if #fonts > 0 then
		table.insert(fonts, 'serif')
		font_family = 'font-family:' .. table.concat(fonts, ", ") .. ';'
	end
	
	-- font size
	local font_size
	if args['font-size'] then
		font_size = 'font-size:' .. args['font-size'] .. '%;'
	end
	
	-- style attribute
	if font_family or font_size or args.extra then
		tag:attr('style', table.concat({font_family or '', font_size or '', args.extra or ''}))
	end
	
	-- class attribute
	tag:addClass('wst-uls ' .. (args.class or ''))
	
	-- content
	tag:wikitext(args[1] or '')
	
	return (mw.getCurrentFrame():preprocess('<templatestyles src="Template:ULS/styles.css" />')) .. tostring(tag)
end

function p.ULS(frame)
	return p._ULS(getArgs(frame))
end

return p