Module:DateTitleList:修订间差异

跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
local p = {}
local p = {}


-- 格式化日期和生成链接
-- 格式化日期函数,增加对 CSS 类的支持
local function formatDate(date, title, year, baseYear)
local function format_date(frame, date_str, title, year, css_class)
     local dateYear = string.sub(date, 1, 4) -- 获取年份
    -- 提取年份、月份和日期
     local monthDay = os.date("%m月%d日", os.time{
     local year_in_date = string.sub(date_str, 1, 4)
        year = tonumber(dateYear),
     local month = tonumber(string.sub(date_str, 5, 6)) -- 去掉前导零
        month = tonumber(string.sub(date, 5, 6)),
    local day = tonumber(string.sub(date_str, 7, 8)) -- 去掉前导零
        day = tonumber(string.sub(date, 7, 8))
 
     })
     -- 格式化日期为 "X月X日"
   
    local month_day = string.format("%d月%d日", month, day)
     -- 构造分类链接
 
     local categoryLink = '<span class="date-link">[[:Category:' .. monthDay .. '|' .. monthDay .. ']]</span>'
     -- 构造分类链接和 ruby 模板
   
     local category_link
     -- 如果年份不同于baseYear,添加小号年份
    if year_in_date ~= year then
     if dateYear ~= baseYear then
        -- 使用 ruby 模板
         categoryLink = '<span class="date-link">[[:Category:' .. monthDay .. '|{{ruby|' .. monthDay .. '|<span class="small-year">' .. dateYear .. '</span>}}]]</span>'
        category_link = frame:expandTemplate{
            title = "ruby",
            args = { month_day, year_in_date .. "年" }
        }
    else
        category_link = month_day
    end
 
    -- 构造最终分类链接和页面链接
    local category = string.format("[[:Category:%s|%s]]", month_day, category_link)
    local page_link = string.format("[[直播记录/vedal987频道/%s年%s|%s]]", year_in_date, month_day, 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
   
end
    -- 构造页面链接
 
    local pageLink = "[[直播记录/vedal987频道/" .. dateYear .. "年" .. monthDay .. "|" .. title .. "]]"
-- 比较日期,用于排序
   
local function compare_dates(a, b)
     return "* '''" .. categoryLink .. "''' " .. pageLink
     return a.date < b.date
end
end


-- 主函数
-- 主函数
function p.render(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local year = args.year or os.date("%Y") -- 默认年份为当前年份
     local year = args.year or os.date("%Y")
     local entries = {}
     local entries = {}
   
 
     -- 收集date和title的对
     -- 收集日期、标题和样式
     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
   
 
     -- 按日期排序
     -- 按日期排序
     table.sort(entries, function(a, b) return a.date < b.date end)
     table.sort(entries, compare_dates)
   
 
     -- 生成输出
     -- 生成输出列表
     local output = {}
     local result = {}
     for _, entry in ipairs(entries) do
     for _, entry in ipairs(entries) do
         table.insert(output, formatDate(entry.date, entry.title, year, year))
         table.insert(result, format_date(frame, entry.date, entry.title, year, entry.css_class))
     end
     end
      
 
     return table.concat(output, "\n")
     -- 包装在 <ul> 中,作为返回结果
     return '<ul class="date-title-list" style="height: -webkit-fill-available;">' .. table.concat(result, "\n") .. '</ul>'
end
end


return p
return p