Module:Cost: Difference between revisions
From Domwiki
More actions
Fix cost rendering for multi-digit values |
Pass cost module input as named raw argument |
||
| Line 53: | Line 53: | ||
function p.render(frame) | function p.render(frame) | ||
local raw = frame.args[1] or '' | local raw = frame.args.raw or frame.args[1] or '' | ||
local class = frame.args.class or 'domwiki-cost' | local class = frame.args.class or 'domwiki-cost' | ||
local size = frame.args.size or '18x18px' | local size = frame.args.size or '18x18px' | ||
Revision as of 22:02, 15 May 2026
Documentation for this module may be created at Module:Cost/doc
local p = {}
local pathNames = {
F = 'Fire',
A = 'Air',
W = 'Water',
E = 'Earth',
S = 'Astral',
D = 'Death',
N = 'Nature',
G = 'Glamour',
B = 'Blood',
H = 'Holy'
}
local labelNames = {
fatigue = 'fatigue',
gems = 'gems',
gold = 'gold',
res = 'resources',
resources = 'resources',
rec = 'recruitment points',
recruit = 'recruitment points',
cmd = 'commander points',
commander = 'commander points'
}
local function span(class, title, contents)
return tostring(mw.html.create('span')
:addClass(class)
:attr('title', title)
:wikitext(contents))
end
local function renderGem(path, amount, class, size)
local name = pathNames[path] or path
local file = string.format('[[File:Gem_%s.png|%s|link=|alt=%s gem]]', path, size, name)
local root = mw.html.create('span')
:addClass(class)
:addClass(class .. '--gem')
:attr('title', name .. ' gems')
:wikitext(file)
root:tag('span')
:addClass(class .. '__amount')
:wikitext(amount)
return tostring(root)
end
local function renderLabel(kind, amount, class)
local label = labelNames[kind] or kind
return span(class .. ' ' .. class .. '--' .. kind, label, amount .. ' ' .. label)
end
function p.render(frame)
local raw = frame.args.raw or frame.args[1] or ''
local class = frame.args.class or 'domwiki-cost'
local size = frame.args.size or '18x18px'
local output = {}
raw = mw.text.trim(raw:gsub(',', ' '))
for token in raw:gmatch('%S+') do
local path, amount = token:match('^([FAWESDNGBH])(%d+)$')
if path then
table.insert(output, renderGem(path, amount, class, size))
else
local key, value = token:match('^([%a_]+)=(%-?%d+)$')
if not key then
key, value = token:match('^([%a_]+)(%-?%d+)$')
end
if key then
table.insert(output, renderLabel(key, value, class))
else
table.insert(output, token)
end
end
end
return table.concat(output, ' ')
end
return p