Difference between revisions of "Module:Infobox"

From Minetest Wiki
Jump to navigation Jump to search
m (Set default food item name to "A food item")
m (Added support for translations)
Line 2: Line 2:
  
 
local TableBuilder = require("Module:TableBuilder")
 
local TableBuilder = require("Module:TableBuilder")
 +
local getLanguageCode = require("Module:Languages").getLanguageCode
 
local animateImages = require("Module:Animated")._images
 
local animateImages = require("Module:Animated")._images
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
Line 35: Line 36:
 
end
 
end
  
local function infobox(args)
+
local function infobox(args, type, names)
 +
    local data = mw.loadData("Module:Infobox/" + getLanguageCode())[type]
 
     local name = args.name
 
     local name = args.name
 
     local width = args.width or "200px"
 
     local width = args.width or "200px"
 
 
     local description = args.description
 
     local description = args.description
    local includewhere = args.includewhere ~= false
 
    if description and includewhere then
 
        local wherein = args.wherein or "[[Subgames/Minetest Game|Minetest Game]]"
 
        description = description .. " in " .. wherein
 
    end
 
 
 
     local image = args.image or "Blank.png"
 
     local image = args.image or "Blank.png"
 
     local imagesize = args.imagesize
 
     local imagesize = args.imagesize
Line 59: Line 54:
 
     end
 
     end
  
     return TableBuilder.create()
+
     local infobox = TableBuilder.create()
 +
 
 +
    infobox
 
         :addClass("wikitable")
 
         :addClass("wikitable")
 
         :css("float", "right")
 
         :css("float", "right")
Line 93: Line 90:
 
                 :attr("align", "center")
 
                 :attr("align", "center")
 
                 :wikitext(description)
 
                 :wikitext(description)
                :allDone()
 
end
 
  
local function addInfoRow(infobox, name, info)
+
    for _, name in ipairs(names) do
    local row = infobox:addRow()
+
        infobox
    row:addData():wikitext("'''" .. name .. "'''")
+
            :addRow()
    row:addData():wikitext(info)
+
                :addData()
 +
                    :wikitext("'''" .. data[name] .. "'''")
 +
                    :done()
 +
                :addData()
 +
                    :wikitext(args[name])
 +
    end
 
end
 
end
  
 
function p._block(args)
 
function p._block(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "150px"
         name = args.name or args.block_name or "A block",
+
     local infobox = infobox(args, infobox, "block", {
         image = args.image,
+
         "type",
         imagesize = "150px",
+
        "drops",
         description = "A [[Blocks|block]]",
+
        "physics",
         wherein = args.wherein,
+
        "luminance",
         width = args.width
+
         "flammable",
 +
         "generated",
 +
         "renewable",
 +
         "stackable",
 +
         "itemstring"
 
     })
 
     })
    addInfoRow(infobox, "Type", args.type or args.block_type or "Solid block")
 
    addInfoRow(infobox, "[[Mining#Drops|Drops]]", args.drops or "''Itself''")
 
    addInfoRow(infobox, "[[:Category:Falling|Physics]]", args.physics or "No")
 
    addInfoRow(infobox, "[[:Category:Luminous|Luminance]]", args.luminance or "No")
 
    addInfoRow(infobox, "[[:Category:Flammable|Flammable]]", args.flammable or "No")
 
    addInfoRow(infobox, "[[:Category:Generated_blocks|Generated]]", args.generated or "Yes")
 
    addInfoRow(infobox, "Renewable", args.renewable or "No")
 
    addInfoRow(infobox, "Stackable", args.stackable or "Yes (99)")
 
    addInfoRow(infobox, "[[Itemstrings|Itemstring]]", args.itemstring or "''?''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
function p._item(args)
 
function p._item(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "160px"
        name = args.name or args.item_name or "An item",
+
     local infobox = infobox(args.imagesize, infobox, "item", {
         image = args.image,
+
         "type",
         imagesize = "160px",
+
         "renewable",
         description = "An [[Items|item]]",
+
         "durability",
         wherein = args.wherein,
+
         "stackable",
         width = args.width
+
         "itemstring"
 
     })
 
     })
    addInfoRow(infobox, "Type", args.type or args.item_type or "Craftitem")
 
    addInfoRow(infobox, "Renewable", args.renewable or "No")
 
    addInfoRow(infobox, "Durability", args.durability or "''N/A''")
 
    addInfoRow(infobox, "Stackable", args.stackable or "Yes (99)")
 
    addInfoRow(infobox, "[[Itemstrings|Itemstring]]", args.itemstring or "''?''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
function p._foodItem(args)
 
function p._foodItem(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "160px"
         name = args.name or args.item_name or "A food item",
+
     local infobox = infobox(args, infobox, "foodItem", {
         image = args.image,
+
         "type",
         imagesize = "160px",
+
         "restores",
         description = "A food [[Items|item]]",
+
         "cookable",
         wherein = args.wherein,
+
         "renewable",
         width = args.width
+
         "stackable",
 +
         "itemstring"
 
     })
 
     })
    addInfoRow(infobox, "Type", args.type or args.item_type or "Food")
 
    addInfoRow(infobox, "Restores", args.restores or "''?''")
 
    addInfoRow(infobox, "Cookable", args.cookable or "No")
 
    addInfoRow(infobox, "Renewable", args.renewable or "No")
 
    addInfoRow(infobox, "Stackable", args.stackable or "Yes (99)")
 
    addInfoRow(infobox, "[[Itemstrings|Itemstring]]", args.itemstring or "''?''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
function p._mob(args)
 
function p._mob(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "150px"
        name = args.name or args.mob_name or "A mob",
+
     local infobox = infobox(args, infobox, "mob", {
         image = args.image,
+
         "health",
         imagesize = "150px",
+
         "armor",
         description = "A [[Mobs|mob]]",
+
         "damage",
         wherein = args.wherein,
+
         "drops",
         width = args.width
+
         "entitystring"
 
     })
 
     })
    addInfoRow(infobox, "Health", args.health or "''?''")
 
    addInfoRow(infobox, "Armor", args.armor or "''N/A''")
 
    addInfoRow(infobox, "Damage", args.damage or "''N/A''")
 
    addInfoRow(infobox, "Drops", args.drops or "''N/A''")
 
    addInfoRow(infobox, "Entitystring", args.entitystring or "''?''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
function p._game(args)
 
function p._game(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "150px"
        name = args.name or "A subgame",
+
     local infobox = infobox(args, infobox, "game", {
         image = args.image,
+
         "type",
         imagesize = "150px",
+
         "author",
         description = "A [[Subgames|subgame]]",
+
         "version",
         includewhere = false,
+
         "forum",
         width = args.width
+
         "source"
 
     })
 
     })
    addInfoRow(infobox, "Type", args.type or "''?''")
 
    addInfoRow(infobox, "Author", args.author or "''?''")
 
    addInfoRow(infobox, "Latest version", args.version or "''?''")
 
    addInfoRow(infobox, "Forum topic", args.forum or "''N/A''")
 
    addInfoRow(infobox, "Source code", args.source or "''N/A''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
function p._mod(args)
 
function p._mod(args)
     local infobox = infobox({
+
    args.imagesize = args.imagesize or "150px"
         name = args.name or "A mod",
+
     local infobox = infobox(args, infobox, "mod", {
         image = args.image,
+
         "type",
         imagesize = "150px",
+
         "author",
         description = "A [[Mods|mod]]",
+
         "version",
         includewhere = false,
+
         "forum",
         width = args.width
+
         "source",
 +
         "luaname"
 
     })
 
     })
    addInfoRow(infobox, "Type", args.type or "''?''")
 
    addInfoRow(infobox, "Author", args.author or "''?''")
 
    addInfoRow(infobox, "Latest version", args.version or "''?''")
 
    addInfoRow(infobox, "Forum topic", args.forum or "''N/A''")
 
    addInfoRow(infobox, "Source code", args.source or "''N/A''")
 
    addInfoRow(infobox, "Technical name", args.luaname or "''?''")
 
 
     return tostring(infobox)
 
     return tostring(infobox)
 
end
 
end
  
 
return p
 
return p

Revision as of 09:12, 13 April 2017

local p = {}

local TableBuilder = require("Module:TableBuilder") local getLanguageCode = require("Module:Languages").getLanguageCode local animateImages = require("Module:Animated")._images local getArgs = require('Module:Arguments').getArgs

function p.block(frame)

   local args = getArgs(frame, {inherited = true})
   return p._block(args)

end

function p.item(frame)

   local args = getArgs(frame, {inherited = true})
   return p._item(args)

end

function p.foodItem(frame)

   local args = getArgs(frame, {inherited = true})
   return p._foodItem(args)

end

function p.mob(frame)

   local args = getArgs(frame, {inherited = true})
   return p._mob(args)

end

function p.game(frame)

   local args = getArgs(frame, {inherited = true})
   return p._game(args)

end

function p.mod(frame)

   local args = getArgs(frame, {inherited = true})
   return p._mod(args)

end

local function infobox(args, type, names)

   local data = mw.loadData("Module:Infobox/" + getLanguageCode())[type]
   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 _, name in ipairs(names) do
       infobox
           :addRow()
               :addData()
                   :wikitext("" .. data[name] .. "")
                   :done()
               :addData()
                   :wikitext(args[name])
   end

end

function p._block(args)

   args.imagesize = args.imagesize or "150px"
   local infobox = infobox(args, infobox, "block", {
       "type",
       "drops",
       "physics",
       "luminance",
       "flammable",
       "generated",
       "renewable",
       "stackable",
       "itemstring"
   })
   return tostring(infobox)

end

function p._item(args)

   args.imagesize = args.imagesize or "160px"
   local infobox = infobox(args.imagesize, infobox, "item", {
       "type",
       "renewable",
       "durability",
       "stackable",
       "itemstring"
   })
   return tostring(infobox)

end

function p._foodItem(args)

   args.imagesize = args.imagesize or "160px"
   local infobox = infobox(args, infobox, "foodItem", {
       "type",
       "restores",
       "cookable",
       "renewable",
       "stackable",
       "itemstring"
   })
   return tostring(infobox)

end

function p._mob(args)

   args.imagesize = args.imagesize or "150px"
   local infobox = infobox(args, infobox, "mob", {
       "health",
       "armor",
       "damage",
       "drops",
       "entitystring"
   })
   return tostring(infobox)

end

function p._game(args)

   args.imagesize = args.imagesize or "150px"
   local infobox = infobox(args, infobox, "game", {
       "type",
       "author",
       "version",
       "forum",
       "source"
   })
   return tostring(infobox)

end

function p._mod(args)

   args.imagesize = args.imagesize or "150px"
   local infobox = infobox(args, infobox, "mod", {
       "type",
       "author",
       "version",
       "forum",
       "source",
       "luaname"
   })
   return tostring(infobox)

end

return p