Open Beta is out on IOS/Android, PC & Mac! Play Now for Free! |
Module:Infobox
Revision as of 05:20, 18 November 2022 by User (talk | contribs) (Created page with "-- -- 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 = "InfoBo...")
Documentation for this module may be created at Module:Infobox/doc
-- -- 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:renderInformation() 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("Video") self:addWikitext(self.args.video) end if hasContent(self.args.son) then self:addHeader("Sound") 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 label = self.args["extra_content_row" .. i .. "_label"] data = self.args["extra_content_row" .. i .. "_data"] if hasContent(label) and hasContent(data) then if hasContent(self.args["extra_content_row" .. (i -1) .. "_data"]) then data = '<hr>'..data -- Add a separator if the previous row had content. end self:addRow(label, data) end end end function InfoBox:renderInformation() 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("Information") if hasContent(self.args.english_term) then self:addRow("English Term", self.args.english_term) end if hasContent(self.args.other_names) then self:addRow("Other Names", 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 specialty = {} -- 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("Specialty"..(#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]]--