Difference between revisions of "Module:Infobox"

From Minetest Wiki
Jump to navigation Jump to search
(Created page with "local p = {} local HtmlBuilder = require("Module:HtmlBuilder") local animateImages = require("Module:Animated")._images function p.block(frame) local args = getArgs(fram...")
 
(FnControlOption changed the content model of the page Module:Infobox from "plain text" to "Scribunto")
Tag: content model change
 
(28 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local HtmlBuilder = require("Module:HtmlBuilder")
+
local TableBuilder = require("Module:TableBuilder")
 +
local getLanguageCode = require("Module:Languages").getLanguageCode
 
local animateImages = require("Module:Animated")._images
 
local animateImages = require("Module:Animated")._images
 +
local makeInvokeFunc = require("Module:Arguments").makeInvokeFunc
 +
local dataPrefix = "Module:Infobox/"
  
function p.block(frame)
+
-- Get data (IDs and names) for infobox row headers.
    local args = getArgs(frame, {inherited = true})
+
--
    return p._block(args)
+
-- @param type Infobox type (e.g. block)
end
+
-- @param langCode Language code, defaults to current language
 
+
-- @param frame Current frame object
local function addInfoRow(infobox, name, info)
+
local function getRowHeadersData(type, langCode, frame)
     local row = infobox:tag("tr")
+
     langCode = langCode or getLanguageCode()
     row:tag("td"):wikitext(name)
+
     frame = frame or mw.getCurrentFrame()
    row:tag("td"):wikitext(info)
+
     local title = dataPrefix .. langCode
end
+
     if frame:preprocess("{{#ifexist:" .. title .. "|true}}") ~= "" then
 
+
        local data = mw.loadData(title)
function p._block(args)
+
        return data and data[type] or nil
     local infobox = p.infobox({
+
    end
        name = args.name or args.block_name or "A block",
+
     return nil
        image = args.image,
 
        description = "A [[Blocks|block]]",
 
        wherein = args.wherein
 
     })
 
    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)
 
 
end
 
end
  
function p.infobox(args)
+
-- 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 name = args.name
     local description = args.description .. " in " .. (args.wherein or "[[Subgames/Minetest Game|Minetest Game]]")
+
     local width = args.width or "200px"
     local image = args.image or "Minetest logo.png"
+
    local description = args.description
 +
     local image = args.image or "Blank.png"
 +
    local imagesize = args.imagesize
 
     local imageText
 
     local imageText
 
     if string.match(image, ",") then
 
     if string.match(image, ",") then
         imageText = animateImages({image, "150px"})
+
         imageText = animateImages({image, imagesize})
 
     else
 
     else
         imageText = "[[File:" .. image .. "|150px]]"
+
         if imagesize then
 +
            imageText = "[[File:" .. image .. "|" .. imagesize .. "]]"
 +
        else
 +
            imageText = "[[File:" .. image .. "]]"
 +
        end
 
     end
 
     end
     return HtmlBuilder.create("table")
+
 
         :tag("tr")
+
     local infobox = TableBuilder.create()
            :tag("th")
+
 
 +
    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)
 
                 :attr("colspan", 2)
 
                 :css("font-size", "110%")
 
                 :css("font-size", "110%")
Line 53: Line 67:
 
                 :done()
 
                 :done()
 
             :done()
 
             :done()
         :tag("tr")
+
         :addRow()
             :tag("th")
+
             :addHeader()
 
                 :attr("colspan", 2)
 
                 :attr("colspan", 2)
 
                 :tag("div")
 
                 :tag("div")
Line 62: Line 76:
 
                 :done()
 
                 :done()
 
             :done()
 
             :done()
         :tag("tr")
+
         :addRow()
             :tag("th")
+
             :addHeader()
 
                 :attr("colspan", 2)
 
                 :attr("colspan", 2)
 
                 :attr("align", "center")
 
                 :attr("align", "center")
 
                 :wikitext(description)
 
                 :wikitext(description)
                 :allDone()
+
 
 +
    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
 
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._object(args)
 +
    args.imagesize = args.imagesize or "150px"
 +
    return tostring(infobox(args, "object"))
 +
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
 +
 +
function p._server(args)
 +
    args.imagesize = args.imagesize or "150px"
 +
    return tostring(infobox(args, "server"))
 +
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.object = makeInvokeFunc(p._object, {inherited = true})
 +
p.game = makeInvokeFunc(p._game, {inherited = true})
 +
p.mod = makeInvokeFunc(p._mod, {inherited = true})
 +
p.server = makeInvokeFunc(p._server, {inherited = true})
  
 
return p
 
return p

Latest revision as of 17:39, 7 June 2022

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

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
-- @param frame Current frame object
local function getRowHeadersData(type, langCode, frame)
    langCode = langCode or getLanguageCode()
    frame = frame or mw.getCurrentFrame()
    local title = dataPrefix .. langCode
    if frame:preprocess("{{#ifexist:" .. title .. "|true}}") ~= "" then
        local data = mw.loadData(title)
        return data and data[type] or nil
    end
    return 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 = "[[File:" .. image .. "|" .. 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._object(args)
    args.imagesize = args.imagesize or "150px"
    return tostring(infobox(args, "object"))
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

function p._server(args)
    args.imagesize = args.imagesize or "150px"
    return tostring(infobox(args, "server"))
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.object = makeInvokeFunc(p._object, {inherited = true})
p.game = makeInvokeFunc(p._game, {inherited = true})
p.mod = makeInvokeFunc(p._mod, {inherited = true})
p.server = makeInvokeFunc(p._server, {inherited = true})

return p