ಮಾಡ್ಯೂಲ್:Optional CSS attribute

ವಿಕಿಸೋರ್ಸ್ದಿಂದ

Documentation for this module may be created at ಮಾಡ್ಯೂಲ್:Optional CSS attribute/doc

--[=[
Simple module to
* construct an HTML attribute with an undefined number (including zero) of properties
* construct a string of multiple HTML attributes with one property each
]=]
require('strict')

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

--[=[
Construct the string from the given table of property:values
]=]
function p.make_attribute_string(attribute, properties)
	if not attribute or not properties or mw.text.trim(attribute) == '' or properties == {} then
		return ''
	end
	attribute = mw.text.trim(attribute)
	
	local out  = ''
	for p, v in pairs(properties) do
		local property = mw.text.trim(p)
		local value = mw.text.trim(v)
		if property ~= attribute and property ~= '' and value ~= '' then
			out = out .. property .. ':' .. value .. ';'
		end
	end
	if properties[attribute] then
		out = out .. mw.text.trim(properties[attribute])
	end
	
	if out == '' then
		return ''
	end
	return attribute .. '="' .. out .. '"'
end

--[=[
The main entry function from templates

Arguments are taken from both frame and parent argument lists
]=]
function p.optional_CSS_attribute(frame)
	local args = getArgs(frame)
	local attribute = args[1] or args.attribute
	args[1] = nil
	args.attribute = nil
	return p.make_attribute_string(attribute, args)
end

--[=[
Construct the string from the given table of attributes
]=]
function p.make_multiple_attribute_string(attributes)
	if not attributes or type(attributes) ~= 'table' then
		return ''
	end
	
	local attr_strings = {}
	for a, v in pairs(attributes) do
		local attribute = mw.text.trim(a)
		local value = mw.text.trim(v)
		if attribute ~= '' and value ~= '' then
			table.insert(attr_strings, attribute .. '="' .. value .. '"')
		end
	end
	return table.concat(attr_strings, ' ')
end

--[=[
The main entry function from templates

Arguments are taken from both frame and parent argument lists
]=]
function p.optional_CSS_attribute_list(frame)
	return p.make_multiple_attribute_string(getArgs(frame))
end

return p