Module:Building infobox: Difference between revisions
From Beyond These Stars Official Wiki
m WIP building infobox |
added icon |
||
| Line 13: | Line 13: | ||
local INFOBOX_CLASS = "bts-infobox-building" | local INFOBOX_CLASS = "bts-infobox-building" | ||
local HEADER_CLASS = INFOBOX_CLASS .. "-header" | local HEADER_CLASS = INFOBOX_CLASS .. "-header" | ||
local HEADER_ICON_CLASS = HEADER_CLASS .. "-icon" | |||
local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description" | local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description" | ||
local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction" | local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction" | ||
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table" | local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table" | ||
local TASKS_CLASS = INFOBOX_CLASS .. "-tasks" | local TASKS_CLASS = INFOBOX_CLASS .. "-tasks" | ||
--- Renders wikitext for the building's icon. | |||
---@param building BuildingRecord | |||
---@return string wikitext | |||
local function render_icon_markup(building) | |||
local icon_filename = "BuildingPortrait_" .. building.name .. ".png" | |||
local image_markup = "[[File:" .. icon_filename .. "|256px]]" | |||
return image_markup | |||
end | |||
--- Adds the header of the infobox to the provided html node. | --- Adds the header of the infobox to the provided html node. | ||
| Line 24: | Line 34: | ||
local header = div:tag("div"):addClass(HEADER_CLASS) | local header = div:tag("div"):addClass(HEADER_CLASS) | ||
header:tag("h2"):wikitext(building.display_name):done() | header:tag("h2"):wikitext(building.display_name):done() | ||
header:tag("div"):addClass(HEADER_ICON_CLASS):wikitext(render_icon_markup(building)) | |||
header:tag("h3"):wikitext(building.category):done() | header:tag("h3"):wikitext(building.category):done() | ||
end | end | ||
--- Adds the building's description to the provided html node. | --- Adds the building's description to the provided html node. | ||
| Line 36: | Line 49: | ||
return description | return description | ||
end | end | ||
--- Adds the construction resources to the provided html table row. | --- Adds the construction resources to the provided html table row. | ||
| Line 73: | Line 88: | ||
return table | return table | ||
end | end | ||
--- Lays out the building's tasks in the provided html node. | --- Lays out the building's tasks in the provided html node. | ||
| Line 81: | Line 98: | ||
local section = div:tag("div"):addClass(TASKS_CLASS) | local section = div:tag("div"):addClass(TASKS_CLASS) | ||
end | end | ||
--- Builds the infobox wikitext and html for the given building. | --- Builds the infobox wikitext and html for the given building. | ||
Revision as of 02:04, 21 September 2026
Documentation for this module may be created at Module:Building infobox/doc
local p = {}
local BuildingDataModule = require("Module:Building_data")
--#region html building helper functions
---@alias HtmlNode table
local INFOBOX_CLASS = "bts-infobox-building"
local HEADER_CLASS = INFOBOX_CLASS .. "-header"
local HEADER_ICON_CLASS = HEADER_CLASS .. "-icon"
local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description"
local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction"
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table"
local TASKS_CLASS = INFOBOX_CLASS .. "-tasks"
--- Renders wikitext for the building's icon.
---@param building BuildingRecord
---@return string wikitext
local function render_icon_markup(building)
local icon_filename = "BuildingPortrait_" .. building.name .. ".png"
local image_markup = "[[File:" .. icon_filename .. "|256px]]"
return image_markup
end
--- Adds the header of the infobox to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_header(div, building)
local header = div:tag("div"):addClass(HEADER_CLASS)
header:tag("h2"):wikitext(building.display_name):done()
header:tag("div"):addClass(HEADER_ICON_CLASS):wikitext(render_icon_markup(building))
header:tag("h3"):wikitext(building.category):done()
end
--- Adds the building's description to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_description(div, building)
local description = div:tag("div"):addClass(DESCRIPTION_CLASS)
building.description = building.description:gsub("<hb>", ""):gsub("</hb>", "")
description:tag("p"):wikitext(building.description):done()
return description
end
--- Adds the construction resources to the provided html table row.
---@param tr HtmlNode table row
---@param building BuildingRecord
local function add_construction_resource_row(tr, building)
for _, pair in ipairs(building.construction_resources) do
if pair.name and pair.name ~= "" and pair.amount and pair.amount ~= "" then
tr:tag("td"):wikitext(pair.name):done()
end
end
end
--- Adds the construction amount to the provided html table row.
--- @param tr HtmlNode table row
--- @param building BuildingRecord
local function add_construction_amount(tr, building)
for _, pair in ipairs(building.construction_resources) do
if pair.name and pair.name ~= "" and pair.amount and pair.amount ~= "" then
tr:tag("td"):wikitext(pair.amount):done()
end
end
end
--- Lays out the construction resources in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_construction(div, building)
if not building.construction_resources or building.construction_resources == {} then return end
local table = div:tag("div"):addClass(CONSTRUCTION_CLASS):tag("table"):addClass(CONSTRUCTION_TABLE_CLASS)
local tr_names = table:tag("tr")
add_construction_resource_row(tr_names, building)
tr_names:done()
local tr_amounts = table:tag("tr")
add_construction_amount(tr_amounts, building)
tr_amounts:done()
return table
end
--- Lays out the building's tasks in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_tasks(div, building)
if not building.tasks or building.tasks == {} then return end
local section = div:tag("div"):addClass(TASKS_CLASS)
end
--- Builds the infobox wikitext and html for the given building.
---@param building BuildingRecord
---@return string wikitext
local function build_infobox(building)
---@type HtmlNode
local html = mw.html.create("div")
html:addClass(INFOBOX_CLASS)
build_header(html, building)
build_description(html, building)
build_construction(html, building)
build_tasks(html, building)
return tostring(html)
end
--#endregion
--- `#invoke` entry point. Reads `page_name` from invoke args (not the parent
--- template); falls back to the current page title.
---@param frame table Scribunto frame from `#invoke`.
---@return string|nil Infobox wikitext, or nil if no cargo row exists.
function p.render(frame)
local args = frame.args or {}
---@type string
local name = args[1] or args.page_name or mw.title.getCurrentTitle().fullText
local building = BuildingDataModule.get_building(name)
if not building then return nil end
local wikitext = build_infobox(building)
return wikitext
end
return p


