Modul:Params

Z Wikipedie, otevřené encyklopedie
local f = {}

-- Řada
function f.for_nowiki(frame)
	local index = tonumber(frame.args.lowest) or f.lowestNonEmpty(frame)  -- co když to vrátí string?
	local max = tonumber(frame.args.highest) or f.highestNonEmpty(frame)  -- co když to vrátí string?
	local template = mw.text.unstripNoWiki(frame.args[2])
	local result = {}
	while index <= max do
		local temp = string.gsub(template, '{{{i}}}', '{{{' .. index .. '}}}')
		table.insert(result, temp)
		index = index + 1
	end
	return frame:getParent():preprocess(table.concat(result, frame.args[1]))
end

-- Poslední neprázdný
function f.highestNonEmpty(frame)
	local t = {}
	for key, value in pairs(frame:getParent().args) do
		if value ~= '' then
			local i
			if type(key) == 'string' then
				i = tonumber(string.match(key, '[0-9]*$'), 10)
			else
				i = key
			end
			if i ~= nil then
				t[i] = key
			end
		end
	end
	return t[table.maxn(t)]
end

-- První neprázdný
function f.lowestNonEmpty(frame)
	local args = frame:getParent().args
	local length = 0
	for _ in pairs(args) do
		length = length + 1
	end
	local t = {}
	for key, value in pairs(args) do
		if value ~= '' then
			local i
			if type(key) == 'string' then
				i = tonumber(string.match(key, '[0-9]*$'), 10)
			else
				i = key
			end
			if i ~= nil then
				t[length - i] = key
			end
		end
	end
	return t[table.maxn(t)]
end

-- Poslední prázdný nebo neprázdný
function f.highest(frame)
	local t = {}
	for key in pairs(frame:getParent().args) do
		local i
		if type(key) == 'string' then
			i = tonumber(string.match(key, '[0-9]*$'), 10)
		else
			i = key
		end
		if i ~= nil then
			t[i] = key
		end
	end
	return t[table.maxn(t)]
end

-- První prázdný nebo neprázdný
function f.lowest(frame)
	local args = frame:getParent().args
	local length = 0
	for _ in pairs(args) do
		length = length + 1
	end
	local t = {}
	for key in pairs(args) do
		local i
		if type(key) == 'string' then
			i = tonumber(string.match(key, '[0-9]*$'), 10)
		else
			i = key
		end
		if i ~= nil then
			t[length - i] = key
		end
	end
	return t[table.maxn(t)]
end

return f