Open main menu

Documentation for this module may be created at Module:Infobox/Documentation

local p = {}

local File = require("Module:File")
local Term = require("Module:Term")
local utilsString = require("Module:UtilsString")

local FIELDS = require("Module:Infobox/Fields")

function p._Main(frame)
	local args = frame.args
	local templateArgs = frame:getParent().args
	return p.Main(templateArgs, args["type"])
end

function p.Main(args, type)
	local result = p.BuildInfobox(args, type)
	return result
end

function p.BuildInfobox(args, type)
	-- Building table and global fields (name, image, etc)
	local infobox = mw.html.create("table")
	:addClass("infobox wikitable")
	
	-- Name field
	-- If a |name= parameter is provided, it will use it, else it will try to
	-- use the Series term
	
	local nameDisplayed = args["name"]
	if utilsString.isEmpty(nameDisplayed) then
		nameDisplayed = Term.printTerm({page = mw.title.getCurrentTitle().text})
	else
		nameDisplayed = nameDisplayed .. "[[Category:Infoboxes Using the Name Parameter]]"
	end
		
	infobox:node(mw.html.create("tr"):node(mw.html.create("th")
		:addClass("infobox-name centered")
		:attr("colspan", "2")
		:wikitext(nameDisplayed)))
	
	-- Image field
	if not utilsString.isEmpty(args["image"]) then
		local imageWikitext = args["image"]
		if string.sub(args["image"], 1, 5) == "File:" then
			imageWikitext = File.image(args["image"], {
				size = "320x320px",
				scale = 10,
			})
		end
		if not utilsString.isEmpty(args["caption"]) then
			imageWikitext = imageWikitext .. "<br>" .. args["caption"] .. "[[Category:Infoboxes Using Captions]]"
		end
		infobox:node(mw.html.create("tr"):node(mw.html.create("td")
			:addClass("infobox-image centered")
			:attr("colspan", "2")
			:wikitext(imageWikitext)))
	end
	
	local row = ""
	-- Generated fields
	for key, field in ipairs(FIELDS[type]) do
		if not utilsString.isEmpty(args[field["parameter"]]) then
			row = mw.html.create("tr")
				:addClass("infobox-field")
			if field["type"] == "wide" then
				row:node(mw.html.create("th")
					:addClass("centered")
					:attr("colspan", "2")
					:wikitext(args[field["parameter"]]))
			else
				row:node(mw.html.create("th")
						:wikitext(field["caption"]))
					:node(mw.html.create("td")
						:node(mw.html.create("div")
							:addClass("infobox-field-content")
							:wikitext(args[field["parameter"]])))
			end
			infobox:node(row)
		end
	end
	
	return infobox
end

-- Documentation
function p._UsageDocumentation(frame)
	local args = frame.args
	return p.UsageDocumentation(args["langcode"], args["type"])
end

function p.UsageDocumentation(langcode, infoboxType)
	local result = ""
	local preHeader = "{{Infobox " .. string.gsub(infoboxType, "^%l", string.upper)
	local preFooter = "}}"

	local descriptions = mw.html.create("pre"):wikitext(preHeader)
	local blank = mw.html.create("pre"):wikitext(preHeader)
	
	-- Adding global fields
	descriptions:newline():wikitext("|image= Image(s) of the topic. (Use {{Media}} when possible.)")
	descriptions:newline():wikitext("|caption= Caption of the image. (Not to be used with {{Media}}.)")
	blank:newline():wikitext("|image= ")
	blank:newline():wikitext("|caption= ")
	
	-- Adding generated fields
	for key, field in ipairs(FIELDS[infoboxType]) do
		descriptions:newline():wikitext("|" .. field["parameter"] .. "= " .. field["description"])
		blank:newline():wikitext("|" .. field["parameter"] .. "= ")
	end

	descriptions:newline():wikitext(preFooter)
	blank:newline():wikitext(preFooter)
	return descriptions, "\nBlank code:\n", blank
end


return p