Module:Tile

From Isekai Wiki

Documentation for this module may be created at Module: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)
	-- Detect by date first.
	local currentDate = frame:callParserFunction("#timel", "n-j")
	dayConfig = conf[currentDate]
	if (dayConfig ~= nil) then  -- Output tile for current date
		return dayConfig
	end
	-- Detect by day of week
	local currentDOW = "#" .. frame:callParserFunction("#timel", "N")
	dayConfig = conf[currentDOW]
	if (dayConfig ~= nil) then  -- Output tile for current day
		return dayConfig
	end
	-- Not a special day
	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