Module:Infobox

local function table_print (tt, indent, done) done = done or {} indent = indent or 0 if type(tt) == "table" then local sb = {} for key, value in pairs (tt) do     table.insert(sb, string.rep (" ", indent)) -- indent it      if type (value) == "table" and not done [value] then done [value] = true table.insert(sb, "{\n"); table.insert(sb, table_print (value, indent + 2, done)) table.insert(sb, string.rep (" ", indent)) -- indent it       table.insert(sb, "}\n"); elseif "number" == type(key) then table.insert(sb, string.format("\"%s\"\n", tostring(value))) else table.insert(sb, string.format( "%s = \"%s\"\n", tostring (key), tostring(value))) end end return table.concat(sb) else return tt .. "\n" end end

local function to_string( tbl ) if "nil"       == type( tbl ) then return tostring(nil) elseif "table" == type( tbl ) then return table_print(tbl) elseif "string" == type( tbl ) then return tbl else return tostring(tbl) end end

local p = {} local namedArgs = {}

-- Translation note: Translatable strings are enclosed [=[ this sort ]=] of delimiter.

-- This variable holds the list of item types. local itemTypes = { armor = "Armor", block = "Solid Block", sblock = "Solid Block", oblock = "Solid Block", tblock = "Transparent Block", component = "Component", crop = "Crop", entity = "Entity", tentity = "Tile Entity", food = "Food", item = "Item", machine = "Machine", mob = "Monster", hmob = "Hostile Monster", nmob = "Neutral Monster", fmob = "Friendly Monster", seed = "Seed", tool = "Tool", wand = "Wand", weap = "Weapon", weapon = "Weapon", hull = "Steve's Carts Hull", module = "Steve's Carts Module", upgrade = "Steve's Carts Upgrade", landmark = "Landmark", ritual = "Ritual", liquid = "Liquid", ["multiblock structure"] = "Multiblock Structure", enchant = "Enchantment", mechanic = "Mechanic", dimension = "Dimension", flower = "Flower", command = "Command" }

-- This variable holds the shape of the infobox. -- It is a list of sections. -- Each section has a name (shown in documentation), a title (shown in the infobox), -- and then a list of subsections. Sections with consecutive names are merged in the docs. -- -- Each subsection is a list of rows. -- Each row is a list of one or two strings. -- A row with only one entry is made into a single cell with rowspan=2 -- Otherwise the first entry is bolded and the second is not. -- -- If instead of a string you use a table, it is interpreted as referring to a named argument -- given to the invocation. -- use =string to denote what argument to use, where type is image,imageIcon,text. -- Use desc for the text to be displayed in documentation. -- Use required--set it truthy--to indicate that it's an error to leave this argument out.

local infoboxFormat = { {       name = "Basic", title = {text="name", required=true, desc="The title that will be displayed at the top of the info box."}, {           { {image="image", desc="The big picture. Use the complete filename, like \"Block_Sand.png\".", required=true } }, { {imageIcon="imageicon", required = true, desc="The icon image. Use the item name as it appears in game. If you want to use multiple image icons, put multiple grid templates like in crafting grid templates or separate the item names using commas. This parameter uses to test if the input are grids and the #grid_foreach parser function to generate grids from a list. The ore dict is disabled for this parameter."} } },       {               { "Mod", {text="mod", required=true, desc="The mod the item belongs to. Always refer to the main page of the mod. Please do not use version numbers like \"Industrial Craft 2\" or \"RedPower 2\"; just use the regular name instead. (Will detect page language and link to respective languages)"} }, { "Type", {switch="type", values=itemTypes, desc="The item type.", required=true} }, { "Lore", {text="lore", desc="Additional information that is displayed on the tooltip of the item in game. If the in-game tooltip has custom colors, they should be added to this parameter using the template. Colors that are typically used can be found on the Minecraft Wiki." }},           { "Module", {text="module", desc="The module or modules that are required to be installed/enabled for the item to be present in the mod. Some examples of mods that add modules include RedPower 2, Project Red, and Railcraft."}} }   },    {        name="Basic", title="Properties", {           {"Blast Resistance", {text="blastresistance", desc="The blast resistance of the block. Include only the number."} }, {"Hardness", {text="hardness", desc="The hardness of a block." }}       }    }   }

-- Using the given frame as context, produce the string required for argdata. local function resolveArgument(frame, argdata) if not argdata then return nil end if type(argdata) == "string" then return argdata end

local argname = (argdata.text or argdata.imageIcon or argdata.image) local text = frame.args[argname] if not text then return nil end if argdata.text then return text elseif argdata.imageIcon then return text elseif argdata.image then return '' end end

local function resolveArgumentAlwaysEmpty(frame, argdata) return "" end

local function curryResolveArg(frame) return function(argdata) return resolveArgument(frame, argdata) end end

local function curryResolveArgAlwaysEmpty(frame) return function(argdata) return resolveArgumentAlwaysEmpty(frame, argdata) end end

local function renderInfobox(effectiveBox, class) result = {} local out = function(thing) result[#result+1] = thing end out(" ") return table.concat(result, "") end

function p.infobox(frame) local resolveArg = curryResolveArg(frame) effectiveBox = {} for i,section in ipairs(infoboxFormat) do       local newSection = { title = resolveArg(section.title)} for j,subsection in ipairs(section) do           local newSubsection = {} for k,row in ipairs(subsection) do               local newRow = { resolveArg(row[1]), resolveArg(row[2])} if (#row == #newRow) then newSubsection[#newSubsection+1] = newRow end end if not (#newSubsection == 0) then newSection[#newSection+1] = newSubsection end end if not (#newSection == 0) then effectiveBox[#effectiveBox+1] = newSection end end --return table_print(effectiveBox) return renderInfobox(effectiveBox, frame.args.class) end

function p.templateCall(frame) local resolveArg = curryResolveArgAlwaysEmpty(frame) result = { "{{#invoke:Infobox" } for i,section in ipairs(infoboxFormat) do       if type(section.name) == "table" then result[#result + 1] = "|" .. resolveArg(section.name) end for j,subsection in ipairs(section) do           for k,row in ipairs(subsection) do                for m,cell in ipairs(row) do                    if type(cell) == "table" then result[#result + 1] = "|" .. resolveArg(cell) end end end end end result[#result + 1] = "}}"

return table.concat(result, "") end

function p.infoboxDoc(frame)

end

return p