انتقل إلى المحتوى

وحدة:Optional style

من ويكي مصدر، المكتبة الحرة
--[=[
	Simple module to construct a style attribute
	with an undefined number (including zero) of CSS properties
]=]

local p = {} --p stands for package
local getArgs = require('Module:Arguments with aliases').getArgs
local aliases = {
	['text-align'] = 'align',
	['font-family'] = 'ff',
	['font-weight'] = 'fw',
	['font-size'] = 'fs',
	['margin'] = 'm',
	['margin-top'] = 'mt',
	['margin-bottom']='mb',
	['letter-spacing'] = 'sp',
	['line-height'] = 'lh',
}

--[=[
	Construct the string from the given table of property:values
]=]
function p.make_style_string(properties)
	local out  = ''
	for k, v in pairs(properties) do
		if k ~= 'style' and v~= '' then
			out = out .. k .. ':' .. v .. ';'
		end
	end
	
	if properties.style ~= nil and properties.style ~= '' then
		out = out .. properties.style	
	end
	
	if out == '' then
		return ''
	end
	
	return 'style="' .. out .. '"'
end

--[=[
	The main entry function from templates
	
	Arguments are taken from both frame and parent argument lists
]=]
function p.optional_style(frame)
	local args = getArgs(frame, {aliases = aliases})
	args.margin = args.margin and (args.margin .. ' 0') or nil
	return p.make_style_string(args)
end

function p.optional_style_filtered(frame)
	local allowed_args = require('Module:TableTools').shallowClone(aliases)
	local args = getArgs(frame, {aliases = aliases})
	for k,_ in pairs(args) do
		if not allowed_args[k] then
			args[k] = nil
		end
	end
	args.margin = args.margin and (args.margin .. ' 0') or nil
	return p.make_style_string(args)
end
return p