Module:InfoBox

De Wikimedica
Sauter à la navigation Sauter à la recherche

La documentation pour ce module peut être créée à Module:InfoBox/Documentation

--
-- This module implements a basic infobox using capiunto.
--
 
local capiunto = require 'capiunto'

InfoBox = {}
InfoBox.__index = InfoBox -- So InfoBox can be used as a meta table for its instances.
-- No Idea why this not handled by the capiunto instance...
InfoBox.__tostring = function (t) return tostring(t:getHtml()) end

function hasContent(str)
	return str ~= nil and str ~= ""
end

function InfoBox.create(args)
	
	if args.title == nil then
		args.title = "InfoBox"
	end
	
	if hasContent(args.acronym) then
		args.title = args.title .. ' (' .. args.acronym .. ')'
	end
	
	local box = capiunto.create(args)
	
	--[[ Since capiunto does not keep any extra parameters passed in args
	besides the ones it knows, we have to repopulate the args table.]]--
	for key, val in pairs(args) do 
  		if box.args[key] == nil then
  			box.args[key] = val
  		end
	end
	
	-- Setup inheritance by allowing InfoBox to access and override capiunto's functions.
	setmetatable(InfoBox, getmetatable(box))
	setmetatable(box, InfoBox)
	return box
end

function InfoBox:render()
	
	frame = mw.getCurrentFrame()
	if(frame:getParent()) then 
		-- If frame.getParent() is defined, this function was called from a template.
		self = InfoBox.create(frame.args) -- Generate an instance of InfoBox.
	end

	self:renderImageAndDescription()
	self:renderVideoAndSound()
	self:renderExtraContent()
	self:renderInformations()
	self:renderBottom()
	
	return self
end

function InfoBox:renderImageAndDescription()
	if hasContent(self.args.class) then
		self:addSubHeader(self.args.class)
	end
	
	if hasContent(self.args.image) then
		self:addImage(self.args.image, self.args.caption ~= nil and self.args.caption or '')
	end
	
	return self
end

function InfoBox:renderVideoAndSound()	
	if hasContent(self.args.video) then
		self:addHeader("Vidéo")
		self:addWikitext(self.args.video)
	end
	
	if hasContent(self.args.son) then
		self:addHeader("Son")
		self:addWikitext(self.args.son)
	end
	
	return self
end

function InfoBox:renderExtraContent()
	if hasContent(self.args.extra_content_header) then
		self:addHeader(self.args.extra_content_header)
	end
	
	for i = 1, 10 do
		if hasContent(self.args["extra_content_row" .. i .. "_label"]) and hasContent(self.args["extra_content_row" .. i .. "_data"])  then
			self:addRow(self.args["extra_content_row" .. i .. "_label"], self.args["extra_content_row" .. i .. "_data"])
		end
	end
	
end

function InfoBox:renderInformations()
	
	if hasContent(self.args.english_term) or hasContent(self.args.other_names) or hasContent(self.args.specialties) or hasContent(self.args.wikidata_id) then
		self:addHeader("Informations")
		if hasContent(self.args.english_term) then
			self:addRow("Terme anglais", self.args.english_term)
		end
		if hasContent(self.args.other_names) then
			self:addRow("Autres noms", self.args.other_names)
		end
		
		if hasContent(self.args.wikidata_id) then
			self:addRow("Wikidata ID", "[https://wikidata.org/wiki/" .. self.args.wikidata_id .. " " .. self.args.wikidata_id .."]")
		end
		
		if hasContent(self.args.specialties) then
			local specialties = {}
			-- Insert a comma at the end of the string to make sure it splits correctly.
			for s in string.gmatch(self.args.specialties..",", "(.-),") do
				if s ~= nil and s ~= '' then
					table.insert(specialties, "[[" .. s:gsub("^%s*(.-)%s*$", "%1") .. "]]")
				end
			end
			
			self:addRow("Spécialité"..(#specialties > 1 and "s" or ""), table.concat(specialties, ", "))
		end
	end
	
	return self
end

function InfoBox:renderBottom()
	return self
end

return InfoBox

--[[
box = InfoBox.create({title = "Title", acronym = "AC", class = "Class", image = "IMG", caption = "caption"})
box:render()
return box]]--