Module:Jcgoble3/Topic display box

From Wikipedia

Documentation for this module may be created at Module:Jcgoble3/Topic display box/doc

-- Conversion of [[wikia:starwars:Template:Topic display box]]

local p = {}

-- function to output the appropriate icon for each article
function icon(code, px)
    local image, blank
	if code == "FA" then
		image = "[[File:LinkFA-star.png|%dpx]]"
	elseif code == "GA" then
		image = "[[File:GoodIcon.png|%dpx]]"
	elseif code == "CA" then
		image = "[[File:30px-ComprehensiveArticle.png|%dpx]]"
	elseif code == "FFA" then
		image = "[[File:LinkDFA-Star.png|%dpx]]"
	elseif code == "FGA" then
		image = "[[File:30px-FormerGAicon.png|%dpx]]"
	elseif code == "FCA" then
		image = "[[File:30px-FormerCAIcon.png|%dpx]]"
	else
		image = "[[File:Blank.svg|%dpx]]"
		blank = true
	end
	return image:format(px), blank
end

-- primary function called by template
function p.displaybox(frame)
	
	-- obtain parameters from template call
	local pframe = frame:getParent()
	local args = pframe.args
	
	-- initialize main variables from parameters
	local topictype = (args.type or ""):upper()                                 -- topic type
	local title = args.title or " "                                              -- topic title
	local titlestatus = (args.titlestatus or ""):upper()                        -- status of title article
	local fullcategory = (args.fullcategory ~= "") and args.fullcategory or nil -- category for full list of possible articles
	local statuslist = {}                                                       -- table that will contain list of statuses
	local articlelist = {}                                                      -- table that will contain list of articles
	
	-- process unnamed parameters, which alternate between status and article
	for n, value in ipairs(args) do
		if n % 2 == 1 then
			statuslist[#statuslist + 1] = value:upper()
		else
			articlelist[#articlelist + 1] = value
		end
	end
	
	-- declare variable that will contain background color
	local bgcolor
	-- determines background color based on topictype
	if topictype == "FT" then
		bgcolor = "FAFAD2"
	elseif topictype == "GT" then
		bgcolor = "ADDFAD"
	elseif topictype == "CT" then
		bgcolor = "FAB09E"
	else
		bgcolor = "FFFFFF" -- default to white if topictype is unrecognized
	end
	
	-- declare variable that will contain status icon for title of box
	local titleicon, nostatus = icon(titlestatus, 20, "")
	if nostatus then
		titleicon = ""
	end

	-- declare variable that will contain list of lines to be used in creating the finished product
	local lines = {}
	-- combine statuslist entries with the corresponding articlelist entries, calling articleicon() function to convert code into image
	for n, article in ipairs(articlelist) do
		lines[#lines + 1] = string.format("%s %s", icon(statuslist[n], 15), article)
	end
	-- if a category is provided, add an extra line with the category link
	if fullcategory then
		lines[#lines + 1] = string.format("(''[[:Category:%s|Full list of possible articles]]'')", fullcategory)
	end
	
	-- declare variables that will contain number and width of columns, respectively
	local columns, colwidth
	-- set number/width of columns, width is expressed as a percentage
	if #lines == 1 then
		columns, colwidth = 1, 100	
	elseif #lines < 7 then
		columns, colwidth = 2, 50
	else
		columns, colwidth = 3, 33
	end
	
	-- calculate number of lines per column (last column may be up to two short)
	local lpc = math.ceil(#lines / columns)
	
	-- now the real work begins
	
	-- build finished product
	local ret = {}
	ret[#ret + 1] = string.format('<div style="margin: 1em 0; padding: 7px; background-color: #%s; border: 1px solid #999; ', bgcolor)
	ret[#ret + 1] = 'box-shadow: 0.1em 0.1em 0.5em rgba(0,0,0,0.75); -moz-box-shadow: 0.1em 0.1em 0.5em rgba(0,0,0,0.75); -webkit-box-shadow: 0.1em 0.1em 0.5em rgba(0,0,0,0.75); '
	ret[#ret + 1] = 'border-radius: 1em; -moz-border-radius: 1em; -webkit-border-radius: 1em; width: auto;">'
	ret[#ret + 1] = string.format("\n<big><center>%s '''%s'''</center></big>", titleicon, title)
	ret[#ret + 1] = '\n<div>\n{| style="width: 100%; background: none;"'
	
	for c = 1, columns do
		if c == 3 then
			colwidth = 34
		end
	
		-- add cell styling for each column
		ret[#ret + 1] = string.format('\n| style="vertical-align: top; text-align: left; width: %d%%" | ', colwidth)
	
		-- calculates what line to start current column at, based on column number (c)
		local columnstart = lpc * (c - 1) + 1
		-- calculates what line to end current column at
		local columnend = math.min(lpc * c, #lines)
	
		-- build column; each "line" begins with a <br /> unless it is the first of the column
		for line = columnstart, columnend do
			if line == columnstart then
				ret[#ret + 1] = lines[line]
			else
				ret[#ret + 1] = "<br />" .. lines[line]
			end
		end
	end
	
	ret[#ret + 1] = "\n|}\n</div></div>"
	
	return table.concat(ret)
end

return p