The editors' meeting has been canceled for technical reasons.

Module:DateTitleList

From NeuroWiki
Revision as of 01:03, 23 January 2025 by Selfice (talk | contribs) (Created page with "local p = {} -- 英文月份映射表 local month_map = { ["01"] = {short = "Jan", full = "January"}, ["02"] = {short = "Feb", full = "February"}, ["03"] = {short = "Mar", full = "March"}, ["04"] = {short = "Apr", full = "April"}, ["05"] = {short = "May", full = "May"}, ["06"] = {short = "Jun", full = "June"}, ["07"] = {short = "Jul", full = "July"}, ["08"] = {short = "Aug", full = "August"}, ["09"] = {short = "Sep", full = "September...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:DateTitleList/doc

local p = {}

-- 英文月份映射表
local month_map = {
    ["01"] = {short = "Jan", full = "January"},
    ["02"] = {short = "Feb", full = "February"},
    ["03"] = {short = "Mar", full = "March"},
    ["04"] = {short = "Apr", full = "April"},
    ["05"] = {short = "May", full = "May"},
    ["06"] = {short = "Jun", full = "June"},
    ["07"] = {short = "Jul", full = "July"},
    ["08"] = {short = "Aug", full = "August"},
    ["09"] = {short = "Sep", full = "September"},
    ["10"] = {short = "Oct", full = "October"},
    ["11"] = {short = "Nov", full = "November"},
    ["12"] = {short = "Dec", full = "December"},
}

-- 格式化日期
local function format_date(frame, date_str, title, year)
    -- 提取年份、月份和日期
    local year_in_date = string.sub(date_str, 1, 4)
    local month = string.sub(date_str, 5, 6) -- 保留字符串,用于查表
    local day = tonumber(string.sub(date_str, 7, 8)) -- 去掉前导零

    -- 获取月份的短名称和全称
    local month_short = month_map[month].short
    local month_full = month_map[month].full

    -- 格式化日期为 "Short Day" (e.g., "Jan 3")
    local display_date = string.format("%s %d", month_short, day)

    -- 构造分类链接
    local category_link
    if year_in_date ~= year then
        -- 使用 ruby 模板来标注年份
        category_link = frame:expandTemplate{
            title = "ruby",
            args = {display_date, year_in_date}
        }
    else
        category_link = display_date
    end

    -- 构造最终输出
    local category = string.format("[[:Category:%s %d|%s]]", month_full, day, category_link)
    local page_link = string.format("[[Streaming_Records/vedal987_Channel/%s %s %d|%s]]", month_full, day, year_in_date, title)

    return string.format("* '''%s''' %s", category, page_link)
end

-- 比较日期,用于排序
local function compare_dates(a, b)
    return a.date < b.date
end

-- 主函数
function p.main(frame)
    local args = frame:getParent().args
    local year = args.year or os.date("%Y")
    local entries = {}

    -- 收集日期和标题
    for i = 1, 7 do
        local date = args["date" .. i]
        local title = args["title" .. i]
        if date and title then
            table.insert(entries, {date = date, title = title})
        end
    end

    -- 按日期排序
    table.sort(entries, compare_dates)

    -- 生成输出列表
    local result = {}
    for _, entry in ipairs(entries) do
        table.insert(result, format_date(frame, entry.date, entry.title, year))
    end

    -- 合并为字符串输出
    return table.concat(result, "\n")
end

return p