モジュール:今日は何の日
表示
このモジュールについての説明文ページを モジュール:今日は何の日/doc に作成できます
-- メインページの「今日は何の日」。その日(日本時間)のページと、/BY-SA のページの項目を年の順に並べて表示する。
-- 2 つのページは元のライセンスが違うので、文章は別々のページに置いたまま、表示のときだけ並べる。
local p = {}
local function read(title)
local t = mw.title.new(title)
local content = t and t:getContent()
if not content then
return {}, nil
end
content = content:gsub('<noinclude>.-</noinclude>', '')
local items, holiday = {}, nil
for line in mw.text.gsplit(content, '\n') do
local year, text = line:match("^%*%s*'''(.-)'''%s*–%s*(.-)%s*$")
if year then
table.insert(items, { year = year, text = text })
end
local h = line:match('記念日:(.-)%s*$')
if h and h ~= '' and not holiday then
holiday = h
end
end
return items, holiday
end
local function sortkey(year)
local n = tonumber(year:match('%d+'))
if not n then
return math.huge -- 「毎年」など、年の無いものは最後
end
if year:find('紀元前', 1, true) then
return -n
end
return n
end
function p.show(frame)
local m, d
local date = frame.args.date or (frame:getParent() and frame:getParent().args.date)
if date and date:match('^%d+%-%d+$') then
m, d = date:match('^(%d+)%-(%d+)$')
m, d = tonumber(m), tonumber(d)
else
local lang = mw.getContentLanguage()
m = tonumber(lang:formatDate('n', nil, true))
d = tonumber(lang:formatDate('j', nil, true))
end
local base = ('トンチンペディア:今日は何の日/%d月%d日'):format(m, d)
local items, holiday = read(base)
local sa, saholiday = read(base .. '/BY-SA')
for i, x in ipairs(items) do x.i = i end
for i, x in ipairs(sa) do
x.i = #items + i
table.insert(items, x)
end
table.sort(items, function(a, b)
local ka, kb = sortkey(a.year), sortkey(b.year)
if ka ~= kb then
return ka < kb
end
return a.i < b.i
end)
holiday = holiday or saholiday
local out = { ("'''%d月%d日'''"):format(m, d) .. (holiday and (' 記念日:' .. holiday) or '') }
for _, x in ipairs(items) do
table.insert(out, ("* '''%s''' – %s"):format(x.year, x.text))
end
return frame:preprocess(table.concat(out, '\n'))
end
return p