Hopp til innhold

Modul:Category

Fra Wikipedia, den frie encyklopedi

require('strict')

local p = {}

local function getArgument(frame, argument)
	local args = frame.args
	if args[1] == nil then
		local pFrame = frame:getParent();
		args = pFrame.args;
		for k,v in pairs( frame.args ) do
			args[k] = v;
		end
	end
	if args[argument] then
		return args[argument]
	end
	return nil
end

function p.addCats(frame)
	local requested_categories = getArgument(frame, 'cats')
	local output = {}
	if requested_categories and requested_categories ~= '' then
		for category in string.gmatch(requested_categories,"([^,;:]+)") do
			output[#output+1] = '[[Category:' .. category .. ']]'
		end
	end
	return table.concat(output,"\n")
end

local function is_int(n)
  return (type(n) == "number") and (math.floor(n) == n)
end

function p.addCats2(frame)
	local prefix = getArgument(frame, 'prefix')
	if not prefix then
		prefix = ""
	end
	local postfix = getArgument(frame, 'postfix')
	if not postfix then
		postfix = ""
	end
	local args = frame.args
	local output = {}
	if args[1] == nil then
		local pFrame = frame:getParent();
		args = pFrame.args;
		for k,v in pairs( frame.args ) do
			args[k] = v;
		end
	end
	for k,category in pairs( frame.args ) do
		if is_int(k) then
			output[#output+1] = '[[Category:' .. prefix .. category .. postfix .. ']]'
		end
	end
	return table.concat(output,"\n")
end

return p