Module:Resources table: Difference between revisions

From Beyond These Stars Official Wiki
WIP
resource names now links to pages; categories now links to category pages
 
Line 73: Line 73:
   local row = mw.html.create("tr")
   local row = mw.html.create("tr")
   row:tag("td"):wikitext(render_icon(resource))
   row:tag("td"):wikitext(render_icon(resource))
   row:tag("td"):wikitext(resource.display_name)
   row:tag("td"):wikitext("[[" .. resource.display_name .. "]]")
   row:tag("td"):wikitext(resource.category)
   row:tag("td"):wikitext("[[:Category:" .. resource.category .. " Resources|" .. resource.category .. "]]")
   row:tag("td"):wikitext(render_resource_list(resource))
   row:tag("td"):wikitext(render_resource_list(resource))
   row:tag("td"):wikitext(resource.peep_happiness_added)
   row:tag("td"):wikitext(resource.peep_happiness_added)

Latest revision as of 20:19, 10 October 2026

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

--[[
Test in Scribunto console with:
=p.render()
]]--
local p = {}



local Resource_Data_Module = require("Module:Resource_data")



--#region html builders

---@diagnostic disable-next-line: duplicate-doc-alias
---@alias HtmlNode table

local TOP_LEVEL_CLASS = "bts-table-resources"
local TABLE_CLASS = "wikitable sortable"
local UNSORTABLE_CLASS = "unsortable"

local LARGE_ICON_SIZE = "96px"



--- Adds the header row to the provided html node.
---@param wikitable HtmlNode wikitable
---@param resources Resource[]
local function add_header_row(wikitable, resources)
  local top_header = wikitable:tag("tr")
  top_header:tag("th"):attr("colspan", "4"):wikitext("")
  top_header:tag("th"):attr("colspan", "2"):wikitext("Peep Recovery")
  local header = wikitable:tag("tr")
  header:tag("th"):addClass(UNSORTABLE_CLASS):wikitext("Icon")
  header:tag("th"):wikitext("Name")
  header:tag("th"):wikitext("Category")
  header:tag("th"):addClass(UNSORTABLE_CLASS):wikitext("In Lists")
  header:tag("th"):wikitext("Happiness")
  header:tag("th"):wikitext("Exhaustion")
end



--- Renders the markup for the icon for the given resource.
---@param resource Resource
---@return string wikitext
local function render_icon(resource)
  if not resource or not resource.icon then return "" end
  local file_name = "File:" .. resource.icon .. ".png"
  local icon_size = "x" .. LARGE_ICON_SIZE
  local name = resource.display_name or resource.name
  local link_attr = "link=" .. name
  return "[[" .. file_name .. "|" .. icon_size .. "|" .. name .. "|" .. link_attr .. "]]"
end



--- Renders the markup for the resource lists for the given resource.
---@param resource Resource
---@return string wikitext
local function render_resource_list(resource)
  if not resource or not resource.resource_list or #resource.resource_list == 0 then return "" end
  local lists = table.concat(resource.resource_list, "<br />")
  return lists
end



--- Builds a row for the given resource.
---@param resource Resource
---@return HtmlNode row
local function build_row(resource)
  local row = mw.html.create("tr")
  row:tag("td"):wikitext(render_icon(resource))
  row:tag("td"):wikitext("[[" .. resource.display_name .. "]]")
  row:tag("td"):wikitext("[[:Category:" .. resource.category .. " Resources|" .. resource.category .. "]]")
  row:tag("td"):wikitext(render_resource_list(resource))
  row:tag("td"):wikitext(resource.peep_happiness_added)
  row:tag("td"):wikitext(resource.peep_exhaustion_removed)
  return row
end



--- Builds a table of resources.
---@param resources Resource[]
---@return string wikitext
local function build_table(resources)
  ---@type HtmlNode
  local div = mw.html.create("div")
  div:addClass(TOP_LEVEL_CLASS)
  local wikitable = div:tag("table"):addClass(TABLE_CLASS)
  add_header_row(wikitable, resources)
  for _, resource in ipairs(resources) do
    wikitable:node(build_row(resource))
  end
  return tostring(div)
end


--#endregion



--- `#invoke` entry point.
---@param frame table template context object
---@return string wikitext table wikitext code
function p.render(frame)
  local resources = Resource_Data_Module.get_all_resources()
  if #resources == 0 then return "" end
  local wikitext = build_table(resources)
  return wikitext
end

return p