モジュール:サンドボックス/Semi-Brace/カレンダー

モジュールの解説[作成]
--[[--
グレゴリオ暦のカレンダーを作成する
--]]--
require("strict")
local p = {}
function p._main(args)
	local w = tonumber(args.weekday)
	local d = tonumber(args.day)
    local _ = w or error("曜日が未指定です。引数にweekdayを指定してください")
    _ = d or error("末日が未指定です。引数にdayを指定してください")
    if w < 1 or w > 7 then
    	error("曜日が正しくありません。1から7の間で指定してください")
    end
    local cur = args.weekday
    local day = 1
    local last = d
    local t = {
        begin = "{|",
        row = "|-",
        cell = "|",
        head = "!",
        ["end"] = "|}",
        nl = "\n"
    }
    local div7 = 'width="14%"'
    local buf = ""
    -- テーブル初期化
    buf = buf .. t.begin .. 'class="toccolours" '
        .. 'style="' .. (args.table_css or ('float:' .. (args.float or 'right') .. '; width:' .. (args.width or '150px') .. '; margin-left:1em; text-align:center;"'))
        .. t.nl
    local color_1 = args.color_1 or "#ccccff"
    buf = buf .. t.row .. ' ' .. 'style="background:' .. color_1 .. ';"' .. t.nl
    local header = args.header or "カレンダー"
    buf = buf .. t.cell .. 'colspan="7"' .. t.cell .. header .. t.nl
    buf = buf .. t.row .. t.nl
    local color_2 = args.color_2 or "#eeeeff"
    local converts = {
    	[0] = function(a) return '<span style="color:red;">' .. a .. '</span>' end,
    	[6] = function(a) return '<span style="color:deepskyblue;">' .. a .. '</span>' end,
    }
    local convert = function(w_,a)
    	local def_convert = function(a)
    		return '<span style="color:inherit;">' .. a .. '</span>'
	    end
    	return (converts[w_] or def_convert)(a)
    end
    buf = buf .. t.row .. ' ' .. 'style="background:' .. color_2 .. ';"' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[日曜日|' .. convert(0, "日") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[月曜日|' .. convert(1, "月") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[火曜日|' .. convert(2, "火") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[水曜日|' .. convert(3, "水") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[木曜日|' .. convert(4, "木") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[金曜日|' .. convert(5, "金") .. ']]' .. t.nl
    buf = buf .. t.cell .. div7 .. t.cell .. '[[土曜日|' .. convert(6, "土") .. ']]' .. t.nl
    buf = buf .. t.row .. t.nl
    for _ = 1,(w % 7) do -- 空欄作成
    	buf = buf .. t.cell .. "\n"
    end
    
    local prefix = args.prefix or ""
    local suffix = args.suffix or ""

    while day <= last do
    	local s
    	if day >= 1 then
    		local d2 = tostring(day)
    		s = "[[" .. prefix .. d2 .. "日" .. suffix .. "|" .. convert(cur, d2) .. "]]"
    	else
    		s = ""
    	end
        buf = buf .. t.cell .. s .. "\n"
        if cur == 6 then -- 土曜日なら列を区切る
            buf = buf .. t.row .. "\n"
            cur = 0
        else
            cur = cur + 1
        end
        day = day + 1
    end
    return buf .. t["end"] .. "\n"
end

function p.main(frame)
	local getArgs = require("Module:Arguments").getArgs
	return p._main(getArgs(frame))
end

return p