Module:InventoryFormatter
Jump to navigation
Jump to search
Documentation for this module may be created at Module:InventoryFormatter/doc
local InventoryFormatter = {}
local outermostFormatString = [[
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-qud-inventory"><div class="mw-collapsible-content qud-inv-content-itemlist" style="display:none;">%s</div></div>]]
local equippedFormatString = [[
<div class="qud-inv-group qud-equipped"><div class="qud-equipped-title">Equipped</div>%s</div>]]
local carriedFormatString = [[
<div class="qud-inv-group qud-carried"><div class="qud-equipped-title">Carried</div>%s</div>]]
local itemFormatString = [[
<div class="qud-inventory-item">{{favilink|%s}} <span class="qud-item-qty">x%s</span></div>]]
local function formatItem(item)
return itemFormatString:format(item.name, item.quantity)
end
local function formatItems(formatString, items)
if #items > 0 then
local itemsString = ''
for _, item in ipairs(items) do
itemsString = itemsString .. formatItem(item)
end
return formatString:format(itemsString)
else
return ''
end
end
--[[
Example usage:
{{#invoke: InventoryFormatter | format
| item name a
| 2
| yes
| item name b
| 3
| no
}}
Each item comprises three arguments, which are, in order:
- its display name
- its quantity
- "yes" if it's equipped, or anything else if it's not
--]]
function InventoryFormatter.format(frame)
local args = frame.args
local equippedItems, carriedItems = {}, {}
for n = 1, #args, 3 do
local name, quantity, equipped = args[n], args[n + 1], args[n + 2]
local item = {
name = name,
quantity = quantity,
}
local list = equipped == 'yes' and equippedItems or carriedItems
table.insert(list, item)
end
return outermostFormatString:format(
formatItems(equippedFormatString, equippedItems) ..
formatItems(carriedFormatString, carriedItems))
end
return InventoryFormatter