Module:TodayBirthday
跳转到导航
跳转到搜索
这是一个用于显示某个主题中在今天以及近期过生日的角色的模块,其封装模板为{{TodayBirthday}}。
调用方法
- 建议使用封装模板{{TodayBirthday}}进行调用。
使用<syntaxhighlight inline lang="wikitext">package.lua第80行Lua错误:module 'Module:TodayBirthday/data/数据模块名' not found</syntaxhighlight>即可调用模块,各参数说明如下:
- 数据模块名:存放在
Module:TodayBirthday/data/下的数据模块名称,不需要写前缀。 - 角色称呼:输出语句中对角色的称呼,可省略,默认为“角色”。
- 最近生日天数:设置显示最近多少天内过生日的角色,设为0为不显示,默认为0。
- 设置当前时间:给定计算生日的“当天”时间,测试用,在封装模板中不提供此参数,默认为今天。
count:传入非空值时返回最近过生日角色数。count_today:传入非空值时返回今天过生日角色数。
示例
- 调用<syntaxhighlight inline lang="wikitext">7天内没有赛马娘过生日。</syntaxhighlight>将显示:
7天内没有赛马娘过生日。
- 调用<syntaxhighlight inline lang="wikitext">祝无声铃鹿、狂怒乐章生日快乐!7天内过生日的赛马娘有:特别周(5月2日)、骏川手纲(5月2日)、成田白仁(5月3日)、谷野美酒(5月4日)、目白多伯(5月6日)、稻荷一(5月7日)、待兼诗歌剧(5月7日)、森林宝穴(5月7日)、樱花桂冠(5月8日)。</syntaxhighlight>将显示:
祝无声铃鹿、狂怒乐章生日快乐!7天内过生日的赛马娘有:特别周(5月2日)、骏川手纲(5月2日)、成田白仁(5月3日)、谷野美酒(5月4日)、目白多伯(5月6日)、稻荷一(5月7日)、待兼诗歌剧(5月7日)、森林宝穴(5月7日)、樱花桂冠(5月8日)。
- 调用<syntaxhighlight inline lang="wikitext">0</syntaxhighlight>将显示:0
- 调用<syntaxhighlight inline lang="wikitext">0</syntaxhighlight>将显示:0
- 调用<syntaxhighlight inline lang="wikitext">11</syntaxhighlight>将显示:11
- 调用<syntaxhighlight inline lang="wikitext">2</syntaxhighlight>将显示:2
使用方法
使用以下按钮创建一个主题的角色数据模块:
以下为目前已创建的角色数据模块,请注意不要重复创建(点击展开):
数据填写说明
使用以上方式创建新模块后会有如下代码: <syntaxhighlight lang="lua"> local p = {}
p.data={
{
"", --填写显示名称
"", --填写内部链接
1,1 --填写生日,形式:月,日
},
{ --以同样格式添加下一个数据
"",
"",
1,1
},
}
return p </syntaxhighlight>
需要填写的是使用--注释的三行(即5-7行),说明如下:
- 在第一行填写角色显示名称,支持html,但建议将第一行的字符串改为用单引号
'包裹,或者将双引号"使用反斜杠\转义,写为\";并且如果这么做第二行必须要填写内部链接。
- 显示图片:受wiki内链生成机制限制,图片不能以
[[File:]]的形式插入,请使用<img />语法插入图片
- 显示图片:受wiki内链生成机制限制,图片不能以
- 第二行填写内部链接,若与显示名称相同则可省略,不需要写
[[和]]。 - 第三行填写生日,以
月,日的形式。注意分隔符为半角逗号,。
填写示例: <syntaxhighlight lang="lua">
{
"特别周",
"",
5,2
},
</syntaxhighlight>
使用html的填写示例: <syntaxhighlight lang="lua">
{
'特别周',
"特别周",
5,2
},
</syntaxhighlight>
然后在下一行填写新的角色数据即可。
local p = {}
local config = {
name = "角色",
recent = 0
}
local data = {}
--接近生日排序函数
function birth_comp(a, b)
return a[2] < b[2]
end
--frame.args[1]:数据模块名称
--frame.args[2]:角色称呼
--frame.args[3]:最近生日天数
--frame.args[4]:指定当前日期
--frame.args['count_today']:是否返回今天过生日人数
--frame.args['count']:是否返回最近过生日人数
function p.main(frame)
--变量定义
local lang = mw.language.new("zh") --语言库
local today = {} --今天过生日
local recent = {} --最近过生日
local recent_aout = {}
local today_out = ""
local recent_out = ""
local year = os.date("%Y") --年份
local now = "" -- 当前时间
local count_today = 0 --今天过生日人数
local count_recent = 0 --最近过生日人数
-- 处理参数和默认值
if frame.args[1] then
local data_module = require("Module:TodayBirthday/data/" .. frame.args[1])
data = data_module.data
else
return '<span class="scribunto-error">请指定数据模块名!</span>'
end
if frame.args[2] then
config.name = frame.args[2]
end
if tonumber(frame.args[3]) then
config.recent = tonumber(frame.args[3])
end
if frame.args[4] then
now = lang:formatDate("U", frame.args[4], true)
year = lang:formatDate("Y", frame.args[4], true)
else
now = lang:formatDate("U", "", true)
end
--计算是否达到/接近生日
for k, v in ipairs(data) do
local birth = lang:formatDate("U", year .. "-" .. v[3] .. "-" .. v[4] .. "-8hour", true)
local birth_next = lang:formatDate("U", year .. "-" .. v[3] .. "-" .. v[4] .. "-8hour+1 year", true)
local link = ""
if v[2] == "" then
link = "[[" .. v[1] .. "]]"
else
link = "[[" .. v[2] .. "|" .. v[1] .. "]]"
end
if (now - birth >= 0 and now - birth < 86400) or (now - birth_next >= 0 and now - birth_next < 86400) then
table.insert(today, link)
count_today = count_today + 1
end
if config.recent > 0 then
if birth - now > 0 and birth - now <= 86400 * config.recent then
table.insert(recent, {link, birth - now, v[3], v[4]})
table.insert(recent_aout, link)
elseif birth_next - now > 0 and birth_next - now <= 86400 * config.recent then
table.insert(recent, {link, birth_next - now, v[3], v[4]})
table.insert(recent_aout, link)
end
end
end
--生成今天生日字符串
for k, v in ipairs(today) do
if today_out == "" then
today_out = v
else
today_out = today_out .. "、" .. v
end
end
--处理接近生日顺序
if config.recent > 0 then
table.sort(recent, birth_comp)
for k, v in ipairs(recent) do
if recent_out == "" then
recent_out = v[1] .. "<small>(" .. v[3] .. "月" .. v[4] .. "日)</small>"
else
recent_out = recent_out .. "、" .. v[1] .. "<small>(" .. v[3] .. "月" .. v[4] .. "日)</small>"
end
count_recent = count_recent + 1
end
end
--处理输出
local out = ""
if count_today == 0 then
out = ""
else
out = "祝" .. today_out .. "生日快乐!"
end
if config.recent > 0 then
if recent_out == "" then
out = out .. config.recent .. "天内没有" .. config.name .. "过生日。"
else
out = out .. config.recent .. "天内过生日的" .. config.name .. "有:" .. recent_out .. "。"
end
else
out = out .. "。"
end
if frame.args['count'] and frame.args['count'] ~= '' then
return count_recent + count_today -- 返回最近过生日人数
elseif frame.args['count_today'] and frame.args['count_today'] ~= '' then
return count_today -- 返回今天过生日人数
else
return out
end
end
return p