模块:Tile
来自异世界百科
此模块的文档可以在模块:Tile/doc创建
local p = {}
local function loadConfig(titleText)
local title = mw.title.new(titleText)
if (not title.exists and title.contentModel ~= "json") then
return nil
end
local content = title:getContent()
return mw.text.jsonDecode(content)
end
local function getDayConfig(config)
-- 先根据日期判断
local currentDate = frame:callParserFunction("#timel", "n-j")
dayConfig = config[currentDate]
if (dayConfig ~= nil) then -- 输出当前日期的磁贴
return dayConfig
end
-- 然后判断是周几
local currentDOW = frame:callParserFunction("#timel", "D")
dayConfig = config[currentDOW]
if (dayConfig ~= nil) then -- 输出当前日期的磁贴
return dayConfig
end
-- 不存在当日配置
return nil
end
local function getTileArgs(tileConfig)
local tileArgs = {}
if tileConfig.cover ~= nil then
tileArgs.cover = tileConfig.cover
end
if tileConfig.page ~= nil then
tileArgs.href = "[[" .. tileConfig.page .. "]]"
end
if tileConfig.icon ~= nil then
tileArgs.icon = "fa fa-fw fa-" .. tileConfig.icon
end
end
function p.perDay(frame)
local configTitle = frame.args[1] or frame.args.conf
local default = frame.args[2] or frame.args.default
local defaultWide = frame.args[3] or frame.args.defaultWide
local conf = loadConfig(configTitle)
local tileConfig = getDayConfig(conf)
if tileConfig ~= nil then
local tileArgs = getTileArgs(tileConfig)
return '<tilegroup size="sm-12 md-6">' ..
frame:extensionTag('tile', tileConfig.title, tileArgs) ..
frame.args.default ..
'</tilegroup>'
else
return '<tilegroup size="sm-12 md-6">' .. defaultWide .. '</tilegroup>'
end
end
return p