Module:ScheduleList:修订间差异

无编辑摘要
无编辑摘要
第7行: 第7行:


-- 工具函数:计算每月天数
-- 工具函数:计算每月天数
local function days_in_month(month, year, leap_year)
local function days_in_month(month, leap_year)
     local days = {31, leap_year and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
     local days = {31, leap_year and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
     return days[month]
     return days[month]
第24行: 第24行:
     local day = tonumber(string.sub(yydd, 3, 4))
     local day = tonumber(string.sub(yydd, 3, 4))
      
      
     local days_in_current_month = days_in_month(month, nil, leap_year)
     local days_in_current_month = days_in_month(month, leap_year)
     if day == days_in_current_month then
     if day == days_in_current_month then
         day = 1
         day = 1
第38行: 第38行:
end
end


-- 工具函数:计算下一周的周几
-- 工具函数:计算周几
local function get_next_weekday(weekday)
local function get_weekday(weekday)
     local weekdays = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"}
     local weekdays = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"}
     return weekdays[(weekday % 7) + 1]
     return weekdays[(weekday - 1) % 7 + 1]
end
 
-- 格式化日期行
local function format_date_line(frame, date, time, week, title, css_class)
    local formatted_date = format_date(date)
    local ruby_output = frame:expandTemplate{
        title = "ruby",
        args = {formatted_date, time}
    }
    if css_class and css_class ~= "" then
        return string.format('<li class="%s">%s<small>%s</small> %s</li>', css_class, ruby_output, week, title)
    else
        return string.format('<li>%s<small>%s</small> %s</li>', ruby_output, week, title)
    end
end
end


第47行: 第61行:
function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
   
 
     -- 获取初始参数
     -- 获取初始参数
     local date = args.date -- 输入的日期为 `yydd` 格式
     local date = args.date -- 输入的日期为 `yydd` 格式
     local week = tonumber(args.week) -- 输入的周几 (1~7)
     local week = tonumber(args.week) -- 输入的周几 (1~7)
     local leap_year = args.leap_year == "yes" -- 闰年判断
     local leap_year = args.leap_year == "yes" -- 闰年判断
      
     local time_default = "2:00"
 
     -- 如果没有输入初始日期,则报错
     -- 如果没有输入初始日期,则报错
     if not date then return "请提供初始日期 (date 参数,格式为 yydd)!" end
     if not date then return "请提供初始日期 (date 参数,格式为 yydd)!" end
     if not week or week < 1 or week > 7 then return "请提供正确的初始周几 (week 参数,1~7)!" end
     if not week or week < 1 or week > 7 then return "请提供正确的初始周几 (week 参数,1~7)!" end
   
 
     -- 初始化结果表
     -- 初始化结果表
     local result = {}
     local result = {}
     local current_date = date
     local current_date = date
     local current_weekday = week
     local current_weekday = week
   
 
     -- 循环生成7行
     -- 循环生成7行
     for i = 1, 7 do
     for i = 1, 7 do
         local title = args["title" .. i] or "无" -- 未提供标题时默认 "无"
         local title = args["title" .. i] or "无" -- 未提供标题时默认 "无"
         local time = args["time" .. i] or "2:00" -- 未提供时间时默认 "2:00"
         local time = args["time" .. i] or time_default -- 未提供时间时默认 "2:00"
         local class = args["class" .. i] or "" -- 未提供 class 时默认空字符串
         local css_class = args["class" .. i] -- 每行的 CSS 类
         local week_day = get_next_weekday(current_weekday - 1) -- 计算周几
         local week_day = get_weekday(current_weekday) -- 计算周几
       
 
         -- 调用 ruby 模板
         -- 格式化每一行
         local ruby_output = frame:expandTemplate{
         table.insert(result, format_date_line(frame, current_date, time, week_day, title, css_class))
            title = "ruby",
 
            args = {format_date(current_date), time}
         -- 更新日期和周几
        }
         current_date = get_next_date(current_date, leap_year)
       
        current_weekday = current_weekday + 1
        -- 格式化当前行
        local formatted_line
        if class ~= "" then
            formatted_line = string.format('<div class="%s">%s<small>%s</small> %s</div>', class, ruby_output, week_day, title)
        else
            formatted_line = string.format('%s<small>%s</small> %s', ruby_output, week_day, title)
        end
       
         -- 添加到结果表,并在每行后添加换行符
         table.insert(result, formatted_line)
     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