Jump to content

Module:User:Happy5214/FTBox

From Wikipedia, the free encyclopedia
local p = {}

local format = string.format
local insert = table.insert

local function classWithName(class, name)
	return format("{{icon|%s}} [[%s]]", class, name)
end

function p._ftbox(args)
	local featuredCount = 0
	local goodCount = 0
	local topicName = args.title
	local topicImage = args.image
	local topicImageSize = args.imagesize
	local topicLeadClass = args.leadClass
	if topicLeadClass == 'FA' or topicLeadClass == 'FL' then
		featuredCount = featuredCount + 1
		goodCount = goodCount + 1
	elseif topicLeadClass == 'A' or topicLeadClass == 'GA' then
		goodCount = goodCount + 1
	end
	local topicLeadName = args.leadName
	local articleArgs = {}
	local articleCount = 1
	while true do
		local articleClass = args[articleCount * 2 - 1]
		if not articleClass then break end
		if articleClass == 'FA' or articleClass == 'FL' then
			featuredCount = featuredCount + 1
			goodCount = goodCount + 1
		elseif articleClass == 'A' or articleClass == 'GA' then
			goodCount = goodCount + 1
		end
		local article = {class = articleClass, name = args[articleCount * 2]}
		insert(articleArgs, article)
		articleCount = articleCount + 1
	end
	local column2Start = args.column2start
	local column3Start = args.column3start

	-- Start generating output
	-- Topic box
	local lines = {}
	insert(lines, format("{{Featured topic box|title=%s", topicName))
	insert(lines, "|lead=" .. classWithName(topicLeadClass, topicLeadName))
	insert(lines, "|image=" .. topicImage)
	insert(lines, "|imagesize=" .. topicImageSize)
	insert(lines, "|count=" .. articleCount)
	insert(lines, "|column1=")
	for i = 1, column2Start - 1 do
		insert(lines, "*" .. classWithName(articleArgs[i].class, articleArgs[i].name))
	end
	insert(lines, "|column2=")
	for i = column2Start, column3Start - 1 do
		insert(lines, "*" .. classWithName(articleArgs[i].class, articleArgs[i].name))
	end
	insert(lines, "|column3=")
	for i = column3Start, articleCount - 1 do
		insert(lines, "*" .. classWithName(articleArgs[i].class, articleArgs[i].name))
	end
	insert(lines, "}}")

	-- Progress bars
	insert(lines, format("{{Progress bar|%d|total=%d|text=Featured Articles, Featured Lists <small>(50%% required for FT)</small>}}", featuredCount, articleCount))
	insert(lines, format("{{Progress bar|%d|total=%d|text=Featured or Good Articles, Featured Lists <small>(100%% required for GT)</small>}}", goodCount, articleCount))

	local wikitext = table.concat(lines, '\n')
	return wikitext
end

function p.ftbox(frame)
	local argModule = require('Module:Arguments')
	local args = argModule.getArgs(frame)
	return frame:preprocess(p._ftbox(args))
end

return p