Module:Cost: Difference between revisions
From Domwiki
More actions
Render spell costs through Lua module |
Parse cost amounts with string slicing |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 30: | Line 30: | ||
:addClass(class) | :addClass(class) | ||
:attr('title', title) | :attr('title', title) | ||
: | :node(contents)) | ||
end | end | ||
| Line 36: | Line 36: | ||
local name = pathNames[path] or path | local name = pathNames[path] or path | ||
local file = string.format('[[File:Gem_%s.png|%s|link=|alt=%s gem]]', path, size, name) | 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') | |||
:node(amount) | |||
return tostring(root) | |||
end | end | ||
| Line 44: | Line 52: | ||
end | end | ||
function | local function isDigits(value) | ||
local | return value ~= '' and value:match('^%d+$') ~= nil | ||
local | end | ||
local | |||
local function splitLabelToken(token) | |||
local delimiter = token:find(':', 1, true) or token:find('=', 1, true) | |||
if delimiter then | |||
local key = token:sub(1, delimiter - 1) | |||
local value = token:sub(delimiter + 1) | |||
if key:match('^[%a_]+$') and isDigits(value) then | |||
return key, value | |||
end | |||
end | |||
local key = token:match('^[%a_]+') | |||
if key then | |||
local value = token:sub(#key + 1) | |||
if isDigits(value) then | |||
return key, value | |||
end | |||
end | |||
return nil, nil | |||
end | |||
local function renderRaw(raw, class, size) | |||
local output = {} | local output = {} | ||
raw = mw.text.trim(raw:gsub(',', ' ')) | raw = mw.text.trim(raw:gsub(',', ' ')) | ||
for token in raw:gmatch('%S+') do | for token in raw:gmatch('%S+') do | ||
local path, amount = token: | local path = token:sub(1, 1) | ||
if path then | local amount = token:sub(2) | ||
if pathNames[path] and isDigits(amount) then | |||
table.insert(output, renderGem(path, amount, class, size)) | table.insert(output, renderGem(path, amount, class, size)) | ||
else | else | ||
local key, value = token | local key, value = splitLabelToken(token) | ||
if key then | if key then | ||
table.insert(output, renderLabel(key, value, class)) | table.insert(output, renderLabel(key, value, class)) | ||
| Line 66: | Line 97: | ||
return table.concat(output, ' ') | return table.concat(output, ' ') | ||
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' | |||
return renderRaw(raw, class, size) | |||
end | |||
function p.fromParent(frame) | |||
local field = frame.args[1] or 'cost' | |||
local parent = frame:getParent() | |||
local raw = parent and parent.args[field] or '' | |||
local class = frame.args.class or 'domwiki-cost' | |||
local size = frame.args.size or '18x18px' | |||
return renderRaw(raw, class, size) | |||
end | |||
function p.debugParent(frame) | |||
local field = frame.args[1] or 'cost' | |||
local parent = frame:getParent() | |||
return parent and parent.args[field] or '' | |||
end | end | ||
return p | return p | ||
Latest revision as of 22:12, 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)
:node(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')
:node(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
local function isDigits(value)
return value ~= '' and value:match('^%d+$') ~= nil
end
local function splitLabelToken(token)
local delimiter = token:find(':', 1, true) or token:find('=', 1, true)
if delimiter then
local key = token:sub(1, delimiter - 1)
local value = token:sub(delimiter + 1)
if key:match('^[%a_]+$') and isDigits(value) then
return key, value
end
end
local key = token:match('^[%a_]+')
if key then
local value = token:sub(#key + 1)
if isDigits(value) then
return key, value
end
end
return nil, nil
end
local function renderRaw(raw, class, size)
local output = {}
raw = mw.text.trim(raw:gsub(',', ' '))
for token in raw:gmatch('%S+') do
local path = token:sub(1, 1)
local amount = token:sub(2)
if pathNames[path] and isDigits(amount) then
table.insert(output, renderGem(path, amount, class, size))
else
local key, value = splitLabelToken(token)
if key then
table.insert(output, renderLabel(key, value, class))
else
table.insert(output, token)
end
end
end
return table.concat(output, ' ')
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'
return renderRaw(raw, class, size)
end
function p.fromParent(frame)
local field = frame.args[1] or 'cost'
local parent = frame:getParent()
local raw = parent and parent.args[field] or ''
local class = frame.args.class or 'domwiki-cost'
local size = frame.args.size or '18x18px'
return renderRaw(raw, class, size)
end
function p.debugParent(frame)
local field = frame.args[1] or 'cost'
local parent = frame:getParent()
return parent and parent.args[field] or ''
end
return p