Module:Infobox

From Luanti Wiki
Revision as of 18:17, 7 May 2017 by MrIbby (talk | contribs) (Replaced getArgs with makeInvokeFunc)
Jump to navigation Jump to search

local p = {}

local TableBuilder = require("Module:TableBuilder") local getLanguageCode = require("Module:Languages").getLanguageCode local animateImages = require("Module:Animated")._images local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc local dataPrefix = "Module:Infobox/"

-- Get data (IDs and names) for infobox row headers. -- -- @param type Infobox type (e.g. block) -- @param langCode Language code, defaults to current language local function getRowHeadersData(type, langCode)

   langCode = langCode or getLanguageCode()
   local data = mw.loadData(dataPrefix .. langCode)
   return data and data[type] or nil

end

-- Create an infobox. -- -- @param args Infobox arguments -- @param type Infobox type (e.g. block) local function infobox(args, type)

   local rowHeadersData = getRowHeadersData(type) or getRowHeadersData(type, "en")
   local name = args.name
   local width = args.width or "200px"
   local description = args.description
   local image = args.image or "Blank.png"
   local imagesize = args.imagesize
   local imageText
   if string.match(image, ",") then
       imageText = animateImages({image, imagesize})
   else
       if imagesize then
           imageText = "" .. imagesize .. ""
       else
           imageText = "File:" .. image .. ""
       end
   end
   local infobox = TableBuilder.create()
   infobox
       :addClass("wikitable")
       :css("float", "right")
       :css("text-align", "left")
       :css("margin", "0 0 0.5em 1em")
       :css("padding", "5px")
       :css("font-size", "90%")
       :css("position", "relative")
       :css("clear", "right")
       :css("overflow", "auto")
       :css("z-index", "1")
       :attr("width", width)
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :css("font-size", "110%")
               :css("text-align", "center")
               :wikitext(name)
               :done()
           :done()
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :tag("div")
                   :addClass("center")
                   :wikitext(imageText)
                   :done()
               :done()
           :done()
       :addRow()
           :addHeader()
               :attr("colspan", 2)
               :attr("align", "center")
               :wikitext(description)
   for _, rowHeaderData in ipairs(rowHeadersData) do
       local rowData = args[rowHeaderData.id]
       if rowData and rowData ~= "" then
           infobox
               :addRow()
                   :addData()
                       :wikitext("" .. rowHeaderData.name .. "")
                       :done()
                   :addData()
                       :wikitext(rowData)
       end
   end
   return infobox

end

function p._block(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "block"))

end

function p._item(args)

   args.imagesize = args.imagesize or "160px"
   return tostring(infobox(args, "item"))

end

function p._foodItem(args)

   args.imagesize = args.imagesize or "160px"
   return tostring(infobox(args, "foodItem"))

end

function p._mob(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "mob"))

end

function p._game(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "game"))

end

function p._mod(args)

   args.imagesize = args.imagesize or "150px"
   return tostring(infobox(args, "mod"))

end

p.block = makeInvokeFunc(p._block, {inherited = true}) p.item = makeInvokeFunc(p._item, {inherited = true}) p.foodItem = makeInvokeFunc(p._foodItem, {inherited = true}) p.mob = makeInvokeFunc(p._mob, {inherited = true}) p.game = makeInvokeFunc(p._game, {inherited = true}) p.mod = makeInvokeFunc(p._mod, {inherited = true})

return p