Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Kalendar/podstranka

require('Module:No globals')

local p = {}
local root = {}

local function addRow(row)
	table.insert(root, row)
end

local function getMonthName(month)
	return ({"leden","únor","březen","duben","květen","červen","červenec","srpen","září","říjen","listopad","prosinec"})[month]
end

local function isLeapYear(year)
	return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
end

local function getDaysInMonth(month, year)
	return (month == 2 and isLeapYear(year) and 29)
		or ("\31\28\31\30\31\30\31\31\30\31\30\31"):byte(month)
end

local function getValue(args, key, default)
	local value = args[key]
	if value and value ~= '' then
		return value
	else
		return default
	end
end

function p.main(frame)
	local year = tonumber(frame.args.year)
	local month = tonumber(frame.args.month)
	local markDay = tonumber(frame.args.markDay) or 0

	local firstWeekday = tonumber(os.date("%w",os.time{year=year, month=month, day=1}))
	local monthName = getMonthName(month)
	local daysInMonth = getDaysInMonth(month, year)

	local prevMonth = (month + 10) % 12 + 1
	local nextMonth = month % 12 + 1
	local prevMonthName = getMonthName(prevMonth)
	local nextMonthName = getMonthName(nextMonth)
	if getValue(frame.args, 'prev-next', 'month') == 'year-month' then
		if prevMonth == 12 then
			prevMonthName = prevMonthName .. ' ' .. (year - 1)
			nextMonthName = nextMonthName .. ' ' .. year
		elseif nextMonth == 1 then
			prevMonthName = prevMonthName .. ' ' .. year
			nextMonthName = nextMonthName .. ' ' .. (year + 1)
		else
			prevMonthName = prevMonthName .. ' ' .. year
			nextMonthName = nextMonthName .. ' ' .. year
		end
	end

	addRow('{| class="wst-kalendar"')
	addRow('|+ ' .. monthName ..' ' .. year )
	addRow('|-')
	addRow('! Po')
	addRow('! Út')
	addRow('! St')
	addRow('! Čt')
	addRow('! Pá')
	addRow('! So')
	addRow('! Ne')
	addRow('|-')

	for i = 1, (firstWeekday - 1) % 7 do
		addRow("|")
	end

	for i = 1, daysInMonth do
		local text = "| [[/" .. i .. ". " .. monthName .. "|" .. i .. "]]"
		if i == markDay then
			addRow('| class="wst-kalendar-den" ' .. text)
		else
			addRow(text)
		end
		if (i + firstWeekday - 1) % 7 == 0 then
			addRow("|-")
		end
	end

	addRow('|}')

	return table.concat(root, '\n')
end

return p