Module:ScheduleList:修订间差异

来自NeuroWiki
跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
第60行: 第60行:
         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 "2:00" -- 未提供时间时默认 "2:00"
        local class = args["class" .. i] or "" -- 未提供 class 时默认空字符串
         local week_day = get_next_weekday(current_weekday - 1) -- 计算周几
         local week_day = get_next_weekday(current_weekday - 1) -- 计算周几
          
          
第68行: 第69行:
         }
         }
          
          
         -- 格式化当前行
         -- 格式化当前行,添加 class
         table.insert(result, string.format("%s<small>%s</small> %s", ruby_output, week_day, title))
         if class ~= "" then
            table.insert(result, string.format('<div class="%s">%s<small>%s</small> %s</div>', class, ruby_output, week_day, title))
        else
            table.insert(result, string.format('%s<small>%s</small> %s', ruby_output, week_day, title))
        end
          
          
         -- 更新日期和周几
         -- 更新日期和周几
第76行: 第81行:
     end
     end
      
      
     -- 返回结果
     -- 返回结果,每行分开
     return table.concat(result, "\n")
     return table.concat(result, "\n")
end
end


return p
return p

2025年1月23日 (四) 07:06的版本

此模块的文档可以在Module:ScheduleList/doc创建

local p = {}

-- 工具函数:判断是否为闰年
local function is_leap_year(year)
    return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end

-- 工具函数:计算每月天数
local function days_in_month(month, year, leap_year)
    local days = {31, leap_year and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    return days[month]
end

-- 工具函数:计算下一个日期
local function get_next_date(date, leap_year)
    local month, day = date:match("(%d+)月(%d+)日")
    month = tonumber(month)
    day = tonumber(day)
    
    local days_in_current_month = days_in_month(month, nil, leap_year)
    if day == days_in_current_month then
        day = 1
        month = month + 1
        if month > 12 then
            month = 1
        end
    else
        day = day + 1
    end
    
    return string.format("%d月%d日", month, day)
end

-- 工具函数:计算下一周的周几
local function get_next_weekday(weekday)
    local weekdays = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"}
    return weekdays[(weekday % 7) + 1]
end

-- 主函数
function p.main(frame)
    local args = frame:getParent().args
    
    -- 获取初始参数
    local date = args.date
    local week = tonumber(args.week)
    local leap_year = args.leap_year == "yes"
    
    -- 如果没有输入初始日期,则报错
    if not date then return "请提供初始日期 (date 参数)!" end
    if not week or week < 1 or week > 7 then return "请提供正确的初始周几 (week 参数,1~7)!" end
    
    -- 初始化结果表
    local result = {}
    local current_date = date
    local current_weekday = week
    
    -- 循环生成7行
    for i = 1, 7 do
        local title = args["title" .. i] or "无" -- 未提供标题时默认 "无"
        local time = args["time" .. i] or "2:00" -- 未提供时间时默认 "2:00"
        local class = args["class" .. i] or "" -- 未提供 class 时默认空字符串
        local week_day = get_next_weekday(current_weekday - 1) -- 计算周几
        
        -- 调用 ruby 模板
        local ruby_output = frame:expandTemplate{
            title = "ruby",
            args = {current_date, time}
        }
        
        -- 格式化当前行,添加 class
        if class ~= "" then
            table.insert(result, string.format('<div class="%s">%s<small>%s</small> %s</div>', class, ruby_output, week_day, title))
        else
            table.insert(result, string.format('%s<small>%s</small> %s', ruby_output, week_day, title))
        end
        
        -- 更新日期和周几
        current_date = get_next_date(current_date, leap_year)
        current_weekday = (current_weekday % 7) + 1
    end
    
    -- 返回结果,每行分开
    return table.concat(result, "\n")
end

return p