模块: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(conf, frame)
	-- 先根据日期判断	
	local currentDate = frame:callParserFunction("#timel", "n-j")
	dayConfig = conf[currentDate]
	if (dayConfig ~= nil) then  -- 输出当前日期的磁贴
		return dayConfig
	end
	-- 然后判断是周几
	local currentDOW = "#" .. frame:callParserFunction("#timel", "N")
	dayConfig = conf[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
	return tileArgs
end

function p.perDay(frame)
	local configTitle = frame.args[1] or frame.args.conf
	local appendTile = frame.args[2] or frame.args.appendTile
	local defaultTile = frame.args[3] or frame.args.default
	local conf = loadConfig(configTitle)
	local tileConfig = getDayConfig(conf, frame)
	if tileConfig ~= nil then
		local tileArgs = getTileArgs(tileConfig)
		return frame:extensionTag("tilegroup", 
			frame:extensionTag("tile", tileConfig.title, tileArgs) .. appendTile,
			{ size = "sm-12 md-6" })
	else
		return frame:extensionTag("tilegroup", defaultTile, { size = "sm-12 md-6" })
	end
end

return p