The editors' meeting has been canceled for technical reasons.

Module:DateTitleList: Difference between revisions

From NeuroWiki
Jump to navigation Jump to search
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..."
 
No edit summary
Line 18: Line 18:


-- 格式化日期
-- 格式化日期
local function format_date(frame, date_str, title, year)
local function format_date(frame, date_str, title, year, css_class)
     -- 提取年份、月份和日期
     -- 提取年份、月份和日期
     local year_in_date = string.sub(date_str, 1, 4)
     local year_in_date = string.sub(date_str, 1, 4)
Line 47: Line 47:
     local page_link = string.format("[[Streaming_Records/vedal987_Channel/%s %s %d|%s]]", month_full, day, year_in_date, title)
     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)
     -- 如果有 CSS 类,则将其应用到最外层 li
    if css_class then
        return string.format('<li class="%s">* \'\'\'%s\'\'\' %s</li>', css_class, category, page_link)
    else
        return string.format('<li>* \'\'\'%s\'\'\' %s</li>', category, page_link)
    end
end
end


Line 61: Line 66:
     local entries = {}
     local entries = {}


     -- 收集日期和标题
     -- 收集日期、标题和样式
     for i = 1, 7 do
     for i = 1, 7 do
         local date = args["date" .. i]
         local date = args["date" .. i]
         local title = args["title" .. i]
         local title = args["title" .. i]
        local css_class = args["class" .. i]
         if date and title then
         if date and title then
             table.insert(entries, {date = date, title = title})
             table.insert(entries, {date = date, title = title, css_class = css_class})
         end
         end
     end
     end
Line 76: Line 82:
     local result = {}
     local result = {}
     for _, entry in ipairs(entries) do
     for _, entry in ipairs(entries) do
         table.insert(result, format_date(frame, entry.date, entry.title, year))
         table.insert(result, format_date(frame, entry.date, entry.title, year, entry.css_class))
     end
     end


     -- 合并为字符串输出
     -- 包装在 <ul> 中,作为返回结果
     return table.concat(result, "\n")
     return '<ul class="date-title-list">' .. table.concat(result, "\n") .. '</ul>'
end
end


return p
return p

Revision as of 01:32, 23 January 2025

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, css_class)
    -- 提取年份、月份和日期
    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)

    -- 如果有 CSS 类,则将其应用到最外层 li
    if css_class then
        return string.format('<li class="%s">* \'\'\'%s\'\'\' %s</li>', css_class, category, page_link)
    else
        return string.format('<li>* \'\'\'%s\'\'\' %s</li>', category, page_link)
    end
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]
        local css_class = args["class" .. i]
        if date and title then
            table.insert(entries, {date = date, title = title, css_class = css_class})
        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, entry.css_class))
    end

    -- 包装在 <ul> 中,作为返回结果
    return '<ul class="date-title-list">' .. table.concat(result, "\n") .. '</ul>'
end

return p