Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Kalendar/Dokumentace

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 headerColor = getValue(frame.args, 'headerColor', '#C0C0C0')
	local color = getValue(frame.args, 'color', '#DCDCDC')
	local dayColor = getValue(frame.args, 'dayColor', '#FCD1C5')
	local float = getValue(frame.args, 'float', 'left')

	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="toccolours" style="float:' .. float .. '; margin-left: 1em; text-align:center; width:150px; padding:0px;" cellpadding=2 cellspacing=0')
	addRow('|- style="background-color:' .. headerColor .. ';"')
	addRow('|align=center| [[' .. prevMonthName .. '| << ]]')
	addRow("|colspan=\"5\"|'''[[" .. monthName .. "]]'''")
	addRow('|align=center| [[' .. nextMonthName .. '| >> ]]')
	addRow('|-  style="background:' .. color .. ';"')
	addRow('|width="14%"|Po')
	addRow('|width="14%"|Út')
	addRow('|width="14%"|St')
	addRow('|width="14%"|Čt')
	addRow('|width="14%"|Pá')
	addRow('|width="14%"|So')
	addRow('|width="14%"|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('| style="background-color:' .. dayColor .. ';" ' .. text)
		else
			addRow(text)
		end
		if (i + firstWeekday - 1) % 7 == 0 then
			addRow("|-")
		end
	end

	addRow('|- style="background:' .. color .. ';"')
	addRow("|colspan=\"7\"|'''[[" .. year .. "]]'''")
	addRow('|}')

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

return p